Switch Case in C Programming

SonyaRupa 6,799 views 14 slides Sep 14, 2016
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

Amazing slide show about Switch Case in Programming Language C. It's helpfull for engineering students .


Slide Content

1 Switch Case Hamdard University Bangladesh Sonya Akter Rupa Id-315161009 Department of CSE s [email protected] Presented by:

2 Switch Case Flow Chart of Switch Case Creating Menus Switch – syntax Switch Example Switch Default To Switch or not To Switch Uses of Switch Case Summary 0utlInes

Switch Case 3 The last statement of each case in the switch should almost always be a break. The break causes program control to jump to the closing brace of the switch structure. Without the break, the code flows into the next case. This is almost never what you want. A switch statement will compile without a default case, but always consider using one.

Flow Chart of Switch Case 4

5 Creating Menus When you want to give your user a choice on what to do next, you can display a set of choices (a menu). The user then enters his or her choice. You must validate the choice to make sure it is valid before you continue the program!

Switch - Syntax 6 The general syntax of a switch case is: switch and case are reserved words switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... } If expression matches value3 , control jumps to here

Switch Example 7 Example of the switch case: switch (option){ case 'A': aCount ++; break; case 'B': bCount ++; break; case 'C': cCount ++; break; }

Switch Example 8 switch ( day ) { case 0: printf (“Sunday\n”) ; break ; case 1: printf (“Monday\n”) ; break ; case 2: printf (“Tuesday\n”) ; break ; case 3: printf (“Wednesday\n”) ; break ; case 4: printf (“Thursday\n”) ; break ; case 5: printf (“Friday\n”) ; break ; case 6: printf (“Saturday\n”) ; break ; default: printf (“Error -- invalid day.\n”) ; break ; } Is this structure more efficient than the equivalent nested if-else structure?

Switch - Default 9 A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If the default case is present, control will transfer to it if no other case value matches If there is no default case, and no other value matches, control falls through to the statement after the switch

10 Switch With Default Case Example switch (option){ case 'A': aCount ++; break; case 'B': bCount ++; break; case 'C': cCount ++; break; default: otherCount ++; break; }

11 To Switch or not To Switch The expression of a switch statement must result in an integral type , meaning an integer (byte, short, int , long) or a char It cannot be a boolean value or a floating point value (float or double) The implicit boolean condition in a switch statement is equality You cannot perform relational checks with a switch statement

12 Why Use a switch case? A nested if-else structure is just as efficient as a switch case. However, a switch case may be easier to read. Also, it is easier to add new cases to a switch case than to a nested if-else structure.

13 Summary The break statement can be used as the last statement in each case's statement list A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case

THANKS TO ALL