for Loop

for Loop is entry controlled loop. So, what's the syntax of for Loop?
for ( initialization ; condition ; increment/decrement )
{
 // Codes inside for Loop
}
Initialization is initializing the variable from where it will begin the loop. Let's take, suppose, x is the variable and we are in starting the loop from zero, so need to write x = 0. Second is the condition, we need to specific the condition. Until the condition is true, the loop will be executed, once the condition is break, then loop will get break and next line of code will be executed. The increment/decrement is performanced after the codes inside the loop are executed and then after increment/decrement, the condition is checked.
Let's see an example of Printing 1 to 10 with the help of for Loop.
#include <stdio.h>
 int main()
 {
    int i;
    for(i=1;i<=10;i++)
    {
        printf("%d", i);
    }
    return 0;
 }
Explaination of the program:
We have declared a variable i. Now in for Loop, the i variable is initialized from 1, and then condition is given i<=10 ( i less than or equal to 10 ) and then increment the value of i by 1. Inside for Loop, printing function is there, which prints i. And the programs get exit once the condition is break.
Previous
Next Post »
0 Comment