CSE 1071 – Problem Solving Using Computers
MODULE 3 — ITERATIVE CONSTRUCTS & ARRAYS
Comprehensive Exam Study Notes
📋 TOPIC SUMMARY
This module covers loops (iterative constructs) in C programming — a fundamental concept for writing
efficient, non-repetitive code. You will learn three types of loops, their syntax, flowcharts, differences, and
how to control them.
Core Topics Covered:
• Loops in C — Why we need them, what problems they solve
• for loop — Entry-controlled; initialization, condition, update in one line
• while loop — Entry-controlled; condition only in syntax, rest manual
• do-while loop — Exit-controlled; body executes at least once
• Infinite loops — When condition is always true; using for/while/do-while
• Nested loops — Loop inside a loop; useful for 2D patterns/arrays
• Loop control statements — break, continue, goto
📖 DETAILED NOTES
1. Why Do We Need Loops?
Without loops, repeating a task means writing the same code many times. For example, printing "Hello" 1000
times would need 1000 printf() lines. Loops solve this with just 3–5 lines.
🔑 KEY DEFINITION: A loop is a control structure that repeats a block of code as long as a given condition is
true.
Real-Life Analogy: Imagine a teacher saying "Write your name 10 times." That's a loop — same task, repeated a
fixed number of times.
2. Types of Loops in C — Overview
Loop Type Control Type Condition Checked Minimum Best Used When
Executions
for Entry-Controlled Before bod y 0 (may not run) Number of
Loop Type Control Type Condition Checked Minimum Best Used When
Executions
iterations is known
while Entry-Controlled Before body 0 (may not run) Number of
iterations is NOT
known
do-while Exit-Controlled After body 1 (always runs Body must run at
once) least once
🧠 MNEMONIC: 'FWD' — For, While, Do-while. Think of it as 'Forward' through your loops!
3. The for Loop
Definition
The for loop is an entry-controlled loop. The condition is checked before the loop body executes. It is used
when the number of iterations is known in advance.
Syntax
for (initialization; condition; updation) {
// body of the loop
}
Parts of the for Loop
Part Purpose Example
Initialization Sets the loop variable to a starting int i = 0
value. Runs ONCE at the beginning.
Condition Checked before each iteration. i<5
Loop runs while TRUE, stops when
FALSE.
Updation Changes the loop variable after i++ or i--
each iteration (increment or
decrement).
Body The code that runs repeatedly as printf("%d", i);
long as condition is true.
Example — Print numbers 1 to 5
#include <stdio.h>
void main() {
for (int i = 0; i < 5; i++) {
printf("%d\n", i + 1);
}
}
// Output: 1 2 3 4 5
How the for Loop Executes (Step-by-Step)
1. Initialization runs: i = 0
2. Condition checked: is i < 5? YES → run body
3. Body executes: prints i+1 = 1
4. Updation: i++ makes i = 1
5. Back to step 2 — repeat until i = 5 (condition becomes FALSE)
6. Loop ends
⚠️IMPORTANT: The for loop can run ZERO times if the condition is false at the very start. Example: for(i=10;
i<5; i++) — never executes.
4. The while Loop
Definition
The while loop is also an entry-controlled loop. The condition is checked before entering the body. Unlike for
loop, only the condition is in the syntax — initialization and updation must be done manually.
Syntax
initialization; // done BEFORE the loop
while (condition) {
// body of the loop
updation; // done INSIDE the loop
}
Example — Print numbers 1 to 6
#include <stdio.h>
void main() {
int i = 0; // initialization
while (i <= 5) { // condition
printf("%d ", i + 1);
i++; // updation
}
}
// Output: 1 2 3 4 5 6
🔑 KEY DIFFERENCE FROM for: In while loop, if you forget i++ inside the body, you get an INFINITE LOOP — a
very common mistake!
5. The do-while Loop
Definition
The do-while loop is an exit-controlled loop. The condition is checked AFTER the body executes. This guarantees
the body runs at least once, regardless of whether the condition is true or false.
Syntax
initialization;
do {
// body of the loop
updation;
} while (condition); // NOTE: semicolon at the end!
Example — Print 0 to 10
#include <stdio.h>
void main() {
int i = 0;
do {
printf("%d ", i);
i++;
} while (i <= 10);
}
// Output: 0 1 2 3 4 5 6 7 8 9 10
⭐ UNIQUE FEATURE: Even if the condition is FALSE initially, do-while runs the body ONCE. Example: if i=100
and condition is i<=10, it still prints 100 once!
⚠️COMMON MISTAKE: Forgetting the semicolon after } while (condition); — this is a syntax error unique to
do-while!
6. Side-by-Side Comparison of All Three Loops
Feature for Loop while Loop do-while Loop
Syntax location of Inside for() In while() After do{} in while()
condition
Condition checked Before execution Before execution After execution
Feature for Loop while Loop do-while Loop
Minimum executions 0 0 1 (guaranteed)
Initialization Inside for() Before loop Before loop
Updation Inside for() Inside body Inside body
Semicolon after No No YES — required!
condition?
Best when iterations Known in advance Unknown Must run at least once
are...
7. Infinite Loops
An infinite loop runs forever because the condition never becomes false. Usually this is a bug, but it can be used
intentionally (e.g., servers waiting for input).
Infinite for Loop
for ( ; ; ) { // blank condition = always true
printf("Runs forever");
}
Infinite while Loop
while (1) { // 1 = true, always
printf("Runs forever");
}
Infinite do-while Loop
do {
printf("Runs forever");
} while (1);
🛑 CAUSE: Infinite loops happen when: (1) Condition is always TRUE, (2) Updation is missing, (3) Wrong
condition logic. Fix using break or correcting the condition.
8. Nested Loops
A nested loop is a loop placed inside another loop. The inner loop completes all its iterations for each single
iteration of the outer loop.
Example — Print i and j values
#include <stdio.h>
void main() {
for (int i = 0; i < 3; i++) { // outer loop: runs 3 times
for (int j = 0; j < 2; j++) { // inner loop: runs 2 times each
printf("i=%d, j=%d\n", i, j);
}
}
}
// Output: i=0,j=0 i=0,j=1 i=1,j=0 i=1,j=1 i=2,j=0 i=2,j=1
📐 FORMULA: Total iterations = Outer loop count × Inner loop count. In the above: 3 × 2 = 6 total iterations.
Common Uses: Printing patterns (stars, triangles), working with 2D arrays, matrix operations.
9. Loop Control Statements
These statements alter the normal flow of a loop.
Statement What it does Where control goes
break Immediately terminates the entire First statement after the loop
loop
continue Skips the rest of the current Back to the loop's
iteration condition/update
goto Jumps to a labeled statement To the specified label
anywhere in code
Example — break, continue, goto in action
#include <stdio.h>
void main() {
// BREAK example: stops at i=3
for (int i = 0; i < 5; i++) {
if (i == 3) break;
printf("%d ", i); // Output: 0 1 2
}
// CONTINUE example: skips i=3
for (int i = 0; i < 5; i++) {
if (i == 3) continue;
printf("%d ", i); // Output: 0 1 2 4
}
// GOTO example: jumps to label when i=3
for (int i = 0; i < 5; i++) {
if (i == 3) goto skip;
printf("%d ", i);
}
skip:
printf("\nJumped to skip label when i equals 3.");
// Output: 0 1 2 then jumps
}
🧠 REMEMBER: break = STOP the loop entirely. continue = SKIP this round, keep looping. goto = TELEPORT to
label (use sparingly — makes code hard to read).
🔑 IMPORTANT KEYWORDS & DEFINITIONS
Term Definition
Loop A control structure that repeats a block of code while
a condition is true
Iteration One complete execution of the loop body
Entry-controlled loop Condition is checked BEFORE the loop body runs (for,
while)
Exit-controlled loop Condition is checked AFTER the loop body runs (do-
while)
Initialization Setting the loop variable to its starting value
Condition Boolean expression evaluated to decide if loop
continues
Updation / Increment Modifying the loop variable after each iteration
Infinite loop A loop that never terminates because condition stays
true
Nested loop A loop placed inside another loop
break Exits the current loop immediately
continue Skips remaining body of current iteration, moves to
next
goto Unconditional jump to a labeled statement
Label A named point in code used as a goto target (e.g.,
skip:)
⚡ QUICK REVISION SHEET
Loop Syntax at a Glance
Loop Syntax Template
for for(init; cond; update) { body }
while init; while(cond) { body; update; }
do-while init; do { body; update; } while(cond); ← semicolon!
Infinite for for( ; ; ) { body }
Infinite while while(1) { body }
Key Facts to Remember
• for loop: 3 parts in header — init, condition, update. Entry-controlled.
• while loop: Only condition in header. Init and update are manual. Entry-controlled.
• do-while loop: Condition at END. Always executes at least once. Exit-controlled.
• Semicolon rule: ONLY do-while requires a semicolon after the closing condition.
• break vs continue: break EXITS loop; continue SKIPS current iteration.
• Nested loops total iterations: Outer × Inner
• Infinite loop: Use "for(;;)" or "while(1)" — condition always true
📌 MNEMONIC FOR LOOP TYPES: 'For FEW things (known count)' 'While WAITING (unknown count)' 'Do
DEFINITELY once (always runs once)'
🎯 EXAM-ORIENTED QUESTIONS
Frequently Asked Exam Questions
Short Answer / Definition Type
7. What is a loop? Why are loops used in C programming?
8. Differentiate between entry-controlled and exit-controlled loops. Give examples.
9. What is the difference between for and while loop?
10. Why does do-while loop execute at least once even if the condition is false?
11. What is an infinite loop? How can it be created using the while loop?
12. What is a nested loop? What is the formula for total iterations?
13. Differentiate between break and continue with examples.
14. What is the role of the goto statement? Why is its use discouraged?
Program Writing Questions
15. Write a C program using a for loop to print numbers from 1 to n.
16. Write a C program to print multiplication table of a given number using while loop.
17. Write a C program using do-while to keep accepting numbers until user enters 0.
18. Write a C program using nested loops to print a right-angled triangle of stars.
19. Write a C program that uses break to exit a loop when a specific value is found.
20. Write a C program using continue to print all even numbers from 1 to 20.
Trace/Output Questions
21. What is the output of: for(int i=2; i<=10; i+=3) printf("%d ", i);
22. What is the output of: for(int i=5; i>0; i--) printf("%d ", i);
23. How many times does this execute? for(i=0; i<4; i++) for(j=0; j<3; j++)
Common Mistakes Students Make
Mistake Wrong Code Correct Code / Fix
Missing semicolon in do-while } while(i<5) } while(i<5);
Missing updation in while loop while(i<5){ printf(i); } while(i<5){ printf(i); i++; }
Off-by-one error for(i=1; i<5; i++) — only 4 for(i=1; i<=5; i++) for 5 iterations
iterations
Using = instead of == in condition while(i=5) — infinite loop! while(i==5)
Updating variable outside loop in for(i=0;i<5;) { ... } i++; for(i=0;i<5;i++) { ... }
for
break exits only innermost loop Expects break to exit all nested Use flags or goto for multi-level
loops exits
💻 BONUS PRACTICE PROGRAMS
These programs from the revision list use loops and cover common exam scenarios:
Fibonacci Series (for loop)
#include <stdio.h>
void main() {
int n, a=0, b=1, c;
printf("Enter n: "); scanf("%d", &n);
printf("%d %d ", a, b);
for (int i = 2; i < n; i++) {
c = a + b;
printf("%d ", c);
a = b; b = c;
}
}
// Input: 7 | Output: 0 1 1 2 3 5 8
Factorial of a Number (for loop)
#include <stdio.h>
void main() {
int n; long fact = 1;
printf("Enter n: "); scanf("%d", &n);
for (int i = 1; i <= n; i++)
fact = fact * i;
printf("Factorial = %ld", fact);
}
// Input: 5 | Output: Factorial = 120
Reverse a Number (while loop)
#include <stdio.h>
void main() {
int n, rev=0, rem;
printf("Enter number: "); scanf("%d", &n);
while (n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
printf("Reversed = %d", rev);
}
// Input: 1234 | Output: Reversed = 4321
Check Prime Number (for loop + break)
#include <stdio.h>
void main() {
int n, flag=0;
printf("Enter number: "); scanf("%d", &n);
for (int i = 2; i <= n/2; i++) {
if (n % i == 0) { flag = 1; break; }
}
if (flag == 0) printf("Prime");
else printf("Not Prime");
}
📚 STUDY TIPS FOR EXAM SUCCESS
• Practice tracing: Manually trace loop output on paper — this is the #1 exam skill.
• Memorize syntax differences: Especially the semicolon in do-while and the 3-part for header.
• Know when to use which loop: Known count → for | Unknown count → while | At least once → do-
while
• Watch for infinite loops: Always check that your condition eventually becomes false.
• Nested loop output: Trace inner loop completely for every outer loop iteration.
All the best for your exams! 🎓