Professional Documents
Culture Documents
BETR1343 - 05 - Chapter 4b - Control Technique - Repetition
BETR1343 - 05 - Chapter 4b - Control Technique - Repetition
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
while (answer==‘y’|| answer==‘Y’) //condition
{
cout << ”Enter your score:”;
cin>>score;
sum += score;
count++;
cout<<”Do you want to continue? y/Y for Yes:”;
cin>>answer; //updating
}
average = sum/count;
cout << ”The average is : ” << average << endl;
Example of a while loop : User-controlled
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";
}
}
}
Example of a while loop : Flag-controlled
Output:
STOP
EOF-Controlled while Loops
• Is used when the data file is frequently altered, added or deleted
• Example :
while (cin)
{
……cin>> variable; //re-initialize the loop control variable
……
}
• The loop stops when the system detects the end-of-file signal( by pressing
<Ctrl-Z>).
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;
}
Example of a while loop : EOF-controlled
Output:
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;
}
Output :
To stop enter a number between 10 & 20 : 3
To stop enter a number between 10 & 20 : 10
for Looping Structure
for Loop
Trace for Loop
int i:
for (i=0; i<2; i++)
{ declare i
cout << “Welcome to C++”;
}
Trace for Loop
execute initializer
int i:
NOW i is 0
for (i=0; i<2; i++)
{
cout << “Welcome to C++”;
}
Trace for Loop
(i < 2) is true
int i:
since i is 0
for (i=0; i<2; i++)
{
cout << “Welcome to C++”;
}
Trace for Loop
Print
int i:
Welcome to C++
for (i=0; i<2; i++)
{
cout << “Welcome to C++”;
}
Trace for Loop
Execute adjustment statement
int i: NOW i is 1
for (i=0; i<2; i++)
{
cout << “Welcome to C++”;
}
Trace for Loop
(i < 2) is still true
int i:
since i is 1
for (i=0; i<2; i++)
{
cout << “Welcome to C++”;
}
Trace for Loop
Print
int i:
Welcome to C++
for (i=0; i<2; i++)
{
cout << “Welcome to C++”;
}
Trace for Loop
Execute adjustment statement
int i: NOW i is 2
for (i=0; i<2; i++)
{
cout << “Welcome to C++”;
}
Trace for Loop
(i < 2) is false
int i:
since i is 2
for (i=0; i<2; i++)
{
cout << “Welcome to C++”;
}
Trace for Loop
Exit the loop. Execute the next
int i:
statement after the loop
for (i=0; i<2; i++)
{
cout << “Welcome to C++”;
}
Comparing for and while Loops
Comparing for and while Loops
Comparing while, do..while & for loops
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.
Nested for Loop
Other Statement Related to Looping
• break
causes a loop to terminate
terminate only the inner loop in nested loop
• continue
transfers to the testing expression in while and
do…while statement
transfers to the updating expression in a for
statement.
The continue Statement
The break Example
Output