While and do...while loop in C
While and do…while Loop in C
Hello! I hope you all are
doing well, in today’s blog we will learn the while and do…while loop in C programming
with help of simple and suitable example.
In programming , loops are used
to repeat a task or block of code until a specified condition is met.
There are three type of loops:-
1.for loop
2.while loop
3.do…while loop
In the previous blog we
learned about for loop.in this blog we will study while and do while loop.
While loop:-
The syntax for while loop is as
follow:-
While (test expression)
{
//statements inside the body of the loop……….
}
Working of while loop?
1.The while loop evaluates the
test expression inside the parenthesis ()
2.If the test expression is
true, statements inside the body of while loop are executed. Then, the test
expression is evaluated again.
3.The process goes on until
the test expression is evaluated to false.
4.If the test expression is
false the loop terminates.
Example of while loop:-
1.#include<stdio.h>
main()
{
int n;
printf("enter the value of n:");
scanf("%d",&n);
while(n==9)
{
printf("hello");
}
return 0;
}
2. // Print numbers from 1 to 5
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d\n",
i);
++i;
}
return 0;
}
1.Here, we have initialized i to 1.
2.When i is 1, the test expression i <= 5 is true. Hence, the body of
the while loop is executed. This prints 1 on the screen and the value of i is
increased to 2.
3.Now, i is 2, the test expression i <= 5 is again true. The body of
the while loop is executed again. This prints 2 on the screen and the value of
i is increased to 3.
4.This process goes on until i becomes 6. When i is 6, the test
expression i <= 5 will be false and the loop terminates.
Do…while loop:-
In for and while loops, which
test the loop condition at the top of loop expression, the do while loop in c
check condition at the bottom of the loop.
It is just like normal while
loop, except it is surely execute the code at least one time.
Syntax:-
The syntax of a do...while
loop in C programming language is:-
statement(s);
} while( condition );
So you noticed that the conditional expression
appears at the end of the loop, so the statement in the loop executes once
before condition is tested.
How do...while loop works?
1.The body of do...while loop is executed once. Only then, the test
expression is evaluated.
2.If the test expression is true, the body of the loop is executed again and
the test expression is evaluated.
3.This process goes on until the test expression becomes false.
4.If the test expression is false, the loop terminates.
Example:-
// Program to add numbers
until the user enters zero
#include <stdio.h>
int main()
{
double number, sum = 0;
// the body of the loop is executed at
least once
do
{
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %.2lf", sum);
return 0;
}
MCQs on while and do…while
loop:-
1. What will be the output of
the following C code?
#include <stdio.h>
int main()
{
while ()
printf("In while loop ");
printf("After loop\n");
}
a) In while loop after loop
b) After loop
c) Compile time error
d) Infinite loop
#include <stdio.h>
int main()
{
int i = 0;
do {
i++;
printf("in while
loop\n");
} while (i < 3);
}
a) 2
b) 3
c) 4
d) 1
Answer: b
3. How many times i value is
checked in the following C code?
#include <stdio.h>
int main()
{
int i = 0;
while (i < 3)
i++;
printf("In while loop\n");
}
a) 2
b) 3
c) 4
d) 1
answer-c
4. What will be the output of the following C code?
#include <stdio.h>
void main()
{
int i = 2;
do
{
printf("Hi");
} while (i < 2)
}
a) Compile time error
b) Hi Hi
c) Hi
d) Varies
answer-a
5.What will be the of the following C code?
#include <stdio.h>
void main()
{
int i = 0;
do
{
printf("Hello");
} while (i != 0);
}output
a) Nothing
b) H is printed infinite times
c) Hello
d) Run time error
answer-c
Read my previous blog about for loop in C click here.....

You make great effort good job my blessings are with you ❤️❤️😘😎
ReplyDeleteGreat work 👏 👍 👌 💪 🙌
ReplyDeletewhat a great content .......very well done
ReplyDelete