for loop In C

In this article, you are going to learn about for-loop you have discussed while-loop in the previous article

* The for loop is a more concise loop control structure

* This is an entry-controlled loop

* it is a fixed loop

* In this loop initialization of a counter variable, test-condition and incrementation of a counter variable are in one line.  

          *The general format of the for loop is shown below:

                        for ( initialization; test-condition; increment ){



look at the following example.

The logic of the above program is

n=9( User has given input )

i                       condition(i<=n)          Result                     print i                                 i=i+2

1                         (1<=9 )                      true                            1                                     i=1+2=3
3                         ( 3<=9 )                     true                            3                                      i=3+2=5
5                         ( 5<=9 )                     true                            5                                     i=5+2=7
7                         ( 7<=9 )                     true                            7                                     i=7+2=9
9                         ( 9<=9 )                     true                            9                                      i=9+2=11
11                       ( 11<=9 )                   false                           -                                             -

In the above example, i is incremented by twice( i=i+2). when does the execution of statements in for loop stops? when the condition is failed in the above example I should be lesser or equal to n as 11 <=9 ( false ) for loop gets terminated

We will discuss Nested for loops later in C language. I think you have got some basic idea of how loops work thank you.

Post a comment