Upload
kelley-parrish
View
214
Download
0
Embed Size (px)
Citation preview
© 2000 Scott S Albert
Selection Structures
Structured Programming 256
Chapter 4
© 2000 Scott S Albert
Flow of Control
• Computers execute statements sequentially
• Flow of control statements alter this sequential process– Branching statements choose one or another
branch depending on a test– Looping statements repeats one or more
statements multiple times
© 2000 Scott S Albert
C++ flow control statements
Sequence• the default
• C++ automatically executes the next instruction unless you use a branching statement
Branching: Selection
• if
• if-else
• if-else if-else if- … - else
• switch
© 2000 Scott S Albert
If Else Statement
• Branching statement– Format 1
• if ( <boolean expression> ) <statement>• <statement> is only executed if the expression is true
– Format 2• if ( <boolean expression> ) <statement1>
else <statement2>• <statement1> is only executed if the expression is true
<statement2> is only executed if the expression is false
© 2000 Scott S Albert
Boolean Expressions
• Boolean Expression– Expression that can simplifies to true or false– We have already used some Boolean
expressions and if statements in our sample programs
• if (cAnswer == ‘y’) cout<<“thanks”;
• Simple Expressions– <variable> <comparison operator> <variable or constant>
© 2000 Scott S Albert
Comparison Operators
equals = = nT*5/9-32 == 0
not equal ! = sChar != ‘S’
greater than > hours > 40
less than < pay < 0
greater or equal >= radius >= 5.2
less than or equal <= sales <= plan
© 2000 Scott S Albert
Boolean Expressions
• Complex arithmetic expressions can be used• rate*43 / 17 <= base * 8
• Simple expressions can be combined using– And -- &&
• Both expressions must be true for the result to be true
– Or -- || • If either expression is true the result is true
• Each simple expression should be in ( )
© 2000 Scott S Albert
Boolean Variables
• Boolean variables have the value true or false
• Boolean variables can be assigned– A boolean expression– The value true or false– Another boolean variable
• A boolean variable can be substituted for a boolean expression
© 2000 Scott S Albert
Comparing Objects
• Comparison operators only work with basic types (int, float, char etc.)
• To compare classes, like Strings you must– Use a class comparison method if one exists– Use a class method to retrieve the class data
into a basic type
© 2000 Scott S Albert
Grouping statements
• What if you need more than one statement to be executed if an if Boolean expression is true?– Statements are grouped by { }– Statement groups can be nested– All statements in a group are treated as a single
statement. This is called a statement block
© 2000 Scott S Albert
Example
int nHours;float nPay, nGross, nOvertime;
// Read in nHours, nPay, sOT (overtime paid)
if (nHours <= 0)cou<<"Error in hours input value is ”<<nHours;
else {if ( nHours > 40 )
nOvertime = (nHours - 40) * ( Pay * 1.5 );else
nOvertime = 0;}
© 2000 Scott S Albert
Matching Elses
– An else matches up to the nearest preceding if that has not already been spoken for by another else Use blocks to help document your code.
if (coffee==‘y’) if (donuts == ‘y’) cout<<“We have coffee and donuts.”; else cout<<“We have coffee, but not donuts.”;else if (tea==‘y’) cout<<“We have no coffee, but tea.”; else cout<<“We have no coffee, but tea.”;
© 2000 Scott S Albert
Switch Statement
– Multi-branch if statements such as the one used in the previous example are quite common in many programming projects
• Especially in menu systems– Enter 1 for New Messages
– Enter 2 for Greetings
– Enter 3 for Administrative options
– Enter 4 to Send a message
– Enter 5 to end
• Switch is easier to implement than many if statements
© 2000 Scott S Albert
Switch Statement Syntax
– switch ( <integer or char expression> ) { case <integer or char> :
<statement(s)> break; case <integer or char>:
<statement(s)>break;
default:<statement(s)>
}
© 2000 Scott S Albert
Sampleint nMenu;// System.out.println statements listing a menucout<<“Enter choice”;cin>>nMenu;switch( nMenu ) { case 1:
cout<<“Listening to messages”;break;
case 2:cout<<“Change greetings”;break;
case 3:cout<<“Administrative stuff”;break;
case 4:cout<<“Sending message”;break;
case 5:cout<<“Goodbye”;break;
default:cout<<“Sorry we did not understand you”;
}
© 2000 Scott S Albert
Switch notes
• Usually at least one case statement is needed
• The default statement is optional, but highly recommended
• If you omit the break statement, the flow of execution will continue into the next case