201Spring08-L11 - CMPSC 201C Spring 2008 Lecture 11...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 201C Spring 2008 Lecture 11 February 8, 2008 Switches C++ allows an alternative to the if/else if structure for multi-way branches called switches. The switch has a controlling expression that is evaluated and matched to a series of cases. The controlling expression if evaluated to an integer or single character. Ranges have to be handled very carefully (but in some instances they can be done)! Very commonly used for menus Switch Switch is an alternative method to treat multibranched decisions. A variable or expression will determine which branch will be executed. switch (integer expression) { case const1: statement(s) case const2: statements(s) case const3: statement(s) case constn: statement(s) default : statement(s) } Switch (cont) Integer expression is a variable or an expression that evaluates to an integer const1 - constn are integers or expressions that evaluate to integers Remember that char variables or constants are actually stored as their ASCII equivalent so `A' is an integer constant. The "default" case may be omitted. When present it contains the statements that are to be executed when no match is found. General Format of a Switch switch (expression) { case answer1: statement(s) break; case answer2: statement(s) break; case answer3: statement(s) break; default: statement(s) } Example - Switch switch (ltrgrade) { case `A': cout <<"You did exceptional!"<<endl; break; case `B': cout <<"You did better than average."<<endl; break; case `C': cout <<"You did average."<<endl; break; case `D': cout <<"You did below average."<<endl; break; default : cout<<"You failed"<<endl; } Switch (cont.) The integer expression is evaluated and an attempt to match it with constant expression. If a match is found, the statements associated with that specific constant expression are executed. The break statement ends the execution of statements for each case. If the break statement is omitted for a case, execution will continue using the statements for the next case until a break is encountered. Another Example switch (ltrgrade) { case `A': cout <<"You did exceptional!"<<endl; case `B': cout <<"You did better than average."<<endl; case `C': cout <<"You passed the course."<<endl; break; case `D': cout <<"You did below average."<<endl; default : cout<<"You failed"<<endl; } Items to Note The default case does not have to be included. If the default is not included and no cases match the expression, no statements will be executed. Although the case comparison must be an integer or a character, ranges with equal divisions can still use a switch, but manipulations are needed. For example in assigning the letter grades, you can manipulate the range so it evaluates to a single integer. Grade Example-switch Many times there is more than one or options that should be expressed. For example, think about assigning grades using the system: >= 90% ---- A >= 80% < 90% ---- B >=70% < 80% ---- C >= 60% < 70% ---- D <60% ---- F switch (int(percent)/10) //assumes percent was a floating pt number { case 10: case 9: grade = `A'; break; case 8: grade = `B'; break; case 7: grade = `C'; break; case 6: grade = `D'; break; default : grade = `F'; } Example calculator-switch.cpp Advantages /Disadvantages Advantages The "look" of the switch may be easier to understand because it looks more like a menu. Disadvantages Cannot easily do irregular ranges. Cannot match strings. Common Mistakes Omitting the break statements - once a match is found, statements will be executed until break is encountered or last statement of switch is executed. Not including all the possibilities - either the default statements will be executed or no statements will be executed. Omitting the braces, { } Questions ...
View Full Document

This note was uploaded on 03/19/2008 for the course CMPSC 201 taught by Professor Susanquick during the Spring '08 term at Pennsylvania State University, University Park.

Ask a homework question - tutors are online