Back to C Programming
2026-07-125 min read

C for Loop in C

Learn C for Loop in C step by step with clear examples and exercises.

Title: Mastering C for Loop: A full guide to Repeating Code Efficiently

Why This Matters

In programming, repetition is a common task that can be tedious and error-prone when done manually. The for loop in C provides an efficient way to repeat a block of code a specific number of times or until a certain condition is met. Understanding the for loop is essential for writing cleaner, more efficient, and less prone-to-error code. This knowledge can be crucial during exams, interviews, and real-world programming projects.

Prerequisites

Before diving into the for loop, you should have a good understanding of C basics such as variables, data types, operators, and control structures like if...else. Familiarity with arrays will also be helpful when working with loops in C. It is recommended to review these concepts before proceeding.

Core Concept

Syntax

The basic syntax of the for loop in C is:

for (initialization; condition; increment/decrement) {
// code to be executed
}
  • Initialization: This statement initializes the control variable (usually an integer) before the loop starts.
  • Condition: This statement checks whether the loop should continue or not. If it evaluates to true, the loop continues; if false, the loop terminates.
  • Increment/Decrement: This statement modifies the control variable after each iteration, allowing the loop to terminate when the condition becomes false.

Example 1 - Simple Loop

Here's a simple example that prints numbers from 1 to 5 using a for loop:

#include <stdio.h>

int main() {
int i;
for (i = 1; i <= 5; ++i) {
printf("Number: %d\n", i);
}
return 0;
}

In this example, the control variable i is initialized to 1. The condition i <= 5 checks if i is less than or equal to 5. If so, the loop continues by printing the value of i. After each iteration, i is incremented using the ++i statement.

Example 2 - Complex Loop with Control Variable Modification

Let's consider a more complex example where we find the sum of all even numbers between 1 and 20.

#include <stdio.h>

int main() {
int sum = 0, i;

for (i = 2; i <= 20; i += 2) {
sum += i;
}

printf("Sum of even numbers between 1 and 20: %d\n", sum);
return 0;
}

In this example, the control variable i is initialized to 2 (the first even number in our range) and incremented by 2 after each iteration. The condition checks if i is less than or equal to 20. Inside the loop, we add the current value of i to the sum variable.

Worked Example

Let's consider a more complex example where we find the sum of all odd numbers between 1 and 50.

#include <stdio.h>

int main() {
int sum = 0, i;

for (i = 1; i <= 50; i += 2) {
sum += i;
}

printf("Sum of odd numbers between 1 and 50: %d\n", sum);
return 0;
}

In this example, the control variable i is initialized to 1 (the first odd number in our range) and incremented by 2 after each iteration. The condition checks if i is less than or equal to 50. Inside the loop, we add the current value of i to the sum variable.

Common Mistakes

  1. Forgetting semicolons: Semicolons are required at the end of each statement in C, including those within a for loop.
// Incorrect
for(int i = 0; i < 5; print("Hello");)

// Correct
for(int i = 0; i < 5; i++) {
printf("Hello\n");
}
  1. Misplaced or missing curly braces: Incorrect placement of curly braces can lead to logic errors and unexpected behavior.
// Incorrect
for(int i = 0; i < 5; print("Hello")) {
printf("World\n");
}

// Correct
for(int i = 0; i < 5; i++) {
printf("Hello\n");
printf("World\n");
}
  1. Incorrect initialization, condition, or increment: The initial value of the control variable, the loop condition, and the increment/decrement statements should be carefully chosen to ensure that the loop executes the correct number of times.
  1. (New) Forgetting to initialize sum variable before using it in the loop: If the sum variable is not initialized before the loop, it will contain garbage values from previous runs, leading to incorrect results.
// Incorrect
int main() {
int i;
for (i = 1; i <= 5; ++i) {
printf("Number: %d\n", i);
sum += i; // sum is not initialized before using it in the loop
}
return 0;
}

Practice Questions

  1. Write a for loop that prints the multiplication table for 7 (up to 10).
  2. Write a for loop that calculates and prints the factorial of a number entered by the user.
  3. Write a for loop that finds the sum of all odd numbers between 1 and 50, using two separate variables: one for the sum and another for the current number being processed.
  4. (New) Rewrite the example where we find the sum of even numbers between 1 and 20, but this time use an array to store the numbers instead of a single variable sum. Print the total sum and the average of the stored numbers at the end.

FAQ

What happens if I forget to increment/decrement the control variable?

If you forget to modify the control variable, the loop will either run indefinitely (if the condition is never met) or not enough times (if the condition is always met). This can lead to infinite loops or incorrect results.

Can I use a for loop with arrays?

Yes, for loops are commonly used with arrays to iterate through each element and perform some operation on them.

What's the difference between for, while, and do...while loops in C?

The main difference lies in when the condition is checked:

  • for loop checks the condition at the beginning of each iteration.
  • while loop checks the condition at the beginning of each iteration, but it can also run an infinite number of times if the condition is always true.
  • do...while loop checks the condition at the end of each iteration, ensuring that the code inside the loop will be executed at least once before the condition is checked again.