There may be a situation, when you need to execute a block of code
several number of times. In general, statements are executed
sequentially: The first statement in a function is executed first,
followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A For loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a For loop statement :
Synatax:
Lets take an Simple Example:
1.Firstly the for loop executes the initialize statement in which the subscript variable will be initialized
with the initial value.
2.After the initialize statement the condition part of the loop will be executed if the condition becomes true then body of the loop will be executed otherwise the loop will be terminated
3.If the loop condition becomes true then body of the loop will be executed. After the execution of the for loop body the control goes to the third part of the loop statement i.e Expression Updation
4.After updating subscript variable control again goes to execute condition statement.
for more understanding the flow of loop plz go here....
Different ways of using For loop :
In order to do certain actions multiple times , we use loop control statements. For loop can be implemented in different verities of using for loop -
1. Single Statement inside For Loop
2. Multiple Statements inside For Loop
3. No Statement inside For Loop
4. Semicolon at the end of For Loop
5. Multiple Initialization Statement inside For Loop
6. Missing Initialization in For Loop
7. Missing Increment /Decrement Statement
8. Infinite For Loop
9. Condition with no Conditional Operator.
Way 1 : Single Statement inside For Loop
2. We have single statement inside for loop body.
3. No need to wrap printf inside opening and closing curly block.
4. Curly Block is Optional.
Way 2 : Multiple Statements inside For Loop
Way 3 : No Statement inside For Loop
2.This verity of for loop is not used generally.
3.At the end of above for loop value of i will be 5
Way 4 : Semicolon at the end of For Loop
2. This is perfectly legal statement in C Programming.
3. This statement is similar to bodyless for loop. (Way 3)
Way 5 : Multiple Initialization Statement inside For
Way 6 : Missing Increment/Decrement Statement
Way 7 : Missing Initialization in For Loop
Way 8 : Infinite For Loop
Programming languages provide various control structures that allow for more complicated execution paths.
A For loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a For loop statement :
for(initial expression; test expression; update expression)
{
body of loop;
}
Lets take an Simple Example:
#include<stdio.h> int main() { int i; for(i=0;i<=5;i++) printf("Hello World"); return 0; }
Output: it will print five times Hello WorldExplanation of For Loop :
1.Firstly the for loop executes the initialize statement in which the subscript variable will be initialized
with the initial value.
2.After the initialize statement the condition part of the loop will be executed if the condition becomes true then body of the loop will be executed otherwise the loop will be terminated
3.If the loop condition becomes true then body of the loop will be executed. After the execution of the for loop body the control goes to the third part of the loop statement i.e Expression Updation
4.After updating subscript variable control again goes to execute condition statement.
Different ways of using For loop :
In order to do certain actions multiple times , we use loop control statements. For loop can be implemented in different verities of using for loop -
1. Single Statement inside For Loop
2. Multiple Statements inside For Loop
3. No Statement inside For Loop
4. Semicolon at the end of For Loop
5. Multiple Initialization Statement inside For Loop
6. Missing Initialization in For Loop
7. Missing Increment /Decrement Statement
8. Infinite For Loop
9. Condition with no Conditional Operator.
Way 1 : Single Statement inside For Loop
for(i=0;i<5;i++) printf("Hello");1. Above code snippet will print Hello word 5 times.
2. We have single statement inside for loop body.
3. No need to wrap printf inside opening and closing curly block.
4. Curly Block is Optional.
Way 2 : Multiple Statements inside For Loop
for(i=0;i<5;i++) { printf("Statement 1"); printf("Statement 2"); printf("Statement 3"); if(condition) { -------- -------- } }If we have block of code that is to be executed multiple times then we can use curly braces to wrap multiple statement in for loop.
Way 3 : No Statement inside For Loop
for(i=0;i<5;i++) { }1.This is bodyless for loop. It is used to increment value of “i”.
2.This verity of for loop is not used generally.
3.At the end of above for loop value of i will be 5
Way 4 : Semicolon at the end of For Loop
for(i=0;i<5;i++);1. Generally beginners thought that , we will get compile error if we write semicolon at the end of for loop.
2. This is perfectly legal statement in C Programming.
3. This statement is similar to bodyless for loop. (Way 3)
Way 5 : Multiple Initialization Statement inside For
for(i=0,j=0;i<5;i++) { statement1; statement2; statement3; }Multiple initialization statements must be seperated by Comma in for loop.
Way 6 : Missing Increment/Decrement Statement
for(i=0;i<5;) { statement1; statement2; statement3; i++; }however we have to explicitly alter the value i in the loop body.
Way 7 : Missing Initialization in For Loop
i = 0; for(;i<5;i++) { statement1; statement2; statement3; }we have to set value of ‘i’ before entering in the loop otherwise it will take garbage value of ‘i’.
Way 8 : Infinite For Loop
i = 0; for(;;) { statement1; statement2; statement3; if(breaking condition) break; i++; }Infinite for loop must have breaking condition in order to break for loop. otherwise it will cause overflow of stack.