University of Washington Computer Programming IOverviewReview: Conditional Control FlowMulti-way Control FlowPowerPoint PresentationSlide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15J-1University of WashingtonComputer Programming ISwitch Statement© 2000 UW CSEJ-2OverviewConcepts this lectureThe switch statementChoosing between if and switchReadingTextbook sec. 4.8J-3Review: Conditional Control FlowThe if statement choses one of two statements to execute before continuingAn if statement could also be used to decide whether or not to to skip a statement before continuingJ-4Multi-way Control FlowThe choice may be “multi-way” rather than simply between two alternativesIn C, if statements can be used, and sometimes a statement called the switch can be usedJ-5Multi-way Choice with if/* How many days in a month? */if ( month == 1 ) { /* Jan */days = 31 ;} else if ( month == 2 ) { /* Feb */days = 28 ;} else if ( month == 3 ) { /* Mar */days = 31 ;} else if ( month == 4 ) /* Apr */days = 30 ; ... /* need 12 of these */J-6Better…if ( month == 9 || month == 4 || /* Sep, Apr */ month == 6 || month == 11 ) { /* Jun, Nov */ days = 30 ;} else if ( month == 2 ) { /* Feb */days = 28 ;} else {days = 31; /* All the rest */}J-7Alternative: switchA switch is a form of conditional statement.It is specifically designed to be useful in multi-way choice situations.Instead of a condition, there is a value which is tested, and a series of cases of which only one may be chosen.J-8Using switch/* How many days in a month? */switch ( month ) {case 2: /* February */days = 28 ;break ;case 9: /* September */case 4: /* April */case 6: /* June */case 11: /* November */days = 30 ;break ;default: /* All the rest have 31 ...*/days = 31 ;}printf ( “There are %d days. \n ”, days ) ;J-9switch StatementThe syntax of switch differs from other C statements switch (int expression) {... /*a series of cases */...}The value of the expression determines which of the cases is executed.J-10CasesA case is a section of code within the switch statement. A case is executed only if the switch expression has a specified valuecase value: /* a sequence of statements*/The sequence is typically ended with special statementbreak;break causes the entire switch statement to endJ-11The switch ExpressionThe switch expression is not a conditional expression as it is in an if statementOnly an integer expression is allowedMost often, the expression is a single integer variableThe value of the variable determines which case is chosenJ-12switch: Flow of Controlmonth = 6 ;switch ( month ) {case 2: /* February */days = 28 ;break ;case 9: /* September */case 4: /* April */case 6: /* June */case 11: /* November */days = 30 ;break ;default: /* All the rest have 31 ...*/days = 31 ;}printf ( “There are %d days. \n ”, days ) ;J-13The One Big Pitfall of switchmonth = 6 ;switch (month) {case 2: /* February */days = 28 ; /* break missing */case 9: /* September */case 4: /* April */case 6: /* June */case 11: /* November */days = 30 ; /* break missing */default: /* All the rest have 31 ... */days = 31 ;}printf ( “There are %d days. \n ”, days ) ;J-14char marital_status ;...switch ( marital_status ) {case ‘m’:case ‘M’:printf ( “Married \n” ) ;break ; int or char expressioncase ‘s’:case ‘S’:printf ( “Single \n” ) ;break ;default:printf ( “Sorry, I don’t recognize that code. \n” ) ;}switch on char is also legalJ-15Switch is a form of conditional statementSwitch is suitable for multi-way conditions that depend upon an integer (or char) valuePay attention to the syntax of switchThe switch and if statements are not fully interchangeableSumming
View Full Document