do-while loop control statements in C-language - C-Tutorial

Latest

Saturday, 8 October 2016

do-while loop control statements in C-language

do-while statement:               do-while is also a loop control statement used for repetitive execution of statements.  The general format of a do-while statement is:


                        
                          Syntax:           do
                                                {
-          -  -
-          -  -      Code Block (Statements)
-          -  -
                                                }

                                                while(condition);


Ø  First the compiler executes Block statements and then enters into condition section.
Ø  Condition is evaluated and produces either TRUE or FALSE.
Ø  If the condition outcome is TRUE, then again control enters into Block Statement and is executed.  This procedure is repeated until the condition becomes FALSE.
Ø  When the condition outcome reaches to FALSE, then the control is transferred out of the loop.

/* PROGRAM TO PRINT FIRST N NATURAL NUMBER USING DO-WHILE STATEMENT */

 main()
{
            int i=1,n;
            clrscr();
            printf("\nEnter how many numbers:");
            scanf("%d",&n);
            printf("\nNATURAL NUMBERS ARE:");
            do
            {
                        printf("%5d",i);
                        i=i+1;
            }
            while(i<=n);
}

No comments:

Post a Comment