/* */ Click to Join Live Class with Shankar sir Call 9798158723

C do-while


do-while is similar to a while loop and used when a set of statements have to be repeatedly executed at least once untile a certain condition is reached. When we do not know exactly how many time a set of statement have to be repeated do while can be used.

Syntax:-


do
{
...........
...........
updation
}while(expression);

do and while:are keywords also called reserved keywords.
The expression must be enclosed within parentheses and must be end with semicolon " ; ".


How do-while 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



Q.) W.A.P to print 1 to 5 using do while loop.
 #include<stdio.h>
 #include<conio.h>
 void main()
 { 
 int i=1; 
 do
 {
 printf(“%d\n”,i); 
 i++; 
 }while(i<=5);
 getch();
 }
Output
1
2
3
4
5
Above Program Explanation
  • Step 1:-
    We declared integer variables called i. The initialization statement is executed. i.e i=1
  • Step 2:-
    Next, print i value 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 do, 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.


Difference between while loop and do-while loop.


While loop do-while loop
It is a top-testing/entry controlled loop since the condition is checked in the beginning itself. It is a bottom-testing/exit controlled loop since the condition is checked at the bottom of the loop.
It is a pre-test loop. It is a post-test loop.
If the condition is evaluated false at the beginning, then the statement within the body of the loop will not be executed even once. In do while loop the body of the loop will be executed at least once.

C for loop MCQ Question


Q1.What is the output of while Program.?
int main()
{
    int a=65;
    
    do
    {
        printf("%d ", a);
        a++;
    }while(a <= 65);

    return 0;
}
  1. 65
  2. 87
  3. 78
  4. error

Answer:- a.
Explanation do { } block is executed even before checking while(condition) at least once. This prints 65. To loop for the second time, while (65 <= 63) fails. So, loop is quit.

Q2.What is the output of while Program.?
int main()
{
    int a=65;
    
    do
    {
        printf("%d ", a);
        a++;
        if(a > 68)
            break;
    }while(1);

    return 0;
}
  1. 65 66 67 68
  2. 65 66 67
  3. 65 66 67 68 69
  4. error

Answer:- a.
Explanation while(1) is infinite loop. So we kept if(condition) to break the loop. a++ is equivalent to a=a+1;