Loops in C

by Yogesh Khanna 22-Aug-18

0 547


In C Programming Language, a Loop is used to execute a block of a statement repeatedly.
Types of Loops in C Programming Language:-

1. While Loop
2. Do While Loop
3. For Loop

do while loop

The do while loop in C Language checks the condition at the base of the loop. It execute the loop inside the body once at-least for one time.

The syntax of Do while loop in C Language is written below:-

do {
statement (s);
} while(condition);
Let's take a look at do while loop example:-

#include <stdio.h>
int main () {
int a = 10;
d {
    printf('value of a: %d\n', a);
    a = a + 1;
    } while( a < 20 );
 return 0;
 }
Output:

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

while loop

The while loop in C Programming Language repeatedly executes the statement until the given condition is true.

Syntax of While Loop in C Programming Language. Read it below:-

while  (Condition test)
{
/ / Statements to be executed repeatedly
/ / Increment (++) or Decrement ( - - ) operation
}
Example of while loop:
 
#include <stdio.h>
int main ()
{
int count = 1;
while (count <= 4)
{
printf("%d ", count);
count++;
}
return 0;
}
Output:

1 2 3 4

for While Loop

The syntax of for While Loop in C Programing Language. Read it below:-

for (initialization; condition test; increment or decrement)
{
/ / Statements to be executed repeatedly
}

Example of For Loop

#include <stdio.h>
int main()
{
int i;
for (i=1; i<=3; i++)
{
printf("%d\n", i);
}
return 0;
}
Output:
1
2
3

Share:

Comments

    No Comments Yet.

Leave a Reply