Nearby lessons

46 of 124

C - For Loop

For Loop is one of the foundational topics in C programming. This lesson explains The for Loop with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

The for Loop

The for loop gathers three things in one line: where to start, when to stop, and how to move forward.

In simple words: for puts the whole plan in one line — start, stop, step. Read for (i = 1; i <= 5; i++) as "i starts at 1, keep going while i ≤ 5, add 1 each round".
Example01
CCode Cell
1for (start; condition; update) {
2 // body
3}

The for Loop

Example02
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int i;
6 for (i = 1; i <= 5; i++) {
7 printf("%d ", i);
8 }
9}
Output

1 2 3 4 5 
      

The for Loop

Example03
CCode Cell
1for (i = 1; i <= 5; i++) // start=1, condition i<=5, update i++
2 | | |
3 start stop step

Program: The classic textbook examples: print even numbers, print a multiplication table, sum of first n numbers.

Example04
CCode Cell
1// Table of 5
2for (int i = 1; i <= 10; i++) {
3 printf("5 x %d = %d\n", i, 5 * i);
4}
Output

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
      
📝 Key Takeaways
  • while checks first; do-while runs at least once; for gathers start/condition/update.
  • for is best when you know the number of iterations.
  • break stops the loop; continue skips one round.
  • Nested loops: inner loop completes for each outer round.
  • Pattern programs = outer loop (rows) + inner loop (columns).
  • Never forget the update (i++) — else you get an infinite loop.

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2