Control Structures Revision
Control Structures Revision
condition
true false
1 or more
statements
true false
condition
statement statement
set 1 set 2
if (intRate > 0)
{ interest = loanAmt * intRate;
cout << interest;
}
else
cout << "You owe no interest.\n";
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-16
4.4 The if/else if Statement
if (condition 1)
{ statement set 1;
}
else if (condition 2)
{ statement set 2;
}
…
else if (condition n)
{ statement set n;
}
Highest !
&&
Lowest ||
Example:
(2 < 3) || (5 > 6) && (7 > 8)
• switch (gender)
•{
• case 'f': cout << "female";
• break;
• case 'm': cout << "male";
• break;
• default : cout << "invalid gender";
•}
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-40
How the while Loop Works
while (condition)
{ statement(s);
}
condition is evaluated
– if it is true, the statement(s) are executed,
and then condition is evaluated again
– if it is false, the loop is exited
false
condition
true
statement(s)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-42
while Loop Example
int val = 5;
while (val >= 0)
{ cout << val << " ";
val = val - 1;
}
• produces output:
5 4 3 2 1 0
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-43
while Loop is a Pretest Loop
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-44
Exiting the Loop
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-46
while Loop Programming Style
• Loop body statements should be indented
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-47
5.2 Using the while Loop for Input
Validation
Loops are an appropriate structure for
validating user input data
1. Prompt for and read in the data.
2. Use a while loop to test if data is valid.
3. Enter the loop only if data is not valid.
4. Inside the loop, display error message and
prompt the user to re-enter the data.
5. The loop will not be exited until the user
enters valid data.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-48
Input Validation Loop Example
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-49
5.3 The Increment and Decrement
Operators
• Increment – increase value in variable
++ adds one to a variable
val++; is the same as val = val + 1;
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-51
Prefix Mode Example
int x = 1, y = 1;
x = ++y; // y is incremented to 2
// Then 2 is assigned to x
cout << x
<< " " << y; // Displays 2 2
x = --y; // y is decremented to 1
// Then 1 is assigned to x
cout << x
<< " " << y; // Displays 1 1
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-52
Postfix Mode
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-53
Postfix Mode Example
int x = 1, y = 1;
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-54
Increment & Decrement Notes
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-56
Letting the User Control the Loop
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-57
User Controls the Loop Example
int num, limit;
cout << "Table of squares\n";
cout << "How high to go? ";
cin >> limit;
cout << "\n\nnumber square\n";
num = 1;
while (num <= limit)
{ cout << setw(5) << num << setw(6)
<< num*num << endl;
num++;
}
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-58
5.5 The do-while Loop
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-59
do-while Flow of Control
statement(s)
true
condition
false
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-60
do-while Loop Notes
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-61
do-while and Menu-Driven Programs
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-62
Menu-Driven Program Example
do {
// code to display menu
// and perform actions
cout << "Another choice? (Y/N) ";
} while (choice =='Y'||choice =='y');
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-63
5.6 The for Loop
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-64
for Loop Mechanics
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-65
for Loop Flow of Control
initialization
code
update
code
false
test
true
statement(s)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-66
for Loop Example
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-67
for Loop Notes
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-68
for Loop Modifications
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-69
More for Loop Modifications
(These are NOT Recommended)
• Can omit initialization if already done
int sum = 0, num = 1;
for (; num <= 10; num++)
sum += num;
• Can omit update if done in loop
for (sum = 0, num = 1; num <= 10;)
sum += num++;
• Can omit test – may cause an infinite loop
for (sum = 0, num = 1; ; num++)
sum += num;
• Can omit loop body if all work is done in header
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-70
5.7 Deciding Which Loop to Use
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-73
5.9 Breaking Out of a Loop
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-74
The continue Statement
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-75