Modal Box

Loops in C Language

Suppose block of statements has to be executed repeatedly, then instead of writing those statements multiple times, loops are used.

The looping statements are used to execute a single statement or block of statements repeatedly until the given condition is FALSE. So if  i have to print myname 100 times, then instead of writing printf hundred times, we can use loop with condition that executes 100 times.

Types of the Loops in C :

  1. while statement
  2. do-while statement
  3. for statement

Using while loop the body executes till the condition is true .

Syntax for while loop

while(condition here) 
{
/*statement to be executed for true condition*/
}

Example

Print no. from 1 to 100

 #include<stdio.h>
 #include<conio.h>
int main()
{
   int i=1;
   while(i<=100)
     {
        printf("%d ", i );
        i++;
} }

⇒ The above loop terminate when the value of i exceeds 100, so loop condition becomes false.

If condtion is always true, it means infinite looping is there.

 #include<stdio.h> 
 #include<conio.h>
int main()
{
   int i=1;
   while(i<=100)
     {
        printf("%d ", i );
      }
}

⇒ Here the value of i is not changing, so it is always true and loop becomes infinite loop .

Video/ C Introduction

Watch video in full size