Chapter 5 CONTROL STRUCTURES II (Repetition)

Download Report

Transcript Chapter 5 CONTROL STRUCTURES II (Repetition)

CHAPTER 5
CONTROL STRUCTURES II
(Repetition)
In this chapter, you will:
 Learn about repetition (looping) control structures
 Explore how to construct and use count-controlled,
sentinel-controlled, flag-controlled, and EOF-controlled
repetition structures
 Examine break and continue statements
 Discover how to form and use nested control
structures
THE while LOOPING (REPETITION) STRUCTURE
The general form of the while statement is
while(expression)
statement
Introduction to Loops
• Execute a block of statement(s) repetitiously
as long as the condition is true (non-zero)
a) counting loop
b) conditional loop
- sentinel loop, flag-controlled loop
• It requires 3 steps to design a loop:
1)Initial the condition before the loop
2)Design the condition (Boolean expression)
for the loop
3)Design the body of the loop.
3a)The action you want to repeat
3b)Update the condition
The while Loop
• A loop is part of a program that repeats.
• A while loop is a “pre test” loop - the expression is tested
before the loop is executed
while (expression)
statement;
while (expression)
{
statement;
statement;
statement;
}
Demonstrate a counter loop
1)initial Num to 1
Star
t
2)Repeat while Num is less or equal to 10
2a)output Num and Num*Num
initialize the
Num to 1
2b)increment Num by 1
End while
F
Num <= 10
T
Output Num
and Num*Num
Increment Num by 1
Stop
// This program displays the numbers 1 through 10 and
// their squares.
#include <iostream>
using namespace std;
void main(void)
{
int Num = 1;
cout << "Number Number Squared\n";
cout << "-------------------------\n";
while (Num <= 10)
{
cout << Num << "\t\t" << (Num * Num) << endl;
Num++;
}
}
1
2
3a
3b
Program Output
Number
Number Squared
------------------------1
1
2
4
3
9
4
16
5
25
6
36
7
49
8
64
9
81
10
100
Case 2: Sentinel Controlled while Loop
cin>>variable;
while(variable != sentinel)
{
.
.
.
cin>> variable;
.
.
.
}
This program demonstrates a loop using
sentinel
1)Initialize Number to 0
Star
t
2)Prompt user to enter an number
3)Repeat while Number is not 99
Initialize Number to 0
enter an number
Prompt user to
enter a number
Number
!=99
F
Stop
End while
T
Enter
number
// This program demonstrates a simple while loop and 99 is the
// sentinel
#include <iostream>
using namespace std;
void main(void)
{
int Number = 0;
cout << "This program will let you enter number after\n";
cout << "number. Enter 99 when you want to quit the ";
cout << "program.\n";
cin >> Number;
1
while (Number != 99)
2
cin >> Number;
3a & 3b
}
Case 3: Flag-Controlled while Loops
• A flag controlled while loop uses a Boolean variable to control the
loop. Suppose found is a Boolean variable. The flag controlled
while loop takes the form:
found = false;
while(!found)
{
.
.
.
if(expression)
found = true;
.
.
.
}
 The value returned by cin can be used to determine if the program
has reached the end of input data or read invalid data.
 Since cin returns a logical value true or false, in a while loop
it can be considered a logical expression.
 An example of an EOF controlled while loop is:
cin>>variable;
while(cin)
{
.
.
.
cin>>variable;
.
.
.
}
The eof Function
Suppose we have the following declaration:
ifstream infile;
Consider the expression:
infile.eof()
• This is a logical (Boolean) expression.
• The value of this expression is true if the program has read past the
end of the input file, infile, otherwise the value of this expression
is false.
Suppose we have the declaration:
ifstream
char ch;
infile;
infile.open("inputDat.dat");
• The following while loop continues to execute as long as the program
has not reached the end of file.
infile.get(ch);
while(!infile.eof())
{
cout<<ch;
infile.get(ch);
}
THE for LOOPING (REPETITION) STRUCTURE
The general form of the for statement is
for(initial statement; loop condition;
update statement)
statement
• The initial statement, loop condition and update
statement (called for loop control statements) that are enclosed
with in the parentheses controls the body (the statement) of the for
statement.
The for Loop
•
Ideal for situations that require a
counter because it has built-in
expressions that initialize and update
variables.
1
2 3b
for (initialization; test; update)
statement;
3a
enter --> 1 2 3a 3b 2 3a 3b
2 ……. ....3a 3b 2 --> exit
for(init;test;update) {
statement;
statement;
statement;
}
for (init1,init2; test; update1,update2)
statement;
init;
for (; test; update)
statement;
The Nested Loop
for(init;test;update) {
statement;
for (initialization;test;update) {
statement;
statement;
}
statement;
}
Example 5-6
The following for loop prints the first 10 positive integers:
for(i = 1; i <= 10; i++)
cout<<i<<" ";
 . The following is a legal for loop:
for(;;)
cout<<"Hello"<<endl;
for(i = 10; i <= 10; i++)
cout<<i<<" ";
for(i = 1; i <= 10; i++);
cout<<i<<" ";
for(i = 1; ; i++)
cout<<i<<" ";
THE do…while LOOPING (REPETITION)
STRUCTURE
The general form of a do...while statement is:
do
statement
while(expression);
Example: Consider the following two loops
(a)
(b)
i = 11;
i = 11;
while(i <= 10)
do
{
{
cout<<i<<" ";
cout<<i<<" ";;
i = i + 5;
i = i + 5;
}
}
while(i <= 10);
In (a), the while loop, produces nothing.
In (b) the do...while loop, outputs the number 11.
BREAK AND CONTINUE STATEMENTS
 A break and continue statement alters the flow of control.
 The break statement, when executed in a switch structure,
provides an immediate exit from the switch structure.
 You can use the break statement in while, for, and
do...while loops.
 When the break statement executes in a repetition structure, it
immediately exits from these structures.
 The break statement is typically used for two purposes:
1. To exit early from a loop
2. To skip the remainder of the switch structure
 After the break statement executes, the program continues to
execute with the first statement after the structure.
 The following while loop is written without using the variable
isNegative:
sum = 0;
cin>>num;
while(cin)
{
if(num < 0)
{
//if number is negative, terminate the loop
cout<<"Negative number found in the data"<<endl;
break;
}
sum = sum + num;
cin>>num;
}
• The continue statement is used in while, for, and do-while
structures.
• When the continue statement is executed in a loop, it skips the
remaining statements in the loop and proceeds with the next iteration of
the loop.
• In a while and do-while structure, the expression (that is, the
loop-continue test) is evaluated immediately after the continue
statement.
• In a for structure, the update statement is executed after the
continue statement, and then the loop condition (that is, the
loop-continue test) executes.
sum = 0;
cin>>num;
while(cin)
{
if(num < 0)
{
cout<<"Negative number found in the data"<<endl;
continue;
}
sum = sum + num;
cin>>num;
}
• The use of a break statement in a loop can eliminate the use of
certain (flag) variables.
sum = 0;
cin>>num;
isNegative = false;
while(cin && !isNegative)
{
if(num < 0)
//if number is negative, terminate the loop
{
cout<<"Negative number found in the data"<<endl;
isNegative = true;
}
else
{
sum = sum + num;
cin>>num;
}
}
• Suppose we want to create the following pattern.
*
**
***
****
*****
• In the first line we want to print one star, in the second line two stars
and so on.
• Since five lines are to be printed, we start with the following for
statement.
for(i = 1; i <= 5 ; i++)
• The value of i in the first iteration is 1, in the second iteration it is 2,
and so on.
• We can use the value of i as the limiting condition in another for
loop nested within this loop to control the number of starts in a line.
for(i = 1; i <= 5 ; i++)
{
for(j = 1; j <= i; j++)
cout<<"*";
cout<<endl;
}
• What pattern does the code produce if we replace the first for
statement with the following?
for(i = 5; i >= 1; i--)