10. switch case
TRANSCRIPT
![Page 1: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/1.jpg)
Switch – Case Statements
By: Er. Aman Kumar
![Page 2: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/2.jpg)
Switch-Case Statements
way2ITech
![Page 3: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/3.jpg)
• Switch case statement allows us to make decisions from multiple choices.
• Integer expression must yield an integer value like 1, 2, 3, etc. The keyword case is followed by an integer or a character constant.
• Each constant in each case must be different from all the others. The “do this” lines in the above form of switch represent any valid C statement.
way2ITech
![Page 4: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/4.jpg)
way2ITech
![Page 5: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/5.jpg)
• break always used with the switch statement to terminate the switch statement or to prevent the entry in other case.
way2ITech
![Page 6: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/6.jpg)
way2ITech
![Page 7: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/7.jpg)
way2ITech
![Page 8: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/8.jpg)
The Tips and Traps
• It is not compulsory to put cases in ascending order as we did above. We can place them in any order we want.
way2ITech
![Page 9: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/9.jpg)
You are also allowed to use char values in case and switch as
shown in the following program
(Program to find whether character entered is vowel or not)
way2ITech
![Page 10: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/10.jpg)
way2ITech
![Page 11: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/11.jpg)
Modified
Form
way2ITech
![Page 12: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/12.jpg)
If a statement doesn’t belong to any case the compiler won’t report an error. However, the statement would
never get executed. For example, in the following program the printf( ) never goes to work.
way2ITech
![Page 13: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/13.jpg)
• If there is no default in switch, then error will not be there. If no case is matching, statements after switch will execute.
• Relational Expression like i>5 are not allowed in any case. Even float values not allowed in any case
• We can check the value of any expression in a switch. Thus the following switch statements are legal.
switch ( i - j * k )
switch ( 3 + 9 % 4 * i )
switch ( a < 5 || b > 7 )
way2ITech
![Page 14: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/14.jpg)
• Expressions can also be used in cases provided they are constant expressions. Thus case 13 + 7 is correct, however, case a + b is incorrect.
• switch may occur within another, but in practice it is rarely done. Such statements would be called nested switch statements.
• The switch statement is very useful while writing menu driven programs
• Even if there are multiple statements to be executed in each case there is no need to enclose them within a pair of braces (unlike if, and else).
way2ITech
![Page 15: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/15.jpg)
switch Versus if-else Ladder
Disadvantages of switch over if-else
• A float expression cannot be tested using a switch
• Cases can never have variable expressions (for example it is wrong to say case a +3 : )
• Multiple cases cannot use same expressions. Thus the following switch is illegal:
way2ITech
![Page 16: 10. switch case](https://reader033.vdocuments.us/reader033/viewer/2022052509/55d220cabb61eb9c4d8b4656/html5/thumbnails/16.jpg)
Advantages of switch over if-else
• compiler generates a jump table for a switch during compilation. As a result, during execution it simply refers the jump table to decide which case should be executed, rather than actually checking which case is satisfied.
• As against this, if-else are slower because they are evaluated at execution time.
way2ITech