CSE 142 Computer Programming I Overview Concepts this lecture The switch statement Choosing between if and switch Reading Switch Statement Textbook sec 4 8 J 1 2000 UW CSE Review Conditional Control Flow The if statement chooses one of two statements to execute before continuing J 2 Multi way Control Flow The choice may be multi way rather than simply between two alternatives An if statement could also be used to decide whether or not to to skip a statement before continuing In C if statements can be used and sometimes a statement called the switch can be used J 3 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 Jan J 5 need 12 of these Feb J 4 Better if month 9 month 4 month 6 month 11 Sep Apr Jun Nov days 30 else if month 2 Feb days 28 Mar else days 31 Apr All the rest J 6 J 1 Using switch Alternative switch How many days in a month 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 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 Cases The syntax of switch differs from other C statements 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 switch int expression a series of cases The value of the expression determines which of the cases is executed break causes the entire switch statement to end J 9 The switch Expression switch Flow of Control 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 10 J 11 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 J 2 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 Summing Up 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 J 14 n Bonus Footnote 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 char marital status switch marital status case m case M Why should a character be allowed here when the expression is supposed to be an integer Answer The actual machine representation of a character is a small integer The switch and if statements are not fully interchangeable J 15 Most of the time however you should treat intsJ 16 and chars as fully different types J 3
View Full Document
Unlocking...