C Have Switch Continue Cases Until
Switch case statement in C language
switch case
This case statement is a multi-way decision statement which is a simpler version of the if-else block which evaluates only one variable. The switch statement is used to pick up or execute a particular group of statements from several available group of statements. It allows us to make a decision from the number of choices. It is a multiway decision statement. It tests the value of given variable or expression against a list of case values and when a match is found, a block of statements associated with that case is executed. The expression follows the keyword switch.
Rules for writing switch() statements:
- The expression in switch statements must be an integer value or a character constant.
- No real numbers are used in an expression.
- Each case block and default blocks must be terminated with break statement.
- The default is optional and can be placed anywhere, but usually placed at end.
- The case keyword must terminate with colon (:).
- No two case constants are identical.
- The case labels must be constants.
- The switch can be nested.
- The value of switch expression is compared with the case constant expression in the order specified. i.e. from top to bottom.
- In the absence of break statement, all statements that are followed by matched cases are executed.
Syntax:
Switch(expression)
{
Case 1:
Statements_Block 1;
Break;
. .
. .
. .
Case n:
Statementd_Block n;
Break;
Default:
Default block;
}
Flow Chart:
Example-1: Write a C program to illustrate switch without break
#include<stdio.h> void main() { int i; printf(" Enter choice [1 or 2]\n"); scanf("%d",&i); switch(i) { case 1: printf("\n This is case 1\n"); case 2: printf("\n This is case 2\n"); default: printf("\n This is default case\n"); } getch(); }
Output:
Case-1
Case-2
Case-3
When we want to execute only one choice we must use break statement.
Break:
The break statement causes the program to jump out of the switch statement. Break is used to suspend sequential execution. If we add a break as the last statement in each case now only print statement will be executed.
Example-2: Write a C program to illustrate switch with break.
#include<stdio.h> void main() { int i; printf(" Enter choice\n"); scanf("%d",&i); switch(i) { case 1: printf("\n This is case 1 \n"); break; case 2: printf("\n This is case 2 \n"); break; default: printf("\n This is default case \n"); } getch(); }
Output:
Case-1
Case-2
Case-3
Example-3: Write a C program to perform arithmetic operations based on user choice.
#include<stdio.h> void main() { int a,b,ch; printf("\n 1. addition\n 2. subtraction\n 3. multiplication\n 4. division\n 5. modulo division\n"); printf("\n Enter your choice. \n"); scanf("%d",&ch); switch (ch) { case 1: printf("\n Enter two values to add \n"); scanf("%d%d",&a,&b); printf(" %d+%d = %d \n",a,b,a+b); break; case 2: printf("\n Enter two values to subtrct \n"); scanf("%d%d",&a,&b); printf("\n %d-%d = %d \n",a,b,a-b); break; case 3: printf("\n Enter two values to multiply \n"); scanf("%d%d",&a,&b); printf("\n %d*%d = %d \n",a,b,a*b); break; case 4: printf("\n Enter two values to divide \n"); scanf("%d%d",&a,&b); printf("\n %d/%d = %d \n",a,b,a/b); break; case 5: printf("\n Enter two values to modulo division \n"); scanf("%d%d",&a,&b); printf("\n %d%%%d = %d \n",a,b,a%b); break; default: printf("\n Invalid choice, Please enter valid choice. \n"); } getch(); }
Output:
Case-1
Case-2
Case-3
Case-4
Case-5
Case-6
Example-4: Write a program to check if the character entered is a vowel or not.
#include <stdio.h> void main() { char ch; printf(" Enter any character:\n"); scanf("%c", &ch); switch(ch) { case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u': printf("\n %c is a vowel \n",ch); break; default: printf("\n %c is not a vowel \n",ch); } }
Output:
Case-1
Case-2
henryyousuponchis.blogspot.com
Source: https://ssonlinecoding.com/c-language/c-switchcasebreak/
Postar um comentário for "C Have Switch Continue Cases Until"