Iteration or loop in computer programming is a process wherein a set of instructions or structures are repeated in a sequence a specified number of times until a condition is true. When the set of instructions is executed again, it is called an iteration. A loop statement allows us to execute a statement or a group of statements multiple times.
C++ provides the following types of loops to handle looping requirements:
for loop
while loop
do-while loop
A for loop is a repetition or iteration control structure that repeats a statement or block of statements for a specified number of times. The for-loop statement includes the initialization of the counter, the condition, and the increment. The for loop is commonly used when the number of iterations is known.
Syntax:
The general syntax of for loop is:
{
statement(s);
}
For Loop Example:
#include<iostream>
using namespace std;
int main ()
{
int i;
for (i = 1; i <= 10; i++)
{
cout << i << ” “;
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
The while loop allows the programmer to specify that an action is to be repeated while some conditions remain true. It is used when the exact number of loop repetitions before the loop execution begins are not known.
Syntax:
The general syntax of a while loop is:
while(condition)
{
statement(s);
}
while Loop Example
#include<iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 10)
{
// The content inside the loop is missing from the extraction
}
}
cout << i << ” “;
i++;
}
return 0;
}
A do-while loop is similar to a while loop, except that a do-while loop is guaranteed to execute at least one time, even if the condition fails for the first time. Unlike for and while loops, which test the loop condition at the start of the loop, the do while loop checks its condition at the end of the loop.
Syntax
The general syntax of a do-while loop is:
do
{
statement(s);
}
while(condition);
do while Loop Example
#include<iostream>
using namespace std;
int main()
{
int i = 1;
do
{
cout << i << ” “;
i++;
}
while (i <= 10);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
When one for, while or do while loop is nested within another loop, they are said to be nested. The inside loop is completely repeated for each repetition of the outside loop.
Nested Loop Example
#include<iostream>
using namespace std;
int main()
{
int row, column;
for (row = 1; row <= 5; row++)
{
for (column = 1; column <= 3; column++)
{
cout << “*”;
}
// The content after this line is missing from the extraction
}
// There may be additional content here that is not included in the extraction.
}
cout << “\n”;
}
return 0;
}
0 of 8 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 8 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
What is the primary purpose of a for loop in C++?
Which loop in C++ is guaranteed to execute at least once, even if the condition fails for the first time?
What is the primary difference between a while loop and a do-while loop in C++?
which loop is commonly used in C++ when the exact number of iterations is known?
In C++, what type of loop is suitable to use when the number of iterations is not known beforehand?
What is the syntax of a for loop in C++?
What happens if the condition of a do-while loop in C++ fails initially?
Which type of loop in C++ allows for nesting within another loop?