👉In this tutorial , you will learn to create while and do...while loop in C programming with the help of examples.
👉The while
loop in C/C++ is used in situations where we do not know
the exact number of iterations of loop beforehand. The loop execution is
terminated on the basis of the test condition.
Syntax:
while (test_expression)
{
// statement
update_expression;
}
// statement
update_expression;
}
The various parts of the While
loop are:
👉 Test Expression: In this expression we have to
test the condition. If the condition evaluates to true then we will execute the
body of the loop and go to update expression. Otherwise, we will exit from the
while loop.
Example:
Example:
i <= 10
👉 Update Expression: After executing the loop body, this
expression increments/decrements the loop variable by some value.
Example:
Example:
i++;
How does a While loop executes?
✔ Control falls into the while loop.
✔ The flow jumps to Condition
✔ Condition is tested.
⭐ If Condition yields true, the flow goes into the Body.
⭐ If Condition yields false, the flow goes outside the loop
✔ The statements inside the body of the loop get executed.
✔ Updation takes place.
✔ Control flows back to Step 2.
✔ The do-while loop has ended and the flow has gone outside.
Example 1: -
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5;
clrscr();
while(a<=20)
{
printf(" * ");
a++;
}
getch();
}
Output :-
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
Example 2: -
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter Number");
scanf("%d",&a);
while(a<=10)
{
printf("%d \n",a);
a++;
}
getch();
}
Output :-
If user enter 1 then 1 to 10 number print
if user enter any number between 1 to 10 then start from there like if user enter 5 then
5
5
6
7
8
9
10
But If user enter more than 10 then they direct came out from program
Example 3: -
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter Number");
scanf("%d",&a);
while(a>=-10)
{
printf("%d \n",a);
a--;
}
getch();
}
Output :-
if user enters 5 then
5
4
3
2
1
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
5
4
3
2
1
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
Example 4: -
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter Number");
scanf("%d",&a);
while(a<=50)
{
printf("%d \t",a);
a=a+2;
}
getch();
}
Output :-
if user enters 2 then
2
4
6
8
.
.
.
.
48
50
2
4
6
8
.
.
.
.
48
50
Example 5: -
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter Number");
scanf("%d",&a);
while(a<=100)
{
printf("%d \n",a);
a=a+5;
}
getch();
}
Output :-
if user enters 5 then
5
10
15
20
25
30
.
.
.
95
100
5
10
15
20
25
30
.
.
.
95
100
Example 6: -
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,no,sum=0;
clrscr();
printf("enter the no:-");
scanf("%d",&no);
while(i<=no)
{
sum=sum+i;
i++;
}
printf("the sum of natural number are: %d",sum);
getch();
}
Output :-
if user enters 5 then answer is 15:
(because 1+2+3+4+5=15)
(because 1+2+3+4+5=15)
0 Comments
If any one want learn some specific program in Computer Languages, so They all can comment there program questions.