Professional Documents
Culture Documents
Lesson 6C - Handling Repeition - Do While Loop
Lesson 6C - Handling Repeition - Do While Loop
Lesson 6C - Handling Repeition - Do While Loop
As discussed in the last tutorial about while loop, a loop is used for repeating a block of statements until
the given loop condition returns false. In this module , we will use do-while loop. do-while loop is similar
to while loop, however there is a difference between them: In while loop, condition is evaluated first and
then the statements inside loop body gets executed, on the other hand in do-while loop, statements inside
do-while gets executed first and then the condition is evaluated.
Do while loop is an exit controlled loop, meaning the test condition is verified after the execution
of the loop, at the end of the body of the loop. Hence, the body executes at least once, regardless of the
result of the test condition, whether it is true or false. This happens to be the foremost difference in
between while loop and do while. In while loop, the condition is tested beforehand, whereas in do while
loop the condition is verified at the finish of body of the loop.
DISCUSSION
do-while loop
First, the statements inside loop execute and then the condition gets evaluated, if the condition returns
true then the control jumps to the “do” for further repeated execution of it, this happens repeatedly until
the condition returns false. Once condition returns false control jumps to the next statement in the
program after do-while.
Example No. 1: This program will print the number of iteration and Hello World 3 times
#include <iostream>
using namespace std;
int main(){
Example explained:
int num=1;
(int num = 1) sets a variable before the loop starts, 1 is
do { initially stored to num.
cout <<num <<" Hello World! " <<endl;
num++; The blocks to be executed before the condition
}
while(num<=3); (num++) the iteration increases to 1 each time the code
return 0; block in the loop has been executed.
}
Defines the condition for the loop to run. If the condition is
true, the loop will to back to do statement, if it is false, the loop
Output: will end.
1 Hello World!
2 Hello World!
3 Hello World!
Example No. 2: This program will ask the user’s name and the number of times his/her name
will be displayed.
string name; Declare and initialize the variable that indicates the
int num=0; numbers of times the name will be displayed
int i=1;
Declare and initialize the iteration variable
cout <<" What is your name? ";
cin>> name;
cout <<"\n How many times you would like to print your name ?" ;
cin>> num;
while(i<=num); Defines the condition for the loop to run. If the condition is
return 0; true, the loop will to back to do statement, if it is false, the
loop will end.
Sample Output:
C++ Exercises: Test yourself : Complete the program codes by filling-up the boxes.
int i = 1;
{
cout << i << "\n";
;
}
(i < 11);
int i = 100;
do {
cout << i << "\n";
;
}
while ;
DEMONSTATE
Performance Task. Apply what you have learned from the previous activities by creating the
program needed on the following situations.
Program No 1. Create a c++ program using d0-while loop statement that will display “ LOADING…...”once
the user entered the correct password.
Example output:
Program No 2. Create a c++ program using do-while statement that will display the following output:
Enter 2 numbers…
First number : 20
Second number : 10
20 x 10 = 200
Try Again? Enter YES if you want to try again and NO if you want to exit.
Procedure:
1. Encode your program on C++ app. Build and run the program.
2. Screenshot your program and output then submit to your subject teacher.