1 / 24

CHAPTER 4

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.

kaveri
Télécharger la présentation

CHAPTER 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 4 CONTROLLING PROGRAM FLOW

  2. The if Statement • Allow a program to make decisions. • Example: if (Temp < 5) cout << “Wear a coat today.” << endl; • Syntax if (condition) statement;

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

  4. Examples ch <= ‘Z’; Grade == 12; FruitName == “Banana”; Radius != 0; 2*pi*Radius >= Area;

  5. /*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);}

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

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

  8. if (Temp < 5) cout << “Wear a coat today.” << endl; else cout << “Don’t wear a coat today.”<< endl;

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

  10. #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); }

  11. 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.”;

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

  13. 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!”;

  14. 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!”;

  15. 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!”;

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

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

  18. #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); }

  19. 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; }

  20. #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; }

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

  22. 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;

  23. 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 -=;

  24. 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)

More Related