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.
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:
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;
}
For Loop
1
2
3
4
5
6
7
8
9
10
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.
#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.
/*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;
}
For-Loop:
For what Number you Need to Make table ?12 Enter Maximum limit for Table:10 12 x 1 = 12 12 x 2 = 24 12 x 3 = 36 12 x 4 = 48 12 x 5 = 60 12 x 6 = 72 12 x 7 = 84 12 x 8 = 96 12 x 9 = 108 12 x 10 = 120
“
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.