C For Loop
1. What is a for Loop?
-
The
forloop is used when you know how many times you want to repeat a block of code. -
Syntax:
Explanation of Parts:
| Part | Description |
|---|---|
| Initialization | Sets up the loop control variable (executed once) |
| Condition | Checked before each iteration; loop stops if false |
| Increment/Decrement | Updates the loop control variable after each iteration |
2. Simple Example – Print 1 to 5
Output:
3. Loop in Reverse – Print 5 to 1
Output:
4. Multiplication Table Using for Loop
Output:
5. Sum of First N Numbers
Sample Input/Output:
6. Nested for Loops – Print a Pattern
Output:
7. Key Points
-
forloop is ideal when the number of iterations is known beforehand. -
Initialization, condition, and increment/decrement are all in one line.
-
Can be nested for patterns, matrices, or complex iterations.
-
Loop variables can be declared inside or outside the loop.
