Do-while loop in c

 


   A do-while loop is used to ensure that the statements in the loop are executed at least once.
   Even-though the conditions becomes false for the first time itself, the statement under the loop will get executed at least once.
   The minor difference between while loop and do-while loop is that, in a while loop if the condition becomes false for the first iteration itself the statements under the loop will not get executed. In the do-while loop if the condition becomes false for the first iteration itself the statements will get executed, this is because in the do-while loop, first the statements are executed, and then the condition is checked.
  Let us understand the difference between a while loop and the do-while loop with an example.
do-while-loop-c

  •  In program 1 output is “no output” because the curser did not come inside while loop the condition became false while checking the first iteration itself. The statement inside the loop did not get print. And statement after while loop is printed.
  •     In Program 2 the output is “god is great” and “no output” because the statement under while loop has printed once as condition checking is done after executing statement under while loop. Statement after a while loop is also printed.

 

Odd Loop

The do-While loop is specially used when we don’t know how many times the statements in the loop are to be executed. This is also called an odd loop.

Example:

 
#include<stdio.h>
int main()
{
  char name[20];
  int age;
  char city[20];
  char choice;

 do
{
  printf("Enter the name age and city of student\n");
  scanf("%s %d %s", name, &age, city);
  fflush(stdin);
  printf("\n enter the choice:");
  scanf("%c", &choice);
  printf("\n choice=%c\n",choice);
 }while(choice=='y');

 return 0;
}  
Output::

do-while-loop-c

In the above example, we have to print the details of the new admission taking students. Details are name, age, city of the student.

We can print as the details of students as many we want. When we enter the choice ‘y’ we can enter the detail of the new student. When we enter the choice other than ‘y’ then we will come out of the do-while loop.

We can also do this type of programming using for loop and while loop. But as a do-while loop is more efficient that why we use a do-while loop.

a. Program using while loop             

quiz:: enter the details of new students taking admission                     

 
 
#include<stdio.h> 
int main()
{
  char name[20];
  int age;
  char city[20];
  char choice='y';

while(choice=='y')
{
  printf("Enter the name age and city of student\n");
  scanf("%s %d %s", name, &age, city);
  fflush(stdin);
  printf("\nenter the choice:");
  scanf("%c", &choice);
  printf("\nchoice=%c\n",choice);
 }while(choice=='y');

 return 0;
}  

b. Program using for loop

quiz:: enter the details of new students taking admission


#include<stdio.h>

int main()
{
  char name[20];
  int age;
  char city[20];
  char choice='y';
 
for( ;choice=='y';  )
{
  printf("Enter the name age and city of student\n");
  scanf("%s %d %s", name, &age, city);
  fflush(stdin);
  printf("\nenter the choice:");
  scanf("%c", &choice);
  printf("\nchoice=%c\n",choice);
 }while(choice=='y');

 return 0;
}  
 
See Also: