for Loop in C (Syntax, Nested Loop & Programs)
In this lesson: Learn the for loop in C with syntax, flow, nested loops and programs like table, sum and factorial — each with dry run and output.
for Loop Syntax
The for loop is the most commonly used loop in C. It's used when you know exactly how many times you want to repeat a block of code.
for (initialization; condition; update) { // Body: code to repeat } // initialization: runs once at start → int i = 0 // condition: checked before each iteration → i < 10 // update: runs after each iteration → i++
How the for Loop Works
- Initialize:
int i = 1— sets up the loop variable once - Check condition:
i <= 5— if false, exit loop - Execute body: run the code inside { }
- Update:
i++— increment/decrement the variable - Go back to step 2
Examples
Example 1: Print 1 to 10
#include <stdio.h> int main() { for (int i = 1; i <= 10; i++) { printf("%d ", i); } printf("\\n"); return 0; }
Example 2: Sum of First N Numbers
#include <stdio.h> int main() { int n, sum = 0; printf("Enter N: "); scanf("%d", &n); for (int i = 1; i <= n; i++) { sum += i; } printf("Sum of 1 to %d = %d\\n", n, sum); return 0; }
Example 3: Multiplication Table
#include <stdio.h> int main() { int num; printf("Enter number: "); scanf("%d", &num); printf("\\n--- Table of %d ---\\n", num); for (int i = 1; i <= 10; i++) { printf("%d x %d = %d\\n", num, i, num * i); } return 0; }
Example 4: Factorial
#include <stdio.h> int main() { int n; long long fact = 1; printf("Enter n: "); scanf("%d", &n); for (int i = 1; i <= n; i++) { fact *= i; } printf("%d! = %lld\\n", n, fact); return 0; }
Nested for Loop
A loop inside another loop. The inner loop completes all its iterations for each iteration of the outer loop.
#include <stdio.h> int main() { int rows = 4; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { printf("* "); } printf("\\n"); } return 0; }
More Pattern Programs
for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { printf("%d ", j); } printf("\\n"); }
for (;;) — infinite loop (no initialization, condition, update)
for (int i=10; i>=1; i--) — count down
for (int i=0; i<100; i+=5) — step by 5
Summary
forloop is used when the number of iterations is known- Syntax:
for (init; condition; update) { body } - Initialization runs once; condition checked before each iteration
- Nested loops: inner loop runs completely for each outer loop iteration
- Common uses: counting, summing, tables, arrays, patterns
Write programs: (1) Sum of even numbers 1-100 (2) Print all prime numbers up to N (3) Fibonacci series up to N terms (4) Right-angled star triangle (5) Inverted star pattern.
Frequently Asked Questions
What is a for loop in C?
for loop repeats a block of code a known number of times. It packs three parts into one line — initialisation, a condition, and an update — such as for (int i = 0; i < 5; i++), which runs the body five times while i goes from 0 to 4.What are the three parts of a for loop?
int i = 0), the condition (checked before each pass, e.g. i < n), and the update (runs after each pass, e.g. i++). Together they control how many times the loop runs.When should I use a for loop instead of a while loop?
for loop when you know how many times to repeat or are counting through a range, because the counter, condition and step sit neatly together. A while loop suits cases where you repeat until some condition changes and the count is not known in advance.Can you have a for loop without a body in C?
for loop can have an empty body if all the work is done in its update part, for example for (i = 0; i < n; sum += a[i], i++);. This is valid but can be harder to read, so it is used sparingly.