for(; ;) Loop control statement in C++ language
for(; ;) Loop control statement is used when we want to execute a block of instructions, the number of times. In C++ Programming, for(; ;) loop is a control statement which allows code to be repeatedly executed number of times. It contains three different parts given below:
1. Initialization part: This part is executed first and this is executed only once when we are entered into the loop statement. This step is used to declare and initialize any loop control variables.
2. Conditional part: This is the second part after initialization if this condition is true, then the body part of the for(; ;) loop is executed otherwise the flow of control goes outside the for the loop.
3. Increment/Decrements part: After completion of Initialization and Condition steps loop body code is executed and then Increment or Decrements steps is executed. This statement allows updating any loop control variables.
Syntax of for(; ;) loop statement in C++ language:
for ( initialization; condition; increment/decrement )
{
statement-1;
statement-2;
..
..
statement-n;
}
Program of for(; ;) loop control statement:
#include<iostream>
int main()
{
int i;
for(i=1;i<5;i++)
{
cout<<i<<endl;
}
return 0;
}
Program output is tested on www.jdoodle.com
output:
1
2
3
4
Important Points to be remembered:
1. In for(; ;) loop is repeated in anti-clockwise direction.
2. Its execution process is faster than while loop.
3. Everything is optional in it, but mandatory to place 2 semicolons (; ;) in for loop.
4. In for loop if condition part is not given then it will repeat infinite times.
Thanks
Mukesh Rajput
Post A Comment:
0 comments: