Professional Documents
Culture Documents
Control Technique
(Repetition)
LECTURE 5
The Objectives
1. To use while, do-while, and for loops to execute
statements repeatedly.
2. To understand the flow of control in loops.
3. To use Boolean expressions to control loops.
4. To write nested loops.
5. To know the similarities and differences of three types of
loops.
6. (Optional) To implement program control with break
and continue.
int main ()
{
double score, sum=0,average;
int studProc;
studProc = 0; //initialization
while (studProc < 10) //condition
{
cout << “Enter your score:”;
cin>>score;
sum += score;
studProc++; //updating
}
average = sum/10;
cout << “The average is : “ << average;
}
Output :
int main ()
{
int count=0;
double score, sum=0,average;
char answer=‘y’; //initialization
Output
int main()
{
char letter;
bool found = false;//initialization
while (!found) //condition
{
cout<<"Enter a letter. Press x to stop : ";
cin >>letter;
if (letter=='x')
{
found = true; //updating
cout <<"\nSTOP\n";
}
}
}
STOP
control variable
……
}
The loop stops when the system detects the end-of-file
signal( by pressing <Ctrl-Z>).
BITG 1113 : LECTURE 5 28
Example of a while loop : EOF-controlled
#include <iostream>
using namespace std;
int main( )
{
int n = 0;
float sum = 0;
cout<<"Enter your numbers:^Z to stop.\n";
while (cin >> n)
sum += n;
cout <<"SUM = "<< sum<<endl;
}
int main()
{
int a; //initialization
do
{
cout << "To stop enter a number between 10 & 20 : ";
cin >> a; //updating
} while (a < 10 || a > 20); //condition
cout <<"END"<<endl;
}
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}
} } while (i <=10);
<=10
while loops do while loops
for (int i = 2;
2 i <=10;
<=10 i+=2)
i+=2
{
cout<<i<<“ “;
for loops
}
Output : 2 4 6 8 10
BITG 1113 : LECTURE 5 49
Recommendations
Use the one that is most intuitive and comfortable for you.
In general, a for loop may be used if the number of
repetitions is known, as, for example, when you need to
print a message 100 times.
A while loop may be used if the number of repetitions is
not known, as in the case of reading the numbers until the
input is 0.
A do-while loop can be used to replace a while loop if
the loop body has to be executed before testing the
continuation condition.
#include <iostream.h>
#include <iomanip>
Output :
Row 1 : 1 2 3 4 5
Row 2 : 1 2 3 4 5
Row 3 : 1 2 3 4 5
continue
transfers to the testing expression in while and
do…while statement
transfers to the updating expression in a for statement.
continue; continue;
… …
} while (limit test)
}
for (initialize;limit test;update)
{
…
continue;
…
}
BITG 1113 : LECTURE 5 53
The break Example
int main ( )
{
int x = 5 ;
while ( x < = 20)
{
if (x == 10 )
break;
cout << x << “ “;
x ++ ;
}
cout << “\n Value of x now is “
<< x << endl;
}
Output :
5 6 7 8 9
Value of x now is 10
int main( )
{
int count = 0;
int n = 0;
float sum = 0;
cout<<"Enter some numbers. Press ^Z to stop.\n";
while (cin >> n)
{
if (n == 0)
continue;
sum += n;
count++;
}
cout <<"Sum/Count = "<<(sum/count)<<endl;
}