Topic :For Loop in C++ Programming Language:



For Loop:

For Loop is a repetition control structure that allows us to efficiently write a loop that needs to execute a specific number of times. The for loop or statement encloses one or more C++ statements that form the body of the loop. These statements in the loop continuously repeat for a specified number of times.



for Loop

Syntax:

The Syntax of the for loop is:



for ( Initialize; condition; incrementation)
{ 
        /* Body of for Loop */
}


Inside the Parentheses () of for statement, semicolons (;) separate three expressions. Initialize, condition, and process:



  • » Initialize: This step is executed first, and only once. this step allows us to declare and initialize any loop control variables. we are not required to put a statement here, as long as semicolon appears.
    » Condition: Next, the condition is evaluated. if it is true, the body of the loop is executed. if it false, the body of the loop does not execute and flow of control jumps to the next statement just after the for-loop (if any).
    » Incrementation: After the body of the for-loop executes, the flow of control jumps back up to the incrementation. this statement allows us to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.



The condition is now evaluated again. if it is true, the loop executes and the process repeats itself (body of the loop, then increment step, and then again condition). after the condition becomes false, the for loop terminates.



Example:

Let's have an short and easy example to clear this concept. in this example we will print first 10 digits. (1-10).



#include <iostream>
 
using namespace std;
main()
{
	int i; //Declare variable i of type int.
 
	/*initialize; Condition; Incrementation*/
	/*note that these three statements must be seperated by semicolon (;) */
	for(i=1; i<=10; i++)
	{
    	    cout<<i<<endl; 
	}
 
        /*endl is used to insert newline at the end of each line:*/
 
	return 0;
}


We can initialize and declare a variable at a same time in for-loop Parentheses; as shown below: but note that there will be no more variable (i.e. "i") outside from the body of for-loop, as it declare only for for-loop.



In for-loop all three statements must be seperated by semicolon (;)

endl is used to insert newline at the end of each line


#include <iostream>
 
using namespace std;
main()
{
	/*initialize; Condition; Incrementation.*/
	/*note that these three statements must be seperated by semicolon (;) */
	for(int i=1; i<=10; i++)
	{
    	  cout<<i<<endl; 
	}
	/*endl is used to insert newline at the end of each line:*/
 
	return 0;
}


Example:

Let's have another useful Example which will show some uses of for-loop: in this example, we will create Math Multiplication Tables. This Program can create a table of any number, and up to any number. this program will ask user, "for what number you need table" you will enter any number for making a table, and next it will ask you to enter the Maximum limit (limit to the end table,). it will start at 1 and will end at MaxLimit. Here we Need three variables having type int.

  • » Number: Select any number to Make table:
    » MaxLimit: select table limit: it will start from 1 and will end at MaxLimit that you selected.
    » Counter: it will used for loop-statement. to show the counter from 1 to Maxlimit.



/*Make table by using for loop: copy and paste this code in your 
compiler to check the program:*/
/*2(Number) X 1(Counter) = 2*/
 
#include<iostream>
using namespace std;
int main()
{
	int Number, counter, MaxLimit;
 
	cout<<"For what Number you Need to Make table ?";
	cin>>Number; //i'll enter 12.
 
	cout<<"Enter Maximum limit for Table:";
	cin>>MaxLimit; //i'll enter 10.
 
	/* initialization; condition;  incrementation */
	/*Loop will start from 1 and end at MaxLimit*/
	for(counter=1;counter<=MaxLimit;counter=counter+1)		
	{
            cout<<Number<<" X "<<counter<<" = "<<Number*counter<<"\n"; 
     	    /*Number Multiple counter= sum*/
	}
 
        return 0;
}


The for-loop most frequently is used as a count-controlled loop or one whose execution is controlled by counting the number of repetitions. Although the initialize portion of a for loop can contain multiple statements, some programmers believe the best style is to use only the loop control variable in the for statement and to insert other statements before the loop begins.







I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement