Explain for Loop in c
Explain for Loop in c
It provides a concise way to execute a block of code repeatedly for a predetermined number of
times or until a specific condition is met. It's particularly useful when you know in advance how
many times you need to iterate.
Syntax:
for (initialization; condition; update) {
// Code to be executed in each iteration of the loop
}
Output:
Iteration number: 0
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
Loop finished.
Explanation:
1. int i = 0; (Initialization): The variable i is declared and initialized to 0. This happens only
once.
2. i < 5; (Condition): The loop continues as long as the value of i is less than 5.
3. printf("Iteration number: %d\n", i); (Loop Body): This statement is executed in each
iteration.
4. i++; (Update): After each iteration, the value of i is incremented by 1.
5. The loop continues until i becomes 5, at which point the condition i < 5 becomes false,
and the loop terminates.
Example 2: Looping with a Different Step:
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i += 2) {
printf("Even number (starting from 1, incremented by 2):
%d\n", i);
}
return 0;
}
Output:
Even number (starting from 1, incremented by 2): 1
Even number (starting from 1, incremented by 2): 3
Even number (starting from 1, incremented by 2): 5
Even number (starting from 1, incremented by 2): 7
Even number (starting from 1, incremented by 2): 9
Output:
Counting down: 10
Counting down: 9
Counting down: 8
Counting down: 7
Counting down: 6
Counting down: 5
Counting down: 4
Counting down: 3
Counting down: 2
Counting down: 1
Important Notes:
● Semicolons are mandatory: You must have two semicolons inside the parentheses of
the for loop, separating the initialization, condition, and update parts, even if one or more
of these parts are empty.
● Empty for loop: You can have an empty loop body by simply placing a semicolon after
the closing parenthesis:
for (int i = 0; i < 10; i++); // Does nothing 10 times
This is sometimes used for time delays or specific hardware interactions, but should be
used with caution for clarity.
● Infinite loop: If you omit the condition or if the condition always evaluates to true, you will
create an infinite loop:
for (;;) {
printf("This will print forever (unless interrupted).\n");
// You'd typically have a break statement inside based on some
condition
}
Infinite loops can be useful in certain scenarios (like event-driven programs) but can also
cause your program to hang if not handled correctly.
● Scope of variables declared in the initialization: If you declare a variable within the
initialization part of the for loop (like int i = 0;), its scope is usually limited to the for loop
itself (and the code block within it) in modern C compilers (C99 and later).
The for loop is a fundamental control flow structure in C and is essential for automating
repetitive tasks and iterating over data structures like arrays. Understanding its components and
how they work together is crucial for writing effective C programs.