for loop in C – Part 2

Read: for loop in C – Part 1

Before proceeding to this tutorial I am assuming that you are familiar with for loop. If you didn’t read my last tutorial then I strongly recommend you to read that. Anyways till now we have learnt about the basic use of for loop and its execution.

Today I will tell you about the basic variations while using for loop. All three things initialization, condition and loop counter increment are resides within the syntax of for loop. This is one of the key advantages of using it. Giving below are some variations while using it.

1. It is not compulsory to initialize the for loop with the syntax. I can also initialise it before.

Consider the above example carefully. You will notice I have not initialized variable i within for loop. But I have left the space for it. I have also given semi-colon for it. It is compulsory even if you do not want to initialize a variable within for loop.

2. The same condition is also true if I do not mention the third part in it. Consider the below example.

Notice that I have not increment the loop counter within for loop. Instead of it I am incrementing it inside the body of the loop. Again it is compulsory to give semicolon.

3. What if I drop both the parts? Consider the below example carefully.

Will it work or not? Well its perfectly fine. So from the above three points we conclude that the initialization and increment part is completely optional in for loop.

4. Use of post increment/decrement operator within for loop.

Checkout the execution of the above program stepwise.

  • i will initialized with value 1
  • Condition is checked within for loop. It turns true.
  • After that i will increment to 2.
  • Now printf() function will display the message “This is working”.
  • Now again condition is checked with value i=2. It will turn true.
  • After that i will increment to 3.

Note: Post increment operator is used with i. So first the condition will be checked. After that i will increment to 2. The reverse is also true for pre-increment/decrement operator.

5. Use of logical operators in for loop.

You can check the loop for multiple conditions by using logical operators in it.

6. In the last tutorial I have told you that we can replace any valid expressions in these three parts. One good example of that is given below.

Shortest C program to print 10 numbers

Consider the above example carefully. I have inserted the printf() function in it. And I have also given a post increment operator with i. After the for loop I have given a semicolon. Because I don’t want to execute a single statement in loop after that.

Loops are important for this C programming tutorial. So I recommend you to try your hands on for loop. Try to write programs with some variations to obtain on good conclusions.

You can try the below programs for practice.
Write the shortest program to print reverse counting from 10 to 1.

1 thought on “for loop in C – Part 2”

  1. In point 5, while running this
    int i=0;
    for(;++i<=3;) // I have used increment operator in conditioning part itself
    printf("This is working");
    I get "This is working" only 3 times, but when i run the below coding

    int i=0;
    for(;i<=3;++i) // I have used increment operator in incrementing part
    printf("This is working");
    I get "This is working" 4 times, could you explain why???

Leave a Comment

Your email address will not be published. Required fields are marked *