chapter 4 controlling program flow. the if statement allow a program to make decisions. example: if...

24
CHAPTER 4 CONTROLLING PROGRAM FLOW

Upload: nathan-alvin-wells

Post on 03-Jan-2016

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

CHAPTER 4

CONTROLLING PROGRAM FLOW

Page 2: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

The if Statement

Allow a program to make decisions. Example:

if (Temp < 5) cout << “Wear a coat

today.” << endl;

Syntaxif (condition) statement;

Page 3: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

Boolean expression

An expression that evaluates to either true or false.

Use relational operatorsOperator Meaning == equal

< less than <= less than or equal to > greater than >= greater than equal to != not equal to

Page 4: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

Examples

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

Page 5: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

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

Page 6: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

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 typeExample: ‘2’ > 3; (page 7.12)

= vs == Misplaced semicolon

Page 7: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

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.

Page 8: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

if (Temp < 5)

cout << “Wear a coat today.” << endl;

else

cout << “Don’t wear a coat today.” << endl;

Page 9: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

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.4Negative radii are illegal.

Page 10: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

#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);

}

Page 11: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

4.5 Nested if StatementsAn 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.”;

Page 12: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

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.

Page 13: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

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

Page 14: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

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

Page 15: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

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

Page 16: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

4.9: Looping – The do-while Statement

iteration Repeat one or more statements during

program execution. Referred to as looping. Form: do

{ statement;

} while (condition);

One or more C++

statements form the body

of the loopA Boolean expression used to determine if

the loop is to be repeated.

Page 17: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

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.

Page 18: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

#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 desiredcout << “Do another circle (Y/N)?”;cin >> Answer;

}while (Answer == ‘Y’);cout << “Thanks for using the Circle Calculator” << endl;Return (0);}

Page 19: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

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

Page 20: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

#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;

}

Page 21: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

4.16 Looping: The for Statement

Used to execute a loop body a fixed number of times.

Form:for (initialization, condition,

increment)statement;

Performed only once

Evaluated before each iteration

Performed after each iteration - counter

Page 22: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

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;

Page 23: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

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

Page 24: CHAPTER 4 CONTROLLING PROGRAM FLOW. The if Statement Allow a program to make decisions.  Example: if (Temp < 5) cout

PROGRAMS

Review 21 and 22Exercises

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)