For Loop in C++

For Loop in C++

In C++ programming yet, We have learned about While loop structure and do while loop structure in previous articles. Today we start for loop structure, This loop structure is little bit different from other loops. For loop consist on three mainly term or things. Which are stated as, Assigning variable is the first term. Secondly we need to set a specific place to keep continue this loop. Third is increment and decrements signs for continuously run this loop until the condition will false. The pattern of for loop structure is mention below.

for (Assigning variable; continuance condition; Increment/ Decrements Condition )
Body of Statement;

As we seen above structure of for loop in C consist of several steps, which we already mentioned above. Let’s take a simple variable having a name (test) in above structure. 

for (test=0; test<=100; test++) {     
cout<<test <<"n"; 

Above we first initialize a variable having a name “test” to 0. Secondly we run a condition at specific target (test<=100), that’s means this loop run continuously at a particular place, when the value of variable “test” is equal or greater than 100. Now in third section we will add increment or decrements signs (test++), This means this loop run continuously till the condition true. We can say that increment or decrements in the variable (test)

How For loop execute or works ?

When the control goes to for statement, It set the variable (test) value 0. Then Execution go to next step which will program check the condition (test<=100) if it is true then body of loop will be executed, And display first execute value “0” on-screen. Next program run the third term increment statement (test++) and this procedure increment in “0” and value will become “1”. Again this execution goes to continuance condition and check the value if it is true then display “1” on-screen. Loop execute repeatedly,Until the value of variable (test<=100) greater than or equal to “100” then this continuance condition will (test<=100) false and control come out of this loop.

Flow Chart of For Loop

 For Loop in C

For Loop Example Program

using namespace std;
	// initialize a few variables for this example program 
	int test,number,maximumNUM;
	//Display input strings 
	cout<<"Please enter the number of table: ";
	cin>> number;
	cout<<"Please enter the maximumNUM: ";
	//For Loop terms 
	for(test=1; test<=maximumNUM; test++)

Output screenshot of above given Program:

 For Loop in C

In above example, We take a simple variables and use them in for loop structure. This program will help you to understand the for loop structure. Don’t forget to commenting on your programs because maybe its helps your team member or other one to understand the code. Always try it yourself. Be creative with your mind and skills. Read these suggested articles: While Loop Structure and Do While Loop Structure.  Don’t forget to share your feedbacks with us. 

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments