Presentation is loading. Please wait.

Presentation is loading. Please wait.

Flow Control Statements

Similar presentations


Presentation on theme: "Flow Control Statements"— Presentation transcript:

1 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.

2 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.

3 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.

4 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

5 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.";

6 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

7 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

8 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 )

9 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

10 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?

11 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

12 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


Download ppt "Flow Control Statements"

Similar presentations


Ads by Google