C++ For Loop Control Structure

In this Tutorial we will understand and learn the working of For Loop in C++ programming language.

Loops are used in programming to repeat a specific block until some end condition is met.
For loop is a repetitive and iterative looping control structure which executes a block of code for a certain number of times. It is specifically used when we already know the number of iterations to be performed or the number of iterations is a numeric value (and not any other condition like in the While loop).

Syntax of a For Loop in C++
for ( init; condition; increment ) {
   statement(s);
}

 

for loop flow chart diagram

Flow chart diagram of for loop in C++

Working of For Loop
  • The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables.
  • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
  • After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables.
  • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
For Loop sample program in C++

Run Online

#include<iostream>
using namespace std;
int main()
{	
	int x=5;
	for(int i=1;i<=10;i++)
	{
		// printing 5s multiplication table
		cout<<"5 x "<<i<<" = "<<(x*i)<<endl;
	}
	
	return 0;
}
Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *