Flow Control Statements Change the natural flow of a program from statement to statement. Two types: Decision-making: make a decision to either execute or not execute a set of code. Repetition: make a decision to execute a set of code again.
Boolean Expressions A program makes a decision using a Boolean expression that evaluates to either true or false. Boolean expressions are formed with relational and logical operators.
Relational Operators == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to Make sure they understand relational expressions involving chars.
Logical Operators Allow the combination of simple boolean expressions to make complex expressions. && Boolean And || Boolean Or ! Boolean Not And and Or truth tables Examples on board
if ( boolean expression ) statement; double hours, otHours = 0; cout << "Enter number of hours worked: "; cin >> hours; if( hours > 40 ) otHours = hours – 40; cout << "You worked " << otHours << " overtime hours.";
if ( boolean expression ) { statement1; statement2; statement3; etc… } double hours, otHours = 0; cout << "Enter number of hours worked: "; cin >> hours; if( hours > 40 ) { otHours = hours – 40; cout << "You worked " << otHours << " overtime hours." << endl; } HANDOUT – ifDemo.cpp
if( boolean expression ) statement(s) else int years; cout << "Enter your age in whole years: "; cin >> years; if( years >= 65 ) { cout << "You’re qualified for Social Security."; cout << "My, you’re old." << endl; } else cout << "You’re not that old." << endl; cout << "Have a nice day." << endl; HANDOUT – paycalc.cpp
if(action == 'd') if(amount > 0) balance += amount; else cout << "You have to deposit something." << endl; if(balance >= amount) balance -= amount; cout << "Account is OVERDRAWN." << endl; if( x == 7 ) if( y == 10 ) cout << "Both conditions are true." << endl; if( x == 7 && y == 10 )
cout << "Both conditions are true."; else if( x == 7 && y == 10 ) cout << "Both conditions are true."; else cout << "One or both of the conditions are false."; if( x == 7 ) if( y == 10 ) cout << "Second condition is false."; cout << "First condition is false."); HANDOUT - lettergrade.cpp
bool eligible, survivor; int age; . . . eligible = false; if(survivor == false) if(age > 65) eligible = true; else bool eligible, survivor; int age; . . . eligible = false; if(survivor == false) if(age > 65) eligible = true; else The left is what I want The right is what the compiler sees. How to fix it?
bool eligible, survivor; int age; . . . eligible = false; if(survivor == false) { if(age > 65) eligible = true; } else An if in one statement block can't be matched to an else in another statement block. So enclose the offending if in it's own block
switch( integral expression ) { case <literal1>: statement1; break; case <literal2>: statement3; statement4; default: statement5; statement6; } switch is the only flow control statement that doesn't use Boolean expression to make decision. Can't switch on a floating point value, or a string. If there is no default case, and the value of the switch expression doesn't match one of the cases, nothing executes. HANDOUT - calculator.cpp calculator_fixed.cpp payroll.cpp