Loops in C
0 3085
In C Programming Language, a Loop is used to execute a block of a statement repeatedly.
Types of Loops in C Programming Language:
- While Loop
- Do While Loop
- For Loop
1 do while loop
The do while loop in C Language checks the condition at the base of the loop. It executes the loop inside the body once at-least for one time.
Syntax:
do {
statement (s);
} while(condition);
Example:
#include <stdio.h>Output:
int main () {
int a = 10;
d {
printf('value of a: %d\n', a);
a = a + 1;
} while( a < 20 );
return 0;
}
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
2 while loop
The while loop in C Programming Language repeatedly executes the statement until the given condition is true.
Syntax:
while (Condition test)
{
// Statements to be executed repeatedly
// Increment (++) or Decrement ( - - ) operation
}
Example:
#include <stdio.h>Output:
int main ()
{
int count = 1;
while (count <= 4)
{
printf("%d ", count);
count++;
}
return 0;
}
1 2 3 4
3 for While Loop
The syntax of for While Loop in C Programing Language:
for (initialization; condition test; increment or decrement)
{
/ / Statements to be executed repeatedly
}
Example of For Loop
#include <stdio.h>Output:
int main()
{
int i;
for (i=1; i<=3; i++)
{
printf("%d\n", i);
}
return 0;
}
1
2
3
Share:
Comments
Waiting for your comments