C++: Explain like I’m five – Part 4: for loop, while loop do-while loop and nested loops
Hi guys, welcome to the 4th part of my series, C++: Explain like I’m five. Whilst programming, you may be stuck in a situation where you need to execute a block of code several times. Has that ever happened to you? Generally, statements are executed sequentially but programming languages like C++ provides different types of control structures through which we can access complicated execution paths. In today's blog, we are gonna discuss those types of control structures!
A loop statement is such a type of control structure that is used to execute a block of statements repeatedly until a certain condition is satisfied. A small and easy example would be displaying numbers from 1 to 100. You can set the value of a variable to 1 and can display it 100 times, incrementing its value by one after every iteration.
There are 3 main types of loops in C++:
- For loop
- While loop
- Do-while loop
There are also loop control statements that change execution from its normal sequence.
C++ has the following loop control statements.
- break statement
- continue statement
- goto statement
We will discuss loop control statements later. First, let's take a deep look at the different types of loops. We will start with for loop.
For loop
Syntax:
for(initialization; condition ; increment/decrement)
{
//statement(s) to be executed
}
- In for loop, the initialization occurs first and only once. Initialization is the part of the loop that only executes once.
- The condition in "for loop" is evaluated on each loop iteration. If the defined condition is true then the statements inside the body of the loop get executed. But when or if the condition returns false, the statements in for loop are not executed. This is the time when the control gets transferred to the next statement in the program after the for loop.
- After every time the body of for loop is executed, the increment/decrement part of the loop executes. This updates the loop counter.
- After the third step, the control jumps to the second step and the condition is then re-evaluated. These steps from 2nd to 4th get repeated until the time the loop condition returns false.
Example:
#include <iostream>
using namespace std;
int main(){
for(int i = 1; i <= 4; i++){
/* This statement will be executed
again and again until the condition
" i <= 4" returns false.
*/
cout<<"The value of i is: "<<i<<endl;
}
return 0;
}
Output:
The value of i is: 1
The value of i is: 2
The value of i is: 3
The value of i is: 4
Infinite for loop
A loop is said to be an infinite loop when it executes repeatedly while never stopping. Generally, this happens by mistake.
Example:
#include <iostream>
using namespace std;
int main(){
for(int i = 1; i >= 1; i++){
cout<<"The value of i is: "<< i <<endl;
}
return 0;
}
This is an example of an infinite loop as we are incrementing the value of i so it would always satisfy the condition i >= 1 and here, the condition would never return false.
while loop
Syntax:
while(condition)
{
//statement(s) to be executed
}
In a while loop, the condition is evaluated first, and if it returns true then the statements inside while loop executes. This happens again and again until the condition returns false. When the condition returns false, the control goes out of the loop. It then jumps to the next statement in the program after the while loop. Use increment/decrement statement inside the loop so that at some time the condition returns false. If the condition does not return false, you will be stuck inside an infinite while loop.
Example:
#include <iostream>
using namespace std;
int main(){
int i = 1;
/* The loop will continue to print
the value of i until the defined condition
i<=4 returns false.
*/
while(i <= 4){
cout<<"The value of i is: "<< i <<endl; i++;
}
}
Output:
The value of i is: 1
The value of i is: 2
The value of i is: 3
The value of i is: 4
Infinite while loop
As discussed above, a while loop that never stops is said to be an infinite while loop. This happens when we define the condition in such a way that it never returns false. Then the loop becomes infinite and repeats itself indefinitely.
Example:
#include <iostream>
using namespace std;
int main(){
int i = 1; while(i <= 4) {
cout<<"The value of i is: "<<i<<endl; i--;
}
}
do-while loop
syntax:
do
{
//statement(s) to be executed
}
while(condition);
Firstly, the statements inside the loop body are executed. Then the condition gets evaluated. If the condition returns true, then the control jumps to the “do” part for further repeated execution. This process occurs repeatedly until the condition returns false. After the condition returns false, control jumps to the next statement in the program after the do-while loop.
Example:
#include <iostream>
using namespace std;
int main(){
int a = 1;
do{
cout<<"Value of a is: "<< a <<endl;
a++;
}
while(a <= 4);
return 0;
}
Output:
Value of a is: 1
Value of a is: 2
Value of a is: 3
Value of a is: 4
Nested Loops
In the last blog, we discussed about nested if/else statements. Well, the good news is that you can also nest loops! It means that there would be a loop inside a loop. Cool, isn't it? You can also do a lot of cool things using nested loops, like printing different shapes. Another fun fact is that there are at least 256 levels of nesting in C++.
Syntax
The syntax for nested for loop is:
for ( initialization; condition; increment/decrement ) {
for ( initialization; condition; increment/decrement ) {
//statement(s);
}
//statement(s);
}
The syntax for nested while loop is:
while(condition) {
while(condition) {
//statement(s);
}
//statement(s);
}
The syntax for nested do-while loop is:
do {
//statement(s);
do {
//statement(s);
} while( condition );
} while( condition );
That's all for today! In the next part, we will discuss loop control statements so stay tuned :)
Do take a look at my repo for C++ codes for beginners here. Considering what we have learned so far, you might want to take a look at codes from 1 to 17.