<<

CSE 142 Overview Computer Programming I Concepts this lecture The switch statement Choosing between if and switch

Switch Statement Reading Textbook sec. 4.8

© 2000 UW CSE J-1 J-2

Review: Conditional Control Multi-way Flow The choice may be “multi-way” rather than simply between two alternatives The if statement An if statement could also In , if statements can be used, and sometimes a chooses one of two be used to decide whether statement called the switch can be used statements to execute or not to to skip a statement before continuing before continuing

J-3 J-4

Multi-way Choice with if Better… /* How many days in a month? */ if ( month == 9 || month == 4 || /* Sep, Apr */ month == 6 || month == 11 ) { /* Jun, Nov */ if ( month == 1 ) { /* Jan */ days = 30 ; days = 31 ; } else if ( month == 2 ) { /* Feb */ } else if ( month == 2 ) { /* Feb */ days = 28 ; days = 28 ; } else if ( month == 3 ) { /* Mar */ } else { days = 31 ; days = 31; /* All the rest */ } else if ( month == 4 ) /* Apr */ } days = 30 ; ... /* need 12 of theseJ-5 */ J-6

J-1 Alternative: switch Using switch /* How many days in a month? */ A switch is a form of conditional switch ( month ) { statement. case 2: /* February */ days = 28 ; break ; It is specifically designed to be useful in case 9: /* September */ case 4: /* April */ multi-way choice situations. case 6: /* June */ case 11: /* November */ days = 30 ; Instead of a condition, there is a value break ; default: /* All the rest have 31 ...*/ which is tested, and a series of cases of days = 31 ; which only one may be chosen. } J-7 printf ( "There are %d days. \n ", days ) ; J-8

switch Statement Cases The syntax of switch differs from other A case is a section of code within the switch C statements statement. A case is executed only if the switch expression has a specified value switch (int expression) { case value: ... /* a sequence of statements*/ /*a series of cases */ The sequence is typically ended with special ... statement } break;

The value of the expression determines break causes the entire switch statement to end which of the cases is executed. J-9 J-10

The switch Expression switch: Flow of Control month = 6 ; The switch expression is not a switch ( month ) { conditional expression as it is in an if case 2: /* February */ days = 28 ; statement break ; case 9: /* September */ case 4: /* April */ Only an integer expression is allowed case 6: /* June */ case 11: /* November */ days = 30 ; Most often, the expression is a single break ; integer variable default: /* All the rest have 31 ...*/ days = 31 ; }

The value of the variable determines J-11 printf ( "There are %d days. \n ", days ) ; J-12 which case is chosen

J-2 The One Big Pitfall of switch switch on char is also legal char marital_status ; month = 6 ; ... switch (month) { switch ( marital_status ) { case 2: /* February */ case ’m’: days = 28 ; /* break missing */ case ’M’: case 9: /* September */ printf ( "Married \n" ) ; case 4: /* April */ case 6: /* June */ break ; int or char expression case 11: /* November */ case ’s’: days = 30 ; /* break missing */ case ’S’: default: /* All the rest have 31 ... */ printf ( "Single \n" ) ; days = 31 ; break ; } default: printf ( "There are %d days. \n ", days ) ; printf ( "Sorry, I don’t recognize that code. \n" ) ; J-13 } J-14

Summing Up Bonus Footnote char marital_status ; Switch is a form of conditional statement ... switch ( marital_status ) { case 'm': Switch is suitable for multi-way case 'M': conditions that depend upon an integer … (or char) value Why should a character be allowed here, when the expression is supposed to be an integer?

Pay attention to the syntax of switch Answer: The actual machine representation of a character is a small integer. The switch and if statements are not fully J-15 Most of the time, however, you should treat intsJ-16 interchangeable and chars as fully different types!

J-3