C While Loop


A while loop is a control statement using which the programmer can give instructions to the computer to execute a set of statement repeatedly as long as specified condition is satisfied. Ones the specified condition is false control comes out of the loop.

Syntax:-


while(condition)
{
Statement….
updating
}

Note:-If the loop body contains only one statement, then the braces are optional.

How while Loop Works

  • Step 1:-
    The initialization statement is executed first
  • Step 2:-
    Then, the test expression is evaluated. If the test expression is evaluated to false, then the while loop gets terminated and the control comes out of the while loop.
  • Step 3:-
    if the test expression is evaluated to true,The statements defined inside the while loop will repeatedly execute until the given condition fails.
  • Step 4:-
    After successful execution of statements inside the body of the while loop, the update expression is executed.
  • Step 5:-
    Again the test expression is evaluated.

Example with dry Run Concepts


steps-shineskill


Note:

"When we do not know exactly how many times a set of statements have to be repeatedly executed, in such a situation we use while-loop."


Q.) W.A.P to print 1 to 5.
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int i=1;
 while(i<=5)
 {
 printf(“%d\n”,i);
 i++;   
 }
 getch();
 }
Output
1
2
3
4
5

Program Explanation
  • Step 1:-
    We declared integer variables called i. The initialization statement is executed. i.e i=1
  • Step 2:-
    Then, the test expression is evaluated. i.e i is less than or equals to 5 or not (i <=5 ) hear condition is true so statements inside the body of the while loop are executed, and it will print the value of i i.e 1
  • Step 4:-
    After successful execution of statements inside the body of the loop, the update expression (i.e i++) means it incremented by 1 now the value of i is 2
  • Step 5:-
    Again the test expression is evaluated. and repeat step 2 till the condition is false
  • Step 5:-
    when the condition is false means the value of i will be 6 then while loop gets terminated and the control comes out of the loop.


Q.) W.A.P to Enter a Number and Print Sum of each digits
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
    int n,s=0;
	printf("Enter a Number");
	scanf("%d",&n);
    while(n!=0)
   {
      a=n%10;
      n=n/10;
      s=s+a;	  
    }
	printf("%d",s);
 getch();
 }
Output
Enter a Number:- 123
Sum of each digits are:- 6

Program Explanation
  • Step 1:-
    We declared integer variables called n and s;
  • Step 2:-
    hear we initialization s=0 because we will have to sum of each digits so it not initialization garbage value
  • Step 3:-
    hear we use % and / operator as we know that any number % by 10 remainders will be last digits and if any number / by 10 quotient will be before last digits
    Let`s n=123 and we have to sum of s=1+2+3 i.e 6
    r=n%10; value of r will be 3 and q=n/10 value of q will be 12
  • Step 4:-
    Next, the test expression is evaluated. i.e n is not equals to 0 (n !=0 ) hear condition is true so statements inside the body of the while loop are executed, and the first iteration value of a will be 3 and value of n will be 12
  • Step 5:-
    Again the test expression is evaluated. and repeat step 2 till the condition is false
  • Step 6:-
    when the condition is false means the value of n will be 0 then while loop gets terminated and the control comes out of the loop.
  • Step 7:-
    After terminated loop sum of each digits print

C While loop MCQ Question


Q1.What is the output of
int main()
{
  int a=0, b=0;
  while(++a < 4)
     printf("%d ", a);
  while(b++ < 4)
     printf("%d ", b);
  return 0; 
}

  1. 0 1 2 3 1 2 3 4
  2. 1 2 3 1 2 3 4
  3. 1 2 3 4 1 2 3 4
  4. 1 2 3 4 0 1 2 3

Answer:- B.
Explanation (++a < 4) first increments and compares afterwards. (b++ < 4) first compares and increments afterwards.

Q2.Choose correct C while loop syntax.
  1. while(condition)
    {
        //statements
    }
    
  2. (condition)
    {
        //statements
    }while
    
  3. do(condition)
    {
        //statements
    }while
    
  4. for(condition)
    {
        //statements
    }
    

Answer:- a.

Q3.What is the output of while Program.?
int main()
{
    while(true)    
    {
        printf("RAJ");
        break;
    }
    
    return 0;
}

  1. RAJ
  2. RAJ is printed unlimited number of times.
  3. No output
  4. Compiler error.

Answer:- d.
Explanation while(TRUE) or while(true) hear true is not a keyword.

Q4.What is the output of while Program.?
int main()
{
    int a=5;
    
    while(a=123)    
    {
        printf("RAJ\n");
        break;
    }
    printf("RANCHI");
    
    return 0;
}

  1. RAJ
  2. RAJ
    RANCHI
  3. No output
  4. Compiler error.

Answer:- b.
Explanation while(a=123) = while(123) = while(Non Zero Number). So while is executed. BREAK breaks the loop immediately. Without break statement, while loop runs infinite number of times.

Q5.What is the output of while Program.?
int main()
{
    int a=5;
    
    while(a >= 3);
    {
        printf("RAJ\n");
        break;
    }
    printf("RANCHI");
    
    return 0;
}

  1. RAJ
  2. RAJ
    RANCHI
  3. No output
  4. None of the above

Answer:- d.
Explanation Notice a semicon(;) after while condition. It makes the printf and break statement blocks isolate.