CS 112 Learning Module in Computer Programming 1
Week
do-while Statement
This lesson introduces the use of do-while statement as another reiteration control statement.
The flow of execution for do-while loop as well as its syntax will be presented. Sample code fragments
will also be presented to demonstrate how do-while loop behaves. Problem sets (Try Me!) will be
given at the end of the lesson to give you time to practice before taking the assessment tasks.
Assessment tasks will be submitted on or before the scheduled date as specified in the Course Guide.
Learning Outcomes:
At the end of the lesson, you are expected to:
• evaluate the output of code fragments having do-while loop statements
• create programs using do-while loop statements
CS 112 Learning Module in Computer Programming 1
Start your lesson here.
The do-while loop is another type of loop statement in C++. It is also used to iterate a block of
statements several times. Specifically, do-while loop is used if the number of iteration is not fixed.
The block of statements is executed at least once since the condition is checked after the block of
statements.
The do-while loop syntax is:
do
{
block_of_statements;
} while (condition);
Where:
do and while – are the keywords used in the loop condition – is where you set the condition
which when satisfied, the loop is executed. Otherwise, the loop is terminated.
block_of_statements – is the body of the loop that is executed once and is executed again
as long as the condition is met.
Notice that in do-while loop, the condition checking is done outside the loop. On the contrary, if you
can recall in the while loop the condition is written at the top of the loop which gives a different
implication to the flow of execution.
The flow of execution of do-while loop is shown below. It starts with the initialization of
variable which is executed only once which is similar to the for-loop. Afterwhich, it executes the block
of statements, and then goes to the condition where it is evaluated whether or not it is true or false.
If the condition is false, the do-while loop is terminated. If the condition is true, it executes the block
of statements again. The increment/decrement is part of the block of statements which when
executed updates the loop counter. After which, it goes back to the condition part where it is re-
evaluated.
CS 112 Learning Module in Computer Programming 1
CONDITION
Take a look at the sample code fragment below.
int c = 0;
do {
cout << c << "\n";
c++;
} while (c < 3);
The initial value of variable c is 0. It goes immediately to the block of statements within the loop
which prints 0. It then increments the value of c, where c becomes 1. It goes to the condition where
c is evaluated. Since 1 is less than 3, it goes again to the block of statements within the while-loop
which prints 1 and increments again the value of c, where c now becomes 2. The condition is then re-
evaluated and since 2 is still less than 3, it goes again to the block of statements within the while-loop
which prints 2 and increments the value of c, where c now becomes 3. The condition is re-evaluated
and since 3 is not less than 3, the whileloop is terminated.
The output is: .
Now, take a look at another sample code fragment below where do-while loop is within
another do-while loop. This is called nested do-while loop.
CS 112 Learning Module in Computer Programming 1
int c = 0;
do {
int r = 0;
do {
cout << r;
r++;
} while (r < 2);
cout << "\n";
c++;
} while (c < 3);
Inner Loop Outer Loop
The initial value of variable c is set to 0. It goes immediately to the body of the outer loop where the
value of the variable r is initialized to 0 and then another do-while loop is found. It goes immediately
to the body of the inner loop where it prints the value of r is which is 0 and increments the value of r
which becomes 1. The condition of the inner loop is evaluated and since r which is 1 is less than 2, it
goes back to the block of statements within the inner loop which prints 1 and increments the value
of r, where r becomes 2. The condition of the inner loop is reevaluated and since r which is 2 is not
less than 2, the inner loop is terminated. It then goes to the next statement just below the inner loop
where it prints a new line and then increments the value of c. The value of c now becomes 1. The
same process will follow until the condition in the outer loop gets false.
The output is: .
This time, try the C++ program below.
CS 112 Learning Module in Computer Programming 1
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int c =
7 1; do {
8 int r =
9 1; do
10 {
11 cout << "&";
12 r++;
13 } while (r < 6);
14 cout << "\n";
15 c++;
16 } while (c < 6);
17 }
18
The output of the above program is:
Thinking Box:
What will be the output of the sample program if line 16 is removed?
Practice Exercises
A. Evaluate the output of the given code fragments.
1. >
CS 112 Learning Module in Computer Programming 1
int c = 1;
do {
int r = 1;
do {
cout << "&"
<< "\n"; r++;
} while (r < 3);
cout << "\n";
c++;
} while (c < 3);
2. > Assuming that n is 3:
int c, n, y;
cout << "Enter a positive integer: ";
cin >> n; y = n * 10; c = n; do
{
cout << c << "\n";
c += n;
} while (c <= y);
B. Provide the correct C++ program of each of the items below using do-while loop statement
given the requirements and corresponding outputs.
1. Display the input name for nth times:
2. Display a staircase using dollar sign:
CS 112 Learning Module in Computer Programming 1
Recommended Learning Materials and Resources
To supplement the lesson in this module, you may visit the following links:
o [Link] o
[Link] o
[Link] o
[Link]