while loop in C programming language

In this article, we will understand the while loop 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.

#include<stdio.h>
#include<conio.h>
void main()
{
    int i=0;
    clrscr();
    label1:
    i=i+1;
    printf("%d This will be repeated 5 times\n", i);
    if(i!=5)
    goto label1;
    printf("End of the program");
    getch();
}

Output:

1 This will be repeated 5 times
2 This will be repeated 5 times
3 This will be repeated 5 times
4 This will be repeated 5 times
5 This will be repeated 5 times
End of the program

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 in C.

Syntax:

while(condition)
{
	/*block of statement*/
}

Explanation:

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

Here, when while keyword 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 inside the while loop 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.

#include<stdio.h> 
#include<conio.h>
void main()
{
    int i=0;
    clrscr();
    while(i!=5)
    {
        i=i+1;
        printf("%d This will be repeated 5 times\n", i);
    }
    printf("End of the program");
    getch();
}

Output:

1 This will be repeated 5 times
2 This will be repeated 5 times
3 This will be repeated 5 times
4 This will be repeated 5 times
5 This will be repeated 5 times
End of the program

As you can see, the output of 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 in C is much easier than using goto statement as we don’t have worry about positioning of labels.

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

The condition being tested can be relational or logical operators.

You may also like...

23 Responses

  1. sumit says:

    nice,,,

  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.
    Thanks.

  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 πŸ™

    #include
    #include
    void main()
    {
    int choice, no1, no2, result;
    clrscr();
    printf(“\n\n*********************”);
    printf(“\nArithmetic Calculator”);
    printf(“\n*********************”);
    printf(“\n\n1. Addition\n2. Subtraction”);
    printf(“\n3. Multiplication\n4. Division”);
    printf(“\n\nEnter your choice: “);
    scanf(“%d”,&choice);
    switch(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);
    break;
    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);
    break;
    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);
    break;
    case 4:
    printf(“\nEnter the numerator and denominator:”);
    scanf(“%d %d”, &no1, &no2);
    if(no2 == 0){
    printf(“\nError: cannot divide by zero”);
    }
    else{
    result = no1 / no2;
    printf(“\nDivision of %d by %d is %d”, no1, no2, result);
    }
    break;
    }
    getch();

    }

  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:

    clrscr();

    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;

    clrscr();

    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;
    label1:
    i=i+1;
    printf(“%d This will be repeated 5 times\n”, i);
    if(i!=5)
    goto label1;
    else
    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 *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet