do-while-loop-in-C .

In this session i have discussed do-while loop in C .


The do-while loop is exit control loop .

Do-While-Syntax :


    do
    {
        loop body
    
    }while(test condition)
    
    

In do while loop first control come inside the loop body and then check the condition if condition is true then again control come inside the loop body other wise loop will be terminated .

Example : Write a program that print the number 1-10 using do while loop .

    #includ<stdio.h> 
    int main()
    {
            int i=1;
            do
            {
                printf("%d",i);
                i++;
            
            }while(i<=10);
            return 0;
    }
    
    OUTPUT :  1 2 3 4 5 6 7 8 9 10
    

Example : Write a program that print the table of 2 using do while loop .

    #includ<stdio.h> 
    int main()
    {
            int i=1;
            int t ;
            do
            {
              t = i * 2;
              printf("%d",t);
              printf(" ");
              i++;
            
            }while( i<=10 );
            return 0;
    }
    
    OUTPUT :  2 4 6 8 10 12 14 16 18 20
    

Example : Write a program that print the table of 01-10 using do while loop .

    #includ<stdio.h> 
    int main()
    {
            int i=1;
            int t,j ;

            do
            {
              j = 1;
              do
              {
                 t = j * i;
                 
                 if (t < 10)
                 {
                    printf("0%d",t);
                    printf(" ");
                 }
                 else
                 {
                    printf("%d",t);
                    printf(" ");
                 }
                   j++;
                
               } while (j <= 10);

               printf("\n");
               i++;
            
            }while( i<=10 );
            return 0;
    }
    

    OUTPUT :  01 02 03 04 05 06 07 08 09 10
              02 04 06 08 10 12 14 16 18 20
              03 06 09 12 15 18 21 24 27 30
              04 08 12 16 20 24 28 32 36 40
              05 10 15 20 25 30 35 40 45 50
              06 12 18 24 30 36 42 48 54 60
              07 14 21 28 35 42 49 56 63 70
              08 16 24 32 40 48 56 64 72 80
              09 18 27 36 45 54 63 72 81 90
              10 20 30 40 45 60 70 80 90 100
    

Example : Write the menu program using do-while loop .

    #includ<stdio.h> 
    int main()
    { 
        int x, y;
        char i;
        int op;
        do
        {
            printf("1-Addition");
            printf("2-Subtraction");
            printf("3-Multiplication");
            printf("4-Division");
            printf("Enter your choice :");
            scanf("%d",&op);

            switch(op)
            {
                case 1:
                {
                    printf("Enter the value for x : ");
                    scanf("%d",&x);
                    printf("Enter the value for y : ");
                    scanf("%d",&y);
                    int sum=x+y;
                    printf("Sum = %d",sum);              
                    break;
                }
                case 2:
                {
                    printf("Enter the value for x : ");
                    scanf("%d",&x);
                    printf("Enter the value for y : ");
                    scanf("%d",&y);
                    int sub=x-y;
                    printf("Sub = %d",sub);              
                    break;
                }
                case 3:
                {
                    printf("Enter the value for x : ");
                    scanf("%d",&x);
                    printf("Enter the value for y : ");
                    scanf("%d",&y);
                    int mul=x*y;
                    printf("Mul = %d",mul);              
                    break;
                }
                case 4:
                {
                    printf("Enter the value for x : ");
                    scanf("%d",&x);
                    printf("Enter the value for y : ");
                    scanf("%d",&y);
                    int div=x/y;
                    printf("Div = %d",div);              
                    break;
                }
                default :
                {
                    printf("Invalid Number");
                    break;
                }
       }
         printf("\ndo you want to continue press y / Y : ");
         scanf("%c",&i);
     
     }while(i=='y'||i=='Y');
   
}
    

   OUTPUT :  1-Addition
             2-Subtraction
             3-Multiplication
             4-Division
             Enter your choice : 1
             Enter the value for x : 20
             Enter the value for x : 10
             Sum = 30
             do you want to continue press y / Y : n  
      

Welcome to AndroidATPoint.

back image                     next image