240 likes | 344 Views
CHAPTER 4. CONTROLLING PROGRAM FLOW. The if Statement. Allow a program to make decisions. Example: if (Temp < 5) cout << “Wear a coat today.” << endl; Syntax if (condition) statement ;. Boolean expression. An expression that evaluates to either true or false.
E N D
CHAPTER 4 CONTROLLING PROGRAM FLOW
The if Statement • Allow a program to make decisions. • Example: if (Temp < 5) cout << “Wear a coat today.” << endl; • Syntax if (condition) statement;
Boolean expression • An expression that evaluates to either true or false. • Use relational operators Operator Meaning == equal < less than <= less than or equal to > greater than >= greater than equal to != not equal to
Examples ch <= ‘Z’; Grade == 12; FruitName == “Banana”; Radius != 0; 2*pi*Radius >= Area;
/*Temperature program */#include <iostream.h>int main( ){ double Temp; cout << “Enter today’s temperature(Celsius): “; cin >> Temp; if (Temp < 5) cout << “Wear a coat today.” << endl; cout << “Have a gret day!” << endl; return (0);}
4.2 if Pitfalls • Never make equality comparisons (== or !=) with values that may have fractional parts. if (480 == 4.8 * 100) cout << “These are equal!”; • Compare values of only the same type Example: ‘2’ > 3; (page 7.12) • = vs == • Misplaced semicolon
4. 3 The if-else Statement • Can include an else clause when condition is false if (condition) statement; else statement; • Indention is good programming style.
if (Temp < 5) cout << “Wear a coat today.” << endl; else cout << “Don’t wear a coat today.”<< endl;
Modify the Circle Area program to display an error message when the value entered for Radius is negative. If Radius is a positive value or zero, only the resulting area in a message should be displayed. The error message should look similar to: Radius entered was -3.4 Negative radii are illegal.
#include <iostream.h> int main( ) { double Radius; cout << “Enter a radius: “; cin >> Radius; if (Radius < 0) { cout << “Radius entered was “ << Radius; cout << “Negative radii are illegal.” << endl; } else { cout << “Radius entered was “ << Radius; cout << “Area = “ << (3.14 * Radius * Radius) << endl; } return (0); }
4.5 Nested if Statements An if statement that contains another if statement if (Votes1 == Votes2) cout << “It was a tie!” << endl; else if (Votes1 > Votes 2) cout << Candidate1 << “ is the winner”; else cout << Candidate2 << “ is the winner.”;
4.6 The else-if Ladder Used to decide among three, four, or more actions. if (temp <= 0) cout << “Freezing”; else if (temp < 13) cout << “Cool”; else if (temp < 41) cout << “Hot”; else cout << “Very hot”; Does not contain an if statement. It is executed of all other conditions are false. DEFAULT.
4.8 Logical Operators • “and” represented by && (true) && (true) evaluates to true (true) && (false) evaluates to false (false) && (true) evaluates to false (false) && (false) evaluates to false if ((Temp < 5) && (Wind > 24)) cout << “Wear a coat today!”;
4.8 Logical Operators • “or” represented by || (true) || (true) evaluates to true (true) || (false) evaluates to true (false) || (true) evaluates to true (false) || (false) evaluates to false if ((Temp < 5) || (Wind > 24)) cout << “Wear a coat today!”;
4.8 Logical Operators • “not” represented by ! !(true) evaluates to false !(false) evaluates to true if (!(Temp < 5)) cout << “DON’T wear a coat today!”;
4.9: Looping – The do-while Statement • iteration • Repeat one or more statements during program execution. • Referred to as looping. • Form: One or more C++ statements form the body of the loop • do • { • statement; • } while (condition); A Boolean expression used to determine if the loop is to be repeated.
4.9 Continued • The do-while loop is executed at least once because the condition is not evaluated until AFTER the first iteration of the loop. • If the condition is true, statement is executed again and then the condition reevaluated. • Looping process is repeated until the condition is evaluated and found to be false.
#include <iostream.> int main() { char Answer; cout << “Circle Area Calculator” << endl; do { const double PI = 3.14159; double Radius; cout << “Enter the radius: “; cin >> Radius; cout << “Area is “ << (PI * Radius * Radius)<< endl; // Ask if another calculation is desired cout << “Do another circle (Y/N)?”; cin >> Answer; }while (Answer == ‘Y’); cout << “Thanks for using the Circle Calculator” << endl; Return (0); }
4.11 Looping: The while Statement • Evaluates its condition BEFORE each iteration • May execute zero or more times. • Syntax (form) of a while loop body: while (condition) { statement; }
#include <iostream.h> int main() { int number; cout << “Enter a positive number: “; cin >> number; while (number < 0) { cout << “Number must be positive\n”; cout << “Please re-enter: “; cin >> number; }
4.16 Looping: The for Statement • Used to execute a loop body a fixed number of times. • Form: for (initialization, condition, increment) statement; Evaluated before each iteration Performed after each iteration - counter Performed only once
4.16 Looping: The for Statement Example: The following loop display the numbers 1 through 10. for (int i = 1; i <= 10; i++) cout << I << endl;
4.17 – and -= • Decrement (decrease) Total--; • Special form of assignment operator indicates the value after the operator is to be subtracted from the value of the variable on the left of the operator. Total = Total – Value; Total -=;
PROGRAMS • Review 21 and 22 • Exercises • 14, p. 4-32 • 15, p. 4-32 • 16a, p. 4-33 • 18a, b, c, p. 4-33/34 • 21 • Quiz on Friday (for statements)