Loops/Repetition
INTRODUCTION
Within a method, we can alter the flow
of control using either conditionals or
loops.
The loop statements: while, do-while,
and for allow us execute a statement(s)
over and over.
Like
conditional,
loop
is
controlled by a boolean expression that
determines how many times the
statement is executed.
PRE-TEST AND POST-TEST LOOP
PRE-TEST : if the expression is false, none
of the statements in the loop will be
executed. Eg: while and for loop
POST-TEST : the statement within the loop
will be executed at least once before the
expression returns false. Eg: dowhile
loops
while Loop
Syntax of while Loop
while (test expression)
{
statement/s to be executed.
}
How while loop works in C++ Programming?
The while loop
checks
expression is true or not.
whether
the
test
If it is true, code/s inside the body of while loop
is executed,that is, code/s inside the braces { }
are executed.
Then again the test expression is checked
whether test expression is true or not. This
process continues until the test expression
becomes false.
Flowchart of while Loop in C++
Figure1: Flowchart of while statement
Figure2: Flowchart of if statement
The
variable
i (known as the loop control variable) is used in
three ways: it is initialized, tested, and updated.
int i = 0;
// Initialize
while (i < n)
// Test
{
cout << *\n;
i++;
// Update
}
All three things must be coordinated in order for
the loop to work correctly!
Exercise 1
1)
Write a method with a while loop to prints 1 through
n in square brackets.
Example
if n = 6
print [1] [2] [3] [4] [5] [6]
2)
Write a method with a while loop that computes the
sum of first n positive integers:
sum = 1 + 2 + 3 + + n
Examples:
n = 5 sum = 15
n = 19 sum = 190
3) Write a method with a while loop that prints 1
through n, separated by commas.
Examples:
for n = 9 print 1, 2, 3, 4, 5, 6, 7, 8, 9
do...while
The do...while Loop is similar to while loop with
one very important difference.
In while loop, check expression is checked at first
before body of loop but in case of do...while loop,
body of loop is executed first then only test
expression is checked.
That's why the body of do...while loop is executed
at least once.
Syntax:
The syntax of a do...while loop in C++ is:
do
{
statement(s);
}while( condition );
Notice that the conditional expression appears at the
end of the loop, so the statement(s) in the loop
execute once before the condition is tested.
If the condition is true, the flow of control jumps
back up to do, and the statement(s) in the loop
execute again. This process repeats until the given
condition becomes false.
How do...while loop works?
The statement/s inside body of loop is executed at least
once, that is, the statement/s inside braces { } is
executed at least once.
Then the test expression is checked.
If the test expression is true, the body of loop is
executed.
This process continues until the test expression
becomes false.
Since the body of loop is placed before the test
expression in do...while loop, the body of loop is
executed at least once.
Example 1:
#include <iostream>
using namespace std;
int main ()
{
int a = 10;
do
{
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );
return 0;
}
When the above code is compiled and executed, it produces the
following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Example 2:
C++ program to add numbers entered by user until user enters 0.
#include <iostream>
using namespace std;
int main() {
float number, sum = 0.0;
{
do
cout<<"Enter a number: ";
cin>>number;
sum += number;
} while(number != 0.0);
cout<<"Total sum = "<<sum;
return 0;
}
Output
Enter a number: 4.5
Enter a number: 2.34
Enter a number: 5.63
Enter a number: 6.34
Enter a number: 4.53
Enter a number: 5
Enter a number: 0
Total sum = 28.34
Exercise
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter the Number :";
cin>>a;
int counter = 1;
do
{
cout<<"Execute Do While "<<counter<<" time"<<endl;
counter++;
}while (counter <= a);
return 0;
}
Sample Output
Enter the Number :6
Execute Do While 1 time
Execute Do While 2 time
Execute Do While 3 time
Execute Do While 4 time
Execute Do While 5 time
Execute Do While 6 time