while Statement in C Programming Language

In this article, we will understand the while loop statement in C programming language.

Let us suppose you want to execute a block of statement 5 times. There is one approach using goto statement as shown below.


The above mentioned approach solves our purpose. But it is very tedious job to take care of labels. It also decreases the readability of the program. Hence there is another approach to perform the above task very effectively. We will use while loop.



while is the keyword which is known to the compiler.
Condition can be any expression

Here, when while is encountered, condition is checked first by the compiler. If the condition is satisfied then the block of statement inside the while loop is executed. After the execution of block of statement, again condition is check. If the conditional expression returns true, the block of statement is executed again. This process is followed till the conditional expression returns false value.

Let us write the above program using while loop in C.


As you can see, the out of the above program is same as the one with goto statement in it.
We can also see that the readability of the program increases. Using while loop is much easier than using goto statement as we don’t have worry about positioning of labels.

Thus, while loop would keep on getting executed till the condition being test remains true. When the condition becomes false then the control goes to the first statement immediately after the boy of while loop.

The condition being tested can be relational or logical operators.

You may also like...

20 Responses

  1. sumit says:


  2. Anonymous says:

    in above program
    giving i=0, means the execution goes starting from i=o, then i=1 then upto i=4 then program stops

  3. LearnCOnline says:

    Yes.. you are right…. execution starts from 1=0 till i=4…

  4. Aankhi says:

    It might have been explained later, but what does getch() do ?

  5. LearnCOnline says:

    getch() waits for the user to input a character and displays the output till the user enters a character.As soon as the user enters a character it transfers the control back to the main function, without displaying what character was entered.
    Hope this clarifies.

  6. supreet says:

    nice ……

  7. Anonymous says:

    now.. if i want end the program by only pressing ‘0’ (zero)… how do i do it using WHILE loop(not other loops)…

    ….can u do it for this program and explain 🙁

    void main()
    int choice, no1, no2, result;
    printf(“\nArithmetic Calculator”);
    printf(“\n\n1. Addition\n2. Subtraction”);
    printf(“\n3. Multiplication\n4. Division”);
    printf(“\n\nEnter your choice: “);
    case 1:
    printf(“\nEnter the two number:”);
    scanf(“%d %d”, &no1, &no2);
    result = no1 + no2;
    printf(“\nThe Addition of %d and %d is %d”, no1, no2, result);
    case 2:
    printf(“\nEnter the two number:”);
    scanf(“%d %d”, &no1, &no2);
    result = no1 – no2;
    printf(“\nThe Subtaction of %d and %d is %d”, no1, no2, result);
    case 3:
    printf(“\nEnter the two number:”);
    scanf(“%d %d”, &no1, &no2);
    result = no1 * no2;
    printf(“\nThe Multiplication of %d and %d is %d”, no1, no2, result);
    case 4:
    printf(“\nEnter the numerator and denominator:”);
    scanf(“%d %d”, &no1, &no2);
    if(no2 == 0){
    printf(“\nError: cannot divide by zero”);
    result = no1 / no2;
    printf(“\nDivision of %d by %d is %d”, no1, no2, result);


  8. muthukumaran says:

    great work………its very helpful.
    thanks a lot…..

  9. deepika says:

    what is the output if the goto statement is given after int statement?

  10. Anonymous says:

    very helpful site. thanx a lot…!!!!!!

  11. LearnCOnline says:

    Thanks a lot everyone…

  12. Anonymous says:

    I Tried While loop but it works fine for int value criteria. I wanted to use char type of variable which holds value from scanf function. Even If value is correct while block fails. Can you please help me??

  13. Michael Cook says:

    Loving this tutorial 🙂
    The above works fine for me except for one line:


    It gives the error “undefined reference to ‘clrscr'”

    I am using the Code:Blocks GNU GCC Compiler. I gather it has something to do with conio.h, but really not understanding what the issue is.

  14. teja says:

    what is the use of clscr();
    could you exlain it….

  15. teja says:

    waht is use of getch();

  16. Anonymous says:

    found this one a site:

    clrscr (); and getch();
    clrscr(); and getch(); requires the header file conio.h i.e #include

    clrscr(); :- This is used for clearing the output screen i.e console

    suppose you run a program, alter it and run it again you may find that the previous output is still stuck there itself, at this time clrscr(); would clean the previous screen.

    One more thing to remember always use clrscr(); after the declaration like

    int a,b,c;

    float total;


    getch(); :- getch is used to hold the screen in simple language, if u don’t write this the screen

    will just flash and go away….

  17. Anonymous says:

    int i=0;
    printf(“%d This will be repeated 5 times\n”, i);
    goto label1;
    printf(“End of the program\n”);

  18. Marie Curie says:

    first thank you for this tutorial. then, without clrscr() and getch() my program does work. ( i use C99). So what do we need to put clscr() and getch() ?

  19. Anonymous says:

    Superb site thanx alot

  20. Ranveer says:

    why don’t u type if(i<5) instead of i!=5 .I was thinking if "i" skips "5"(as u wrote "i" is not equal to 5) nd then goes further till 6 7 8 9… nd so on… sry if thatz a silly que….

Leave a Reply

Your email address will not be published. Required fields are marked *