Switch Statement  The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts ...

Switch case

/
0 Comments
Switch Statement 

  • The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
  • The switch is a control statement that allows a value to change control of execution.
  • Switch case is like if else.And For multiple conditions, it can have same output without writing again and again.
  • In the last section, you saw that nested if statements are used when there is more than one decision to be made. The nested if statements will become very complex if there are many decisions that need to be made, however. Sometimes, the programmer will have problems following the complex if statements. Fortunately, there is another statement in C, the switch statement, that you can use to make unlimited decisions or choices based on the value of a conditional expression and specified cases. The general form of the switch statement is


switch (expression)
 {
 case expression1: statement1; 
 case expression2: statement2;
 .
 .
 . 
 default: statement-default; 
}

  • Here the conditional expression, expression, is evaluated first. If the return value of the expression is equal to the constant expression expression1, the statement statement1 is executed. If the value of the expression is the same as the value of expression2, statement2 is executed. If, however, the value of the expression is not equal to any values of the constant expressions labeled by the case keyword, the statement (statement-default) following the default keyword is executed.
  • You have to use the case keyword to label each case. The default keyword is recommended to be used for the default case. Note that no constant expressions are identical in the switch statement
  • Here the condition is executed as per the switch statement and if no condition is satisfied then the default option is definitely going to be executed.
  • The break Statement 
  • You can add a break statement at the end of the statement list following every case label, if you want to exit the switch construct after the statements within a selected case are executed.
  • In each case, there is a statement followed by a break statement. As mentioned, the break statements help to exit the switch construct after the statement in a selected case is executed.


Program:
1: /*  Using the switch statement */
 2: #include<stdio.h>
3:
4: main() 
5: {
 6: int day; 
7: 
8: printf("Please enter a single digit for a day\n");
 9: printf("(within the range of 1 to 3):\n");
 10: day = getchar(); 
11: switch (day){ 
12: case `1': 13: printf("Day 1\n"); 
14: case `2': 
15: printf("Day 2\n"); 
16: case `3': 
17: printf("Day 3\n"); 
18: default:
19: ; 
20: }
21: return 0;
22: }
Output:
Please enter a single digit for a day
(within the range of 1 to 3):
3
Day 3

  • As you can see in line 6, an int variable, day, is declared; it is assigned the input entered by the user in line 10.
  • In line 11, the value of the integer variable day is evaluated in the switch statement. If the value is equal to one of the values of the constant expressions, the computer starts to execute statements from there. The constant expressions are labeled by prefixing case in front of them.
  • For instance, I entered 3 and then pressed the Enter key. The numeric value of 3 is assigned to day in line 10. Then, after finding a case in which the value of the constant expression matches the value contained by day, the computer jumps to line 17 to execute the printf() function and display Day 3 on the screen.
  • Note that under the default label in Listing 10.4, there is an empty (that is, null) statement ending with a semicolon (;) in line 19. The computer does nothing with the empty statement.
  • However, if I enter 1 from my keyboard and then press the Enter key, I get the following output:
         Please enter a single digit for a day (within the range of 1 to 3):
        1
        Day 1
        Day 2
        Day 3 
  • From the output, you can see that the statement controlled by the selected case, case 1, and the statements controlled by the rest of the cases are executed, because Day 1, Day 2, and Day 3 are displayed on the screen. Likewise, if I enter 2 from my keyboard, I have Day2 and Day3 shown on the screen.
  • This is an important feature of the switch statement: The computer continues to execute the statements following the selected case until the end of the switch statement. 
  • Program
1: #include<stdio.h>
2:#include<conio.h>
3: 
4: main() 
5: { 
6: int day; 
7: 
8: printf("Please enter a single digit for a day\n"); 
9: printf("(within the range of 1 to 7):\n"); 
10: day = getchar(); 
11: switch (day){ 
12: case `1': 
13: printf("Day 1 is Sunday.\n");
14: break; 
15: case `2': 
16: printf("Day 2 is Monday.\n"); 
17: break; 
18: case `3': 
19: printf("Day 3 is Tuesday.\n"); 
20: break; 
21: case `4': 
22: printf("Day 4 is Wednesday.\n"); 
23: break;
24: case `5': 
25: printf("Day 5 is Thursday.\n"); 
26: break; 
27: case `6': 
28: printf("Day 6 is Friday.\n"); 
29: break; 
30: case `7': 
31: printf("Day 7 is Saturday.\n"); 
32: break;
 33: default:
 34: printf("The digit is not within the range of 1 to 7.\n"); 
35: break; 
36: } 
37: getch();
 38: }

OUTPUT 
With the help of the break statement, I can run the executable file 10L05.exe and only obtain the output of the selected case: 
Please enter a single digit for a day (within the range of 1 to 7): 
1
 Day 1 is Sunday.

No comments:

Cybercry 2018. Powered by Blogger.

Popular Posts