C For Loop

C Tutorial

C for Loop (Complete Tutorial: Beginner → Advanced)

The for loop in C language is used when you know in advance how many times a block of code should execute.
It is one of the most commonly used loops in C, especially in DSA, arrays, and interviews.


 What is a for Loop?

A for loop repeatedly executes a block of code until a given condition becomes false.


Syntax of for Loop

Execution Order

  1. Initialization (once)

  2. Condition check

  3. Loop body

  4. Increment / Decrement

  5. Repeat from step 2


 Simple for Loop Example


 

 Output:

1 2 3 4 5

for Loop with Decrement

Output:

5 4 3 2 1

 Infinite for Loop

  •  Runs forever
  • Must use break to stop

for Loop with Multiple Variables

  •  Useful in advanced logic

for Loop with Arrays


 

  • Very common in DSA

Nested for Loop (Pattern Programs)

Output:

* * *
* * *
* * *

break and continue in for Loop

break – Exit Loop

 Output: 1 2


continue – Skip Iteration

Output: 1 2 4 5


for Loop Without Body

  •  Semicolon makes loop body empty
     Output: 5

for Loop vs while Loop

Featureforwhile
Best forFixed iterationsUnknown iterations
InitializationInside loopOutside loop
ReadabilityHighMedium

Common Mistakes

  •  Using = instead of == in condition
  •  Infinite loop due to wrong condition
  •  Forgetting increment/decrement
  • Extra semicolon after for

 Interview Questions (Must Prepare)

  1. Difference between for and while

  2. Can for loop run without initialization?

  3. What is for(;;)?

  4. Can we use multiple variables in for loop?

  5. Difference between break and continue


Real-Life Use Cases

  • Array traversal

  • Pattern printing

  • Searching & sorting

  • Embedded loops

  • Competitive programming


Summary

  • for loop is ideal for known iterations
  • Supports initialization, condition & update
  •  Can be nested and infinite
  •  Essential for DSA & interviews

You may also like...