for Loop in C
For loop is a conditional iterative statement which is used to check certain conditions and then repeatedly execute a block of code untill the conditions are satisfied.
Syntax
for(initialization; condition; increment/decrement)
{
//Code
}
Let's understand the syntax
-
The
initialization
statement is executed only once. -
Then, the
condition
is evaluated. If the condition is evaluated tofalse
, thefor
loop is terminated. -
If the condition is evaluated to
true
, statements inside the body of thefor
loop are executed, and theincrement/decrement
statement is updated. -
Again the
condition
is evaluated.
This process goes on until the condition
is false. When the condition is false, the loop terminates.
Let's write a C program to print numbers 1 to 10.
Program
#include <stdio.h>
int main ()
{
int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
Join Our Social Media Connections
0 Comments