Loop in C++
By
Abdullah Khan Shahani
Green International University
Lecture Outline
For loop
While loop Do
While loop
Loop
• A loop can be used to tell a program to execute statements
repeatedly
• Suppose that you need to display a string (e.g., "Welcome to
C++!") 100 times.
• So how to solve this
problem???
While Loop
• C++ provides a powerful construct called a loop that controls how many
times an operation or a sequence of operations is performed in succession.
Using a loop statement, you simply tell the computer to display a string
100 times without having to code the print statement 100 times, as
follows:
int count = 0;
while (count < 100)
{
cout << "Welcome to
C++!\n"; count++;
}
• Note: The braces enclosing the loop body can be omitted only if the
loop body contains one statement or none.
While Loop
• The variable count is initially 0. The loop checks whether (count <
100) is true. If so, it executes the loop body to display the message
Welcome to C++! and increments count by 1. It repeatedly executes
the loop body until (count < 100) becomes false (i.e., when count
reaches 100). At this point, the loop terminates and the next
statement after the loop statement is executed
• Loops are constructs that control repeated executions of a block of
statements. The concept of looping is fundamental to programming.
C++ provides three types of loop statements: while loops, do-while
loops, and for loops.
Flow Chart for while Loop
While loop
• If i < 10 is true, the program adds i to sum. Variable i is initially set
to 1, then is incremented
• to 2, 3, and up to 10. When i is 10, i < 10 is false, so the loop
exits. Therefore,
• the sum is 1 + 2 + 3 + ... + 9 = 45.
Identify the problem?
Controlling a Loop with a Sentinel
Value
• Another common technique for controlling a loop is to designate a
special value when reading and processing a set of values. This special
input value, known as a sentinel value, signifies the end of the input.
A loop that uses a sentinel value to control its execution is called a
sentinel-controlled loop.
• Run
Task 1:
• Write a c++ code using while loop which print values from 1 to 5
using while loop
• Write a c++ code using while loop which print table of 2 using
while loop
• Write a c++ code which print sum of ist five natural
numbers using while loop
• Write a code which reverse a number entered by user and
print original number and reverse number.
For loop
• A for loop has a concise syntax for writing loops.
• A for loop can be used to simplify the above
loop:
for (i = initialValue; i < endValue; i++)
{
// Loop body
...
}
• The flowchart of the for loop is shown in Figure
Flow chart for loop
1
A for loop performs an initial action once, then repeatedly executes the statements in the loop
body, and performs an action after an iteration when the loop-continuation-condition evaluates
to true.
For Loop
• The loop control variable can be declared and initialized in
the for loop. Here is an example:
for (int i = 0; i < 100; i++)
{
cout << "Welcome to C++!\n";
}
• If there is only one statement in the loop body, as in this example,
the braces can be omitted as shown below:
for (int i = 0; i < 100; i++)
cout << "Welcome to C++!\n";
Question: What are the three parts of a for loop control?
The do-while Loop
• A do-while loop is the same as a while loop except that it executes
the loop body first and then checks the loop continuation condition.
• The do-while loop is a variation of the while loop. Its syntax
is as follows:
do
{
// Loop body;
Statement(s);
}
while (loop-continuation-condition);
Do while loop
• Flow
Chart
The do-while loop executes the loop body first, then checks the loop-continuation-condition
to determine whether to continue or terminate the loop.
Do while loop
• Run this program and differentiate between while and do while
loop
Important: Which Loop to Use?
• You can use a for loop, a while loop, or a do-while loop, whichever
is convenient.
• In general, a for loop may be used if the number of repetitions is
known in advance, as, for example, when you need to display
message 100 times.
• A while loop may be used if the number of repetitions is not fixed,
as in the case of reading the numbers until the input is 0.
Task 2:
• Write a c++ code using for loop which print values from 1 to 5
using while loop
• Write a c++ code using for loop which print table of 2 using while
loop
• Write a c++ code which print first sum of ist five natural
numbers using for loop
• Write a code which reverse a number entered by user and
print original number and reverse number.
Next
Lecture
Nested
loop