In this post, while Loop in C Programming, will learn how to use while loop in C programming with examples.
As I told you in for loop tutorial that if you understand for loop well then you will not have much problem in understanding while loop.
Because while loop is also used like for loop to repeat some statements in your programs more than once with the same or some changes.
Now the question arises that when the while loop does the same thing as the for loop, then the while loop is needed.
If you already know about the start (initialization), stop (condition), and step (Update) to run the loop then you should use the for loop.
But while programming, many such situations come in front of us when we do not already have information about the start, step, and both to run the loop, then we use the while loop.
You will understand the difference between for loop and a while loop better when you make programs of both these types of loops.
You can also read: For Loop In C Programming
Contents
While Loop in C Programming (syntax):
while(Condition)
{
Statements;
-----
-----
-----
-----
Update;
}
While Loop in C Programming (Flow chart):
While Loop in C Programming
While loop execution process:
The while loop begins with the condition written in the parenthesis ( ) of the while ie this condition is checked before entering the body { } of the while.
If this condition becomes true then the control of the program goes to the body of the while key and the statements present in it are run.
When all the statements inside the while run, then again the control of the program goes to the while condition and the condition is checked again.
After the condition is true, the statements are run again and this repetition occurs until the condition of the while loop becomes false.
As soon as the condition of the while loop becomes false, the control of the program goes outside the while loop i.e. on the subsequent statements.
Special Attention:Â If an update (increment or decrement or any other update method) is not given anywhere in the syntax of the while loop, then how will the condition be false?
Right now because we are making small programs only for learning purposes, so we will apply the logic of updating inside the while loop in such a way that after some fixed steps the condition of while becomes false.
While loop example program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while(i<=10)
{
printf("%d\n",i);
i++;
}
getch();
}
Output:
1
2
3
4
5
6
7
8
9
10
Explanation:
Step 1: First we initialize the loop counter variable i to value 1.
Step 2: Now we check this condition whether the value of variable i is less than or equal to 10. Now because the value of i is 1 so our condition (1<=10) will become true.Â
Step 3: As soon as the condition is true, the flow of the program will go to the body of the loop and with the help of printf function the value of variable i will be printed.
Immediately after that, we incremented the value of variable i by 1 number. The value of variable i was 1 earlier which will now increment to 2.Â
After this again the flow of the program will go to step 2 and check the condition of whether the value of variable i is less than or equal to10.Â
Now because the value of i is 2 so our condition (2<=10) will again become true and again the flow of our program will go to step 3 and then to step 2 .Â
This repetition will continue until the condition does not become false at step 2 and this will happen when the value of i is incremented by 10 to 11 and the condition (11<=10) becomes false at step 2 .
Conclusion:
I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at pr***********@gm***.com .