for loop example

C++ For loop With Example

In this post, we will learn about the C++ for loop and its working with the help of examples.

Loops are used in computer programming to repeat a section of code.

  • Let’s imagine we wish to broadcast a message 100 times. We may then use a loop instead of writing the print statement 100 times.
  • That was only a simple example; loops can help us attain far greater efficiency and sophistication in our applications.

The C++ programming language, in general, has a sequential execution strategy. The compiler executes the first statement before moving on to the next statement in line. When using a conditional statement, the statement is performed based on the condition’s outcome. Furthermore, you may use looping instructions in C++ to repeat the execution of a certain block of code until a certain condition is met. You might want to print numbers from 1 to 10 as an example. Instead of manually creating 10 individual print statements, you can use a loop statement to have the compiler manually increment the variable and report the value after each iteration.

Where we used loops?

  • Loops are used in computer programming to repeat a section of code. If you want to repeat a message 100 times, for example, you can use a loop instead of inputting the same code 100 times.
  • The C++ for loop is used to repeatedly iterate a section of the program. If the number of iterations is fixed, the for loop is suggested.

The 3- Types of looping statements available in C++:

  1. For loop
  2. While loop
  3. Do .. While loop

For Loop

In C++ programming, a for loop can be used to repeat a certain execution for a specific amount of times. This allows us to write a single line of code instead of numerous lines of code. The for loop has the following

It is divided into four sections:

  1. Initialization: When the loop begins, the initial condition is executed once. We can either initialise the variable or use one that has already been initialised. It’s  an optional condition.
  2. Condition: This is the second condition, which is run every time the loop is tested. It keeps running until the condition is false. It must return either true or false as a boolean value. It is an optional condition.
  3. Increment/Decrement: It increases or decreases the value of the variable.It is an optional condition.
  4. Statement: The loop’s statement is executed each time the second condition is false.
for(initialization; condition; increment/decrement){    
//…statement or code to be executed
}  

You can declare and initialize a variable in the initialization stage. The variable can then be evaluated against a condition, and the variable’s value can be incremented or decremented in the last step. The loop will continue to run until the condition is false. The program will then exit the loop and proceed with the remaining commands.

Example 1: Printing Numbers From 1 to 5

#include <iostream>

using namespace std;

int main() {
        for (int i = 1; i <= 5; ++i) {
        cout << i << " ";
    }
    return 0;
}

Output

1 2 3 4 5

IterationVariablei <= 5Action
1sti = 1true1 is printed. i is increased to 2.
2ndi = 2true2 is printed. i is increased to 3.
3rdi = 3true3 is printed. i is increased to 4.
4thi = 4true4 is printed. i is increased to 5.
5thi = 5true5 is printed. i is increased to 6.
6thi = 6falseThe loop is terminated

Example

#include <stdio.h>
int main()
{
	int i=0;
	
	for (i = 1; i <= 5; i++)
	{
		printf( "Developers Dome\n");	
	}

	return 0;
}

Output

Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome

IterationVariablei <= 5Action
1sti = 1trueDevelopers Dome is printed and i is increased to 2.
2ndi = 2trueDevelopers Dome is printed and i is increased to 3.
3rdi = 3trueDevelopers Dome is printed and i is increased to 4.
4thi = 4trueDevelopers Dome is printed and i is increased to 5.
5thi = 5trueDevelopers Dome is printed and i is increased to 6.
6thi = 6falseThe loop is terminated
Developer’s Dome

Nested For loop:

for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
{
statement;
}
}

Pyramid Example using nested loop

#include <iostream>
using namespace std;

int main(){ 
for(int i=1;i&lt;=6;i++){  
for(int j=1;j&lt;=i;j++){  
        cout<<" * ";  
}  
cout<<"\n";// for new line  
}  

Output:

*
**
***
****
*****

Infinite for Loop in C++

#include <iostream>
using namespace std;

int main(){ 
int sum = 0;
        for (int i = 1; i <= 10; --i) {
            cout<<"Developers Dome";
        }
}  

In this case, the test expression, i<= 10, is never false, and Developers Dome is printed until memory runs out.

You may like:

Type Conversion In C++ Programming

Operators In C++ Programming

This Post Has One Comment

Leave a Reply