University of Washington Computer Programming I Switch Statement 2000 UW CSE J 1 Overview Concepts this lecture The switch statement Choosing between if and switch Reading Textbook sec 4 8 J 2 Review Conditional Control Flow The if statement choses one of two statements to execute before continuing An if statement could also be used to decide whether or not to to skip a statement before continuing J 3 Multi way Control Flow The choice may be multi way rather than simply between two alternatives In C if statements can be used and sometimes a statement called the switch can be used J 4 Multi way Choice with if How many days in a month if month 1 days 31 else if month 2 days 28 else if month 3 days 31 else if month 4 days 30 need 12 of these Jan Feb Mar Apr J 5 Better if month 9 month 4 month 6 month 11 Sep Apr Jun Nov days 30 else if month 2 Feb days 28 else days 31 All the rest J 6 Alternative switch A 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 7 Using 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 8 switch Statement The 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 9 Cases A case is a section of code within the switch statement A case is executed only if the switch expression has a specified value case value a sequence of statements The sequence is typically ended with special statement break break causes the entire switch statement to end J 10 The switch Expression The switch expression is not a conditional expression as it is in an if statement Only an integer expression is allowed Most often the expression is a single integer variable The value of the variable determines which case is chosen J 11 switch Flow of Control month 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 12 The One Big Pitfall of switch month 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 13 switch on char is also legal char marital status switch marital status case m case M printf Married n break int or char expression case s case S printf Single n break default printf Sorry I don t recognize that code n J 14 Summing Up Switch is a form of conditional statement Switch is suitable for multi way conditions that depend upon an integer or char value Pay attention to the syntax of switch The switch and if statements are not fully interchangeable J 15
View Full Document
Unlocking...