MODULE 6.
FOR LOOP, WHILE LOOP, DO WHILE LOOP
DIFINITIONS
In computer programming, loops are used to repeat a block of code.
For Example, let’s say we want to show a message 100 times. Then instead of writing the
print statement 100 time, we can use a loop.
That was just a simple example; we can achieve much more efficiency and
sophistication in our programs by making effective use of loops.
There are 3 types of loops in C++
for loop
while loop
do…while loop
OBJECTIVES
At the end of the lessons, the students should be able to:
1. Demonstrate understanding several fundamentals concepts used throughout the Manual
including the for loop, while loop, do while loop.
2. Able to use for loop, while loop, do while loop in C++ programming.
3. Apply the problem-solving methodology used in this module.
4.4 C++ while loop
The syntax of while loop is:
8
MODULE 6.1
Here,
A while loop evaluates the condition
If the condition evaluates the true, the code inside the while loop is executed.
The condition is evaluated again.
This process continues until the condition is false.
When the condition evaluates to false, the loop terminates.
4.5 Flowchart of for while Loop in C++
Figure 2.
9
MODULE 6.1
Example 5. Printing Number From 1 to 5
Output
Here is how this program works
10
MODULE 6.1
Example 6. Sum of Positive Number Only
Output
In this program, the user is prompted to enter a number, which is stored in the variable number.
In order to store the sum of the numbers, we declare a variable sum and initialize it to the value
of 0.
11
MODULE 6.1
The while loop continues until the user enters a negative number. During each iteration, the
numbers entered by the user is added to the sum variables.
When the user enters a negative number, the loop terminates. Finally, the total sum is displayed.
4.6 C++ do…while loop
The do…while loop is a variant of the while loop with one important difference: the body of
do…while loop is executed once before the condition is checked.
Its syntax is:
Here,
The body of the loop is executed at first, then the condition is evaluated.
If the condition evaluates to true, the body of the loop inside the do statement is executed
again.
The condition is evaluated once again.
If the condition evaluates to true, the body of the loop inside the do statement is executed
again.
This process continues until the condition evaluates to false. Then the loop stops.
12
MODULE 6.1
4.7 Flowchart of do…while Loop in C++
Example 7: Display Number from 1 to 5
13
MODULE 6.1
Output
Here is how this program works
14
MODULE 6.1
Example 8. Sum of Positive Numbers Only
Output
Here, the do…while loop continues until the user enters a negative number.
When the number is negative, the loop terminates; the negative number is not added to the sum
variables.
The body of the do…while loops runs only once if the user enters a negative number.
15
MODULE 6.1
4.8 Infinite while loop
If the condition of a loop is always true, the loop runs for infinite times (until the memory is full).
For example,
Here is an example of an infinite do…while loop.
In the above programs, the condition is always true. Hence, the loop body will run for infinite
times.
4.9 for vs while loop
A for loop is usually used when the number of iterations is known. For example,
Here, we know the for-loop will be executed 5 times.
However, while and do…while loops are usually used when the number of iterations is unknown.
For example,
16
MODULE 6.1
Try this at home…
Problem
Write a program in C++ to convert a hexadecimal number to decimal number. See
example output below.
17