C Programming • C Basics, Operators, Loops
C Programming / Control Structures: for

Control Structures: for

Notes 2 C Basics, Operators, Loops

Definition: The for loop is a repetition control structure used to execute a block of statements repeatedly. It is especially useful when the number of iterations or the loop range is known in advance.

Notes

For Loop in C

Definition: The for loop is a repetition control structure used to execute a block of statements repeatedly. It is especially useful when the number of iterations or the loop range is known in advance.

💡 Example: A for loop can be used to print numbers from 1 to 5.
#include <stdio.h>

int main()
{
    int i;

    for (i = 1; i <= 5; i++)
        printf("%d ", i);

    return 0;
}

Output

1 2 3 4 5

Syntax of for Loop

The for loop contains three important parts: initialization, condition and update.

for (initialization; condition; update)
{
    statements;
}

Example

💡 Example: In for (i = 1; i <= 5; i++):

Initialization: i = 1
Condition: i <= 5
Update: i++

How for Loop Works

Initialization ↓ Check Condition ↓ Condition TRUE? ↓ ↓ Yes No ↓ ↓ Execute Body Exit Loop ↓ Update ↓ Check Condition Again

Real-World Example

A college application may need to process the records of a fixed number of students. A for loop can repeat the same operation for each student.

💡 Example: Process student 1 → student 2 → student 3 → ...

Practical Example — Sum of Natural Numbers

Problem Statement

Write a C program to calculate the sum of natural numbers from 1 to N using a for loop.

Learning Outcomes

  • Understand the use of a for loop.
  • Use initialization, condition and update in one loop statement.
  • Calculate a running total using a loop.

Hint

Initialize sum to 0 and use a for loop from 1 to N. Add each value to sum.

Theory

A for loop combines initialization, condition checking and updating in a compact structure. It is commonly used when the number of iterations is known or follows a fixed range.

Program

#include <stdio.h>

int main()
{
    int n, sum = 0;          // sum stores the running total

    printf("Enter N: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++)   // i runs from 1 to n
        sum += i;                   // add the current value to sum

    printf("Sum of first %d natural numbers = %d
", n, sum);

    return 0;
}

Expected Output

Enter N: 10
Sum of first 10 natural numbers = 55

Note

A common mistake is using the wrong loop condition or forgetting the update expression. Make sure the loop variable moves toward the stopping condition.

for Loop vs while Loop

for while
Initialization, condition and update are written together. Initialization and update are usually written separately.
Useful when the loop range is known. Useful when repetition depends mainly on a condition.
Compact loop structure. More flexible when the update logic is complex.

Important Points for Exam

  • The for loop is used for repeated execution.
  • It contains initialization, condition and update.
  • The condition is checked before each iteration.
  • If the condition is false initially, the loop body does not execute.
  • The update expression changes the loop variable after each iteration.
🎯 Easy Rule:

Initialize → Check → Execute → Update → Repeat

Quick Revision

Part Meaning
Initialization Sets the starting value.
Condition Determines whether the loop continues.
Update Changes the loop variable.

Important Exam Questions

Short Answer Questions

  1. What is a for loop in C?
  2. Write the syntax of a for loop.
  3. What are the three parts of a for loop?
  4. What is the purpose of initialization?
  5. What is the purpose of the update expression?
  6. What happens if the condition is false initially?

Long Answer Questions

  1. Explain the for loop with syntax and suitable example.
  2. Write a C program to calculate the sum of natural numbers from 1 to N using a for loop.
  3. Explain the three parts of a for loop with an example.
  4. Differentiate between for and while loops.
🎥 Recommended Learning

Watch a beginner-friendly explanation of the for loop in C.

▶ Watch: for Loop in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for the for loop will be provided here.

🧠 Mind Map

Use the mind map for quick revision of initialization, condition, execution and update.