switch case in c programming

14
1 Switch Case Presented by:

Upload: sonya-rupa

Post on 16-Apr-2017

137 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Switch Case in C Programming

1

Switch Case

Presented by:

Page 2: Switch Case in C Programming

2

Switch CaseFlow Chart of Switch CaseCreating MenusSwitch – syntaxSwitch ExampleSwitch DefaultTo Switch or not To SwitchUses of Switch CaseSummary

0utlInes

Page 3: Switch Case in C Programming

3

Switch Case 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.

Page 4: Switch Case in C Programming

Flow Chart of Switch Case

4

Page 5: Switch Case in C Programming

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!

Page 6: Switch Case in C Programming

Switch - Syntax

6

The general syntax of a switch case is:

switchandcaseare

reservedwords

switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ...

}If expressionmatches value3,control jumpsto here

Page 7: Switch Case in C Programming

Switch Example

7

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

Page 8: Switch Case in C Programming

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?

Page 9: Switch Case in C Programming

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

Page 10: Switch Case in C Programming

10

Switch With Default Case Example

switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; default: otherCount++;

break;}

Page 11: Switch Case in C Programming

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

Page 12: Switch Case in C Programming

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.

Page 13: Switch Case in C Programming

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

Page 14: Switch Case in C Programming

THANKS TO ALL