while loop in C Language

This “while loop c language Tutorial” post provides syntax, flow chart and example programs to understand looping concepts in C language.


 

Loop control statements :

  • Also called iterators.
  • C-supports 3 iterators (while, do-while, for)
  • Iterators are used to execute block of statements repeatedly as long as the condition is true.

While loop :

  • Execute a block of instructions continuously as long as the given condition is true.
  • Loop never terminates if condition always true.

 

#include<stdio.h>
main()
{
    while(1)
    {
        printf(“Hello\n”);
    }
}


#include<stdio.h>
main()
{
    while(1)
    {
        
    }
    printf(“Hello\n”);//unreachable statement
}


#include<stdio.h>
main()
{
    int a=3,b=3;
    while(–a && –b)
    {
        printf(“%d,%d\n”,a,b);
    }
}


#include<stdio.h>
main()
{
    signed char ch = 120;
    while(++ch)
    {
        printf(“%d\t”,ch);
    }
}


#include<stdio.h>
main()
{
    int i=2, j=3;
    while(i+1 ? –i : j++)
    {
        printf(“i : %d\n”,i);
    }
}

Share this