## fb popup

/* remove if it's exist in your template */

### While And Do-While Loop

While Loop:-It is used instead of for loop.And it has the following syntax.

Syntax:-
```initialisation;

while(condition)

{

statements;

increment/decrement operator;

}```

while is used mainly in the cases where we do not know how many times a loop will run.

FOR EX:-To print factorial of a number.
```#include<stdio.h>

main()

{

int f=1,n,t;

printf("Enter a number\n");

scanf("%d",&n);

t=n;

while(n)

{

f=f*n;

n--;

}

printf("Factorial of %d is %d\n",t,f);

}

output:-Enter a number

5

Factorial of 5 is 120.```

EXPLANATION:-
```
Initializes n,t,f as integer variables and assign f to 1.
Asks user to enter a value to get factorial of a number.
Storing the entered number in t.
Checking whether the value n is nonzero or not.As it is non zero the loop continues for execution.
f=f*n means f=1*5("if the entered value is 5 which is stored in variable n).Then f=5
n gets decremented by 1 so now n becomes 4.
The loop iterates and checks if n is nonzero.It enters and executes f=5*4(as f=5 and n=4 from previous iteration).
This process goes on until f=120 and n=0.
Then it comes out of loop and prints the value of f.
here we used a dummy variable t because n changes to zero after all iterations and to store that value for printing at the end of program we need to preserve it so we used it to store the entered value.

```
Steps involved:-
```
f=1*5   => f=5 and n--
as n is post increment it increments in next step so her n becomes n=4;f=5*4 =>f=20;  n--
n=3;f=20*3 =>f=60;n--
n=2; f=60*2 =>f=120; n--
n=1; f=120*1 =>f=120; n--
n=0 condition becomes false and loop terminates here.
prints value of f as 120.

```

DO WHILE LOOP:- It is mainly used when the loop must be executed at least one time.

Syntax:-
```do

{

................

statement

................

}while(condition);```

Ex:-
```#include<stdio.h>
main()
{
int i,a,age,x;
char name;
do
{
printf("enter name and age of a person\n");
scanf("%s%d",&name,&age);
printf("name=%s age=%d\n",name,age);
printf("If you want to enter again press 1 otherwise 0 \n");
scanf("%d",&x);
if(x==0)
break;
} while(1);

}```

Explanation of the above program:-
```
It initializes variables i,age,a,x as integer variables.And name as character variable with 100 blocks of spaces in memory so that we can enter a word containing 100 letters including spaces,commas,etc.
In "do while loop" what ever the condition is it executes the loop atleast one time before checking the condition and after that depending on the condition it will proceed.
Now it asks the user to enter name and age of a person dynamically*.
After entering the name and age then it asks the user whether he would continue with giving the details again or not.If yes press '1' otherwise '0'.
Now here we used if-condition.If it is zero it breaks the loop and exit to the next line from the loop.
If it is '1' then the while loop having '1' as condition with in it which means that the condition always evaluates as true. So, it continues for one more iteration and depending on the next user's choice it will decide whether to iterate or exit to the next line of the loop.
Like this it continues.

```
*dynamically:-It means to give input during run-time(during execution) of the program.
Program:-

OUTPUT:-