for loop control statement in C-language - C-Tutorial

Latest

Sunday, 9 October 2016

for loop control statement in C-language

FOR LOOP: for is also a loop control statement used for repetitive execution of statements.  The general format of a for statement is


Syntax:           for(Initialization ; Condition ; Increment/Decrement)
                                    {
-          -  -
-          -  - Code Block (Statements)
-          -  -
                                    }

           

Flowchart:




1. The initialization step is executed first, and only once. The initialization part is used to assign a values to variables.


2.         Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

3.         After the body of the for loop executes, the flow of control jumps back up to the increment/decrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

4.         The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
/* PROGRAM TO PRINT FIRST N NATURAL NUMBER USING FOR STATEMENT */

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

No comments:

Post a Comment