While And Do While Loop In C [With Examples]
While loop and Do While loop are also a type of loop about which we are going to know with the examples in detail.
So first of all let’s understand what is while Loop In C
What is While Loop In C
While loop is a way to repeatedly run a particular statement or block in a program to a particular condition.
While loop is also called pre-tested loop or entry-controlled loop. Because while loop has first condition check as if statement, after that if condition is true then the control enters into the body of the loop (which is covered by the curly bracket {}) and runs the statements inside it
As soon as the control comes to the end of the body of the while loop, the control again goes to the while loop and the condition is checked again. If this time the condition is correct, then the statement of the body of the while loop runs.
This action lasts until the condition of the while loop becomes incorrect. As soon as the condition of the while loop is wrong, the control comes out of the while loop and starts running the statements after the while loop.
While loop is the easiest structure Loop Statement in C language.
While loop is used in C language when we do not know how many times the statements inside the body of the loop have to run.
In the body of the while loop (which is covered by the curly bracket {})
If there is only one statement, then even if it is not covered with a curly bracket, it works.
But if there is more than one statement in the body of the while loop, then it has to be covered with curly bracket {}.
I suggest you should always cover the while loop with the curly bracket even if it contains a single statement. This makes the code easier to read.
Syntax of While Loop
while (test_expression)
{
// statements
update_expression;
}
While loop has two parts -:
- Test expression
- Update Expression
Test Expression -:
In this expression, we check the condition. If the condition is true then we enter inside the body of the while loop and execute the statement inside it, and if the condition is false then we do not enter inside the while loop.
Example -:
include <stdio.h>
#include <conio.h>
int main ()
{
int num = 1; // initializing the variable
while (num<=10) // test_expression
{
printf ("%d \n", num);
num++; // update_expression;
}
return 0;
}
In the example above (num <= 10) there is a test_expression.
Update Expression -:
When we enter inside the body of the while loop, we try to change the condition of the while loop using the update expression. In Update Expression we use increment or increment operator with variable.
Example -:
while (num<=10) // test_expression
{
printf ("%d \n", num);
num++; // update_expression;
}
See the example program we created above, in it, we have written num ++, this is an update expression incrementing the value of num variable.
To understand the while loop more, let us now create a program.
Example Program of While Loop
We are going to create a program in which we will print the values from 1 to 10.
#include <stdio.h>
int main ()
{
int i = 1;
while (i<=10)
{
printf ("%d \n", i);
i++;
}
return 0;
}
In this program we have int i = 1; I did a variable declare named i and stored 1 in it.
Then we put a condition in the parenthesis of the while loop by i<= 10. Since this condition is correct, the control enters the body of the while loop and runs the statement inside it.
i++ in the body of the while loop; Is a statement with the help of which we are increasing the value of the i variable.
Thereby comes a time in which the value of i variable becomes larger than 10 and i<= 10 condition becomes false. The control then exits from the while loop.
The output of this program will be something like
Output -:
1
2
3
4
5
6
7
8
9
10
Now let us know about Do While Loop
What is Do While Loop in C
Do while loop is also like a while loop, But the difference in them is that the condition is checked first in the while loop and the statements inside the while loop are run only when the condition is correct.
While the condition in the do-while loop is true or false, the statement inside it only runs once because in the do-while loop the condition is checked in the end of the block.
So if the condition is true then the block of do while loop is executed again and if the condition is false then the block of do while loop is not executed and the control exits from the do while loop.
Note – the condition is true or False, in the Do While loop, the statement inside it runs once.
Syntax of Do While loop
Do
{
// statements inside the loop
}
While (condition);
Note – After writing While (condition) in Do While loop, it is necessary to apply semicolon in the end, whereas it was not in the while loop.
The Do While loop is also called the post-tested loop or exit controlled loop because in the Do While loop, the condition is checked at the end of the block
Let’s understand the Do While loop by making a program and understand it better.
Example Program of Do While Loop
// C program to illustrate do-while loop
#include <stdio.h>
int main ()
{
int i = 1; // Initialization expression
do {
// loop body
printf ("Hello World \n");
i++; // Update expression
} while (i<=1); // Test expression or do-while loop condition
return 0;
}
Look at this example program above, in that we first created the i variable and stored 2 values in it.
Then we created a body of do-while loop inside which we used the printf () function to print “Hello World”.
After that we have i++; By increasing the value of i, due to which the value of i will increase to 2.
At the end of the body of the do-while loop, we have written the condition i<= 1, which will check if the condition is false because the value of the i variable has increased due to which i<= 1 is not true.
Because of which the condition will be false, the control will come out of the do-while loop.
If you want to try this program by running , then you can try it by clicking on the demo button below.
The output of this program will be something like this.
Output -:
Hello World
Read More -:
- Loop in C
- For Loop in C
- Goto Statement in C
- Break Statement in C
- Switch Statement in C
- Control Statements in C
- Continue statement in C
- Conditional Operator in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, hope that after reading this post today, you will have got all the information related to What is While Loop in C Language and What is Do While Loop in C Language.
Friends, I hope you have found the answer of your question and you will not have to search about What is While Loop in C and What is Do While Loop in C language.
However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about While Loop in C and Do While Loop in C.
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.