Page 1
Lecture 8: © coderMind
Iteration Statements (Loops) in Java
Magic! ✨
📌 Lecture Overview
• What are Iteration Statements? • For Loop & Internal Working
• Why Loops are Needed • Infinite For Loops
• While Loop & Infinite Loops • Multiple Variables & Conditions
• Do-While Loop • Nested Loops
• While vs Do-While • Loop Best Practices
d
• Menu Driven Programs • Interview Questions
1️⃣ What are Iteration Statements?
i n
M
Iteration Statements are used to execute a block of code repeatedly . Commonly known
r
as: Loops
de
Why Do We Need Loops?
Imagine you want to print numbers from 1 to 10.
c o
Without Loops With Loops (Solution!)
[Link](1); for(int i=1; i<=10; i++) {
[Link](2); [Link](i);
[Link](3); }
...
❌ Repetitive
❌ Difficult to maintain
❌ Inefficient
Page 2
2️⃣ Types of Loops in Java © coderMind
Java provides three main loops:
Iteration Statements
│
├── While Loop
├── Do-While Loop
└── For Loop
3️⃣ While Loop
d
The simplest loop in Java.
i n
Syntax
Flow of While Loop
while(condition) { Condition Check
M
// code
↓
}
r
True ?
e
Important Rule: Condition must return
↓
d
true or false .
Execute Loop Body
o
↓
c
Back To Condition
False ?
↓
Exit Loop
int i = 1;
while(i <= 10) {
[Link](i);
i++;
Example: Print 1 to 10 }
Execution Breakdown:
• Iteration 1: i=1 → 1 <= 10 (True) → Print 1 → i becomes 2.
• Iteration 2: i=2 → 2 <= 10 (True) → Print 2 → i becomes 3.
• Continues until...
• i=11 → 11 <= 10 (False) → Loop terminates.
Page 3
4️⃣ Infinite While Loop © coderMind
If the loop condition never becomes false , it runs forever. Warning! ⚠️
int i = 1;
Problem:
while(i <= 10) {
[Link](i); i never changes!
} Condition is always: true
Result: Infinite Loop (1, 1, 1, 1... Forever)
Why? Because i++; is missing.
d
5️⃣ Reverse Printing Using While Loop
n
Print: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
i
int i = 10;
while(i >= 1) {
M
[Link](i);
r
i--;
e
}
d
6️⃣ Using Increment Operator Inside Condition
o
while(i++ < 10) {
c
[Link](i);
}
Important Observation Trap! 🪤
Postfix increment (i++) executes after comparison.
This completely changes the output pattern.
Interview Point:
Always understand i++ vs ++i inside loop conditions!
Page 4
7️⃣ Do-While Loop © coderMind
A variation of While Loop. Executes first, checks later.
Syntax
Flow of Do-While
do { Execute Body
// code
} while(condition);
↓
Check Condition
↓
True ?
d
↓
n
Repeat
i
False ?
M
↓
r
Exit Loop
de
int i = 1;
do {
o
[Link](i);
c
i++;
Example: Print 1 to 10 } while(i <= 10);
8️⃣ Difference Between While and Do-While
V. IMP! ⭐
Suppose: int i = 11;
While Loop Do-While Loop
while(i <= 10) { do { [Link](i); }
[Link](i); } while(i <= 10);
Output: Nothing Output: 11
Condition fails immediately. Body executes first, then checks.
While: May execute 0 times
Do-While: Executes AT LEAST once!
Page 5
9️⃣ Real-World Use of Do-While © coderMind
Best Example: Menu Driven Programs
Why Do-While?
Example Menu:
Because the menu must appear at least
1. Play Game
once to the user.
2. Load Game
3. Exit
Flow:
d
Show Menu → User Selects Option → Perform Action → Show Menu Again (until Exit
🎮
n
selected).
i
🔟 For Loop
M
The most commonly used loop in Java.
r
Syntax & Example
de
for(initialization; condition; for(int i=1; i<=10; i++)
update) {
o
{ [Link](i);
c
// code }
}
1️⃣1️⃣ Anatomy of For Loop
A For Loop has three parts:
1. Initialization 2. Condition 3. Update
int i = 1; i <= 10; i++
Runs only once. Checked every iteration. Runs after each
iteration.
Init → Condition → True? → Execute Body → Update → Condition...
Page 6
1️⃣2️⃣ Reverse Counting Using For Loop © coderMind
for(int i=10; i>=1; i--) {
[Link](i);
}
Output: 10, 9, 8... 1
1️⃣3️⃣ Infinite For Loop
Example 1: Forever! ♾️
for(;;) {
[Link]("Hello");
d
}
i n
Why? Because no condition exists to stop the loop.
This is a perfectly valid Java statement!
M
Other Infinite Variations:
r
for(int i=1; ; i++) { } for(; true; ) { }
de
1️⃣4️⃣ Optional Parts of For Loop
o
All three
Intervisections
ew! 🎤 (Init, Condition, Update) are optional .
c
• for(;;) {} → Valid.
• for(int i=1;;) {} → Valid.
• for(;i<10;) {} → Valid.
Q: Which parts of For Loop are mandatory?
Answer: NONE! All are optional.
Page 7
1️⃣5️⃣ Multiple Variables in For Loop © coderMind
Java allows declaring and updating multiple variables simultaneously using comma
separation.
Example:
for(int i=1, j=1; i<=5; i++, j+=2) {
[Link](i*j);
}
Execution: Benefits
d
• 1 × 1 = 1 Allows multiple counters simultaneously.
n
• 2 × 3 = 6 Very useful in complex algorithmic
i
• 3 × 5 = 15 problems!
• 4 × 7 = 28
M
• 5 × 9 = 45
er
1️⃣6️⃣ Multiple Conditions
d
Possible using logical operators (like && or ||).
c o
for(int i=1, j=1; i<=10 && j<=5; i++, j+=2) { }
Loop continues ONLY when: i<=10 AND j<=5 are BOTH true.
1️⃣7️⃣ Boolean Controlled Loop
Condition can be any Boolean expression .
boolean flag = true;
for(int i=1; flag; i++) {
if(i==5) flag=false;
}
Loop stops when: flag becomes false.
Important Concept: Loop conditions are NOT limited to numbers!
Page 8
1️⃣8️⃣ Why We Usually Use "int" in Loops © coderMind
Possible integer data types: byte, short, int, long.
Most developers use: int
Reason: Type Promotion
Even if you write byte i = 1; or short i = 1;, Java internally promotes
calculations to int . 🤔
Therefore, defining the counter as int natively is standard and preferred.
d
1️⃣9️⃣ Nested Loops
n
A loop inside another loop.
i
for(...) {
for(...) {
M
// code
r
}
e
} Example: Star Pattern
d
for(int i=1; i<=5; i++) { Working Principle:
for(int j=1; j<=5; j++) {
o
Outer Loop (i): Controls Rows
[Link]("* ");
c
} Inner Loop (j): Controls Columns
[Link](); Total Iterations = 5 × 5 = 25
}
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Page 9
2️⃣0️⃣ Loop Comparison © coderMind
Feature While Do-While For
Condition First ✅ ❌ ✅
Executes At Least Once ❌ ✅ ❌
Best For Known Count ❌ ❌ ✅
Best For Menu Programs ❌ ✅ ❌
Most Common ⚠️ ❌ ✅
d
🎯 Interview Questions
Q1. What is an Iteration Statement?
i n
Q2. Types of Loops in Java?
M
Ans: A statement that repeatedly Ans: While, Do-While, For.
r
executes a block of code.
de
Q3. Difference Between While and Q4. Best loop for Menu Programs?
c o
Do-While? Ans: Do-While Loop.
Ans: While checks condition first (may
run 0 times). Do-while runs AT LEAST
once.
Q5. Can a For Loop Become Infinite?
Ans: Yes. for(;;) {}
Q&A Time! ⏳
Page 10
© coderMind
Q6. Which Parts of For Loop Are Q7. Multiple Variables in For Loop?
Optional? Ans: Yes, using comma separation.
Ans: ALL are optional (Init, Condition,
Update).
Q8. What is a Nested Loop? Q9. Why Is int Preferred in Loops?
Ans: A loop inside another loop. Ans: Java promotes smaller integer
types to int during calculations.
d
Q10. Most Frequently Used Loop?
i n
Ans: For Loop.
M
🚀 Lecture 8 Final Summary
r
• ✅ While checks condition first.
e
• ✅ Do-While executes at least once.
d
• ✅ For Loop is the most commonly used loop.
o
• ✅ Infinite loops occur when conditions never become false.
c
• ✅ Nested Loops enable matrix and pattern problems.
💡 Memory Trick Flow
While Loop Do-While Loop For Loop
Check → Execute Execute → Check Init → Cond → Exec →
Update
Mastering loops is essential for arrays, strings, patterns, and DSA! 🚀
Lecture 8 Complete! 🎉