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:-
  1. Initializes n,t,f as integer variables and assign f to 1.
  2. Asks user to enter a value to get factorial of a number.
  3. Storing the entered number in t.
  4. Checking whether the value n is nonzero or not.As it is non zero the loop continues for execution.
  5. f=f*n means f=1*5("if the entered value is 5 which is stored in variable n).Then f=5
  6. n gets decremented by 1 so now n becomes 4.
  7. 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).
  8. This process goes on until f=120 and n=0.
  9. Then it comes out of loop and prints the value of f.
  10. 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:-
  1. f=1*5 => f=5 and n--
  2. as n is post increment it increments in next step so her n becomes n=4;f=5*4 =>f=20; n--
  3. n=3;f=20*3 =>f=60;n--
  4. n=2; f=60*2 =>f=120; n--
  5. n=1; f=120*1 =>f=120; n--
  6. n=0 condition becomes false and loop terminates here.
  7. 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[100];
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:-
  1. 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.
  2. 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.
  3. Now it asks the user to enter name and age of a person dynamically*.
  4. 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'.
  5. Now here we used if-condition.If it is zero it breaks the loop and exit to the next line from the loop.
  6. 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.
  7. Like this it continues.
*dynamically:-It means to give input during run-time(during execution) of the program.  
Program:-

Do While Program

 OUTPUT:-

Do While Program output

No comments:

Post a Comment