For loop in C language with Example programs

Spread the love
for-loop-in-c-programming-language-with-example-programs

Introduction:

We have discussed the while loop in our earlier article, In today’s article, We will look at the for loop in C language with example programs.

For loop in C Language:

We use the for loop to iterate over a block of code multiple times. The loop will continue until the loop condition becomes false. The for loop is very similar to the while loop.

Here is the syntax of the for loop

Syntax of the for loop in C language:

Flow Diagram of For Loop in C Programming:

for-loop-in-c-programming-with-example-programs

How for Loop Work in C?

  • The for loop execution starts with the Initialization statement.
  • The Initialization statement is used to initialize any variables and It is going to execute only once.
  • Once the Initialization is completed, the Condition is evaluated.
  • If the Condition returns True, Then the body of the For loop will be executed.
  • Once the body of the for loop is executed, For loop will execute the update statement, Where we usually increment or decrement our control variable.
  • Once the Update step is completed, For loop will check the Condition again, If it is evaluated to True, the body of the for will be executed, followed by the update statement.
  • The above step will continue until the Condition returns False, Once the Condition becomes False, Then for loop will terminate and program control comes out of the for loop.

📢 The Initialization statement is executed only once, And it is an Optional statement. You can skip it but make sure to include the semicolon (;), We will look at one example about it in the following sections.

📢 The Update step is very important, So make sure to update your loop control variable appropriately to avoid infinite loops.

Program to understand the For loop in C programming:

Let’s look at an example program to understand the for loop in C Language

We will write a program to print the odd numbers from 1 to 10 using the for loop.

Save the program as the for-loop.c and build and run it.

For loop Example Program Output:

Program Explanation: For Loop in C Step by Step walk-through:

The for loop has three main steps

  • Initialization: We have initialized our loop control variable i = 1
  • Condition: This is our loop condition, We need to find the odd numbers up to 10, So we set our condition as i 10, The for loop will continue until loop condition becomes False
  • Update: This is where we update our loop control variable i. We are updating our control variable i by 1 at each iteration using the post-increment operation – i++.

Let’s look at the step-by-step walk-through:

First Iteration:

At the start,

  • Initialization:
    • The loop control variable i is initialized with value of 1,
  • Check Condition:
    • The value of i is 1 so The for loop condition i 10 is evaluated as True
  • Execute Body:
    • As the condition is evaluated as True the body of for loop is executed.
    • Which checks if i is an odd number using the modulus operator and prints i variable if it Odd number, Our i present value is 1, So it is an Odd number. So the number will be printed on the console.
  • Update:
    • After that, We need to update the control variable, We are increasing the i by 1, So our i variable becomes 2

Output so far: 1

Second Iteration:

📢 The initialization step won’t be executed. It will be executed only once (at the start of the for loop).

  • Check Condition:
    • The value of i is 2 so The for loop condition i 10 is evaluated as True
  • Execute Body:
    • As the condition is evaluated as True, the body of for loop is executed.
    • Which checks if i is an odd number using the modulus operator and prints i variable if it Odd number, Our i present value is 2, So it is not an Odd number. So the number won’t be printed on the console.
  • Update:
    • After that, We need to update the control variable, We are increasing the i by 1, So our i variable becomes 3.

Output so far: 1

Third Iteration:

  • Check Condition:
    • The value of i is 3 so The for loop condition i 10 is evaluated as True
  • Execute Body:
    • As the condition is evaluated as True. The body of for loop is executed.
    • Which checks if i is an odd number using the modulus operator and prints i variable if it Odd number, Our i present value is 3, So it is an Odd number. So the number will be printed on the console.
  • Update:
    • After that, We need to update the control variable, We are increasing the i by 1, So our i variable becomes 4

Output so far: 1 3

Fourth Iteration:

  • Check Condition:
    • The value of i is 4. so The for loop condition i 10 is evaluated as True
  • Execute Body:
    • As the condition is evaluated as True, the body of for loop is executed.
    • Which checks if i is an odd number using the modulus operator and prints i variable if it Odd number, Our i present value is 4, So it is not an Odd number. So the number won’t be printed on the console.
  • Update:
    • After that, We need to update the control variable, We are increasing the i by 1, So our i variable becomes 5.

Output so far: 1 3

Fifth Iteration:

  • Check Condition:
    • The value of i is 5. so The for loop condition i 10 is evaluated as True
  • Execute Body:
    • As the condition is evaluated as True. The body of for loop is executed.
    • Which checks if i is an odd number using the modulus operator and prints i variable if it Odd number, Our i present value is 5, So it is an Odd number. So the number will be printed on the console.
  • Update:
    • After that, We need to update the control variable, We are increasing the i by 1, So our i variable becomes 6.

Output so far: 1 3 5

Sixth Iteration:

  • Check Condition:
    • The value of i is 6. so The for loop condition i 10 is evaluated as True
  • Execute Body:
    • As the condition is evaluated as True, the body of for loop is executed.
    • Which checks if i is an odd number using the modulus operator and prints i variable if it Odd number, Our i present value is 6, So it is not an Odd number. So the number won’t be printed on the console.
  • Update:
    • After that, We need to update the control variable, We are increasing the i by 1, So our i variable becomes 7.

Output so far: 1 3 5

Seventh Iteration:

  • Check Condition:
    • The value of i is 7. so The for loop condition i 10 is evaluated as True
  • Execute Body:
    • As the condition is evaluated as True. The body of for loop is executed.
    • Which checks if i is an odd number using the modulus operator and prints i variable if it Odd number, Our i present value is 7, So it is an Odd number. So the number will be printed on the console.
  • Update:
    • After that, We need to update the control variable, We are increasing the i by 1, So our i variable becomes 8.

Output so far: 1 3 5 7

Eighth Iteration:

  • Check Condition:
    • The value of i is 8. so The for loop condition i 10 is evaluated as True
  • Execute Body:
    • As the condition is evaluated as True, the body of for loop is executed.
    • Which checks if i is an odd number using the modulus operator and prints i variable if it Odd number, Our i present value is 8, So it is not an Odd number. So the number won’t be printed on the console.
  • Update:
    • After that, We need to update the control variable, We are increasing the i by 1, So our i variable becomes 9.

Output so far: 1 3 5 7

Ninth Iteration:

  • Check Condition:
    • The value of i is 9. so The for loop condition i 10 is evaluated as True
  • Execute Body:
    • As the condition is evaluated as True. The body of for loop is executed.
    • Which checks if i is an odd number using the modulus operator and prints i variable if it Odd number, Our i present value is 9, So it is an Odd number. So the number will be printed on the console.
  • Update:
    • After that, We need to update the control variable, We are increasing the i by 1, So our i variable becomes 10.

Output so far: 1 3 5 7 9

Tenth Iteration:

  • Check Condition:
    • The value of i is 10. so The for-loop condition i 10 is evaluated as True
  • Execute Body:
    • As the condition is evaluated as True, the body of for loop is executed.
    • Which checks if i is an odd number using the modulus operator and prints i variable if it Odd number, Our i present value is 10, So it is not an Odd number. So the number won’t be printed on the console.
  • Update:
    • After that, We need to update the control variable, We are increasing the i by 1, So our i variable becomes 11.

Output so far: 1 3 5 7 9

Eleventh Iteration:

  • Check Condition:
    • The value of i is 11. so The for loop condition i 10 is evaluated as False

As the for loop condition is evaluated as False, The for loop terminates, and program control comes out of the loop.

Final Output : 1 3 5 7 9

Example 2: For loop to generate multiplication table for given number:

Let’s write another program to make our understanding concrete.

We will write a program to generate the multiplication table. The program will ask for a number and generates the multiplication table for the given number up to 10.

For loop Program Output:

Multiplication Table Program Explanation:

The program started with asking the user for an input number and we are storing that number in num variable.

We also created a counter variable i

As we need to print the table up to 10 times, We decided to use the for loop

  • We initialized the counter variable i to 1.
  • Our condition is i 10 , Which means for loop will continue until i value reaches greater than 10
  • We are updating the control variable i by 1 at each iteration.

So the above for loop iterates 10 times and at each iteration prints one line (by multiplying i and num) on to the console. Thus generating the multiplication table up to 10.

Exercise For loop Program:

The above program, by default, generates the table up to 10 numbers. Re-Write the above multiplication table program to generate the table up to the user-given number.

You need to take two numbers from the user.

Expected Output:

For loop without Initialization:

We usually see many programs without the for loop initialization statement. Sometimes you already have the desired value ready, so you don’t need to initialize again.

Here is a program to print numbers from 1 to 10.

Program Output:

As you can see we not initialized the counter at the start of for loop.

📢 All three statements of the for loop are optional.

Infinite Loops using the for loop in C language:

As we have discussed in our previous article, If you forgot to update the loop control variable properly, Then there will be a good chance of creating infinite loops.

Here is an example of an unintentional infinite loop.

Let’s say, We want to print the numbers from 1 to 5 on the console. but we forgot to increment the counter, Then it can create the infinite loop.

Infinite for loop output:

Note: Press the CTRL + C to stop the Infinite loop( In Linux and Mac). Close the output window if you are on Windows PC.

We missed updating the loop control variable counter in the above example. To Avoid Infinite loops, We need to check loop Condition Properly, and Update the Loop Control variable (i.e Counter)

Infinite loop with for loop:

All Three options of the for loop are optional, So you can create an Infinite loop by not specifying any options like below.

The above code will generate the Infinite Loop.

📢 We still need to specify the semicolons (;) , Otherwise the compiler will throw an error.

Conclusion:

We have discussed about the For loop in C programming language with example programs. We also looked at the step-by-step walk-through of the for loop. Finally, we briefly covered Infinite loops using the for loop. In the following article, We will learn about the do while loop in C Language.

Practice programs on Loops:

Tutorials Index:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. September 23, 2022

    […] For Loop in C Language with Example Programs […]

  2. September 25, 2022

    […] have discussed about the while loop and for loop in c language in our earlier articles, In today’s article, We are going to look at the do […]

Leave a Reply