Day 9: Java Loops - for, while & do-while
Master Java loop structures for repetitive operations: for loop, while loop, and do-while loop with practical examples
Today's Learning Objectives
Loop Fundamentals
- • Understanding loop concepts and purpose
- • 4 types of loops in Java
- • Loop syntax and structure
- • Infinite loops and how to avoid them
Practical Applications
- • Number sequences and patterns
- • Conditional loops with exclusions
- • Increment and decrement operations
- • Real-world loop examples
Introduction to Loops in Java
What are Loops?
If you want to repeat a block of statements for a specific amount of time, then we use loop concepts.
4 Types of Loops in Java
- 1. for loop - Known number of iterations
- 2. while loop - Condition-based repetition
- 3. do-while loop - Execute at least once
- 4. enhanced for loop - Array/collection iteration
1. For Loop
For Loop Syntax
Use for loop when you want to repeat a block of statements for a specific amount of time.
Syntax Structure
// statements to repeat
}
Basic Example: Print 1 to 5
System.out.println(i); // Output: 1 2 3 4 5
}
Reverse Order: Print 10 to 5
System.out.println(i); // Output: 10 9 8 7 6 5
}
Custom Increment: Print 1, 3 (i = i + 2)
System.out.println(i); // Output: 1 3
}
For Loop Edge Cases
⚠️ Missing Increment (Infinite Loop)
System.out.println(i); // Prints 1 infinitely
}
Without increment/decrement, the loop runs infinitely!
⚠️ Missing Condition (Infinite Loop)
System.out.println(i); // Prints 1, 2, 3... infinitely
}
Without end condition, the loop never stops!
⚠️ False Initial Condition
System.out.println(i); // No output
}
If initial condition is false, loop doesn't execute at all.
For Loop with Conditional Logic
Print 1 to 5 except 3
if(i != 3) {
System.out.println(i); // Output: 1 2 4 5
}
}
Print 1 to 5 except 2 and 3
if(i != 2 && i != 3) {
System.out.println(i); // Output: 1 4 5
}
}
Print 30 to 40 except 34, 38, and 39
if(i != 34 && i != 38 && i != 39) {
System.out.println(i);
}
}
2. While Loop
While Loop Syntax
While loop repeats a block of statements while the condition is true.
Syntax Structure
datatype variable = startValue;
while(endValue with condition) {
// statements
increment/decrement;
}
Basic Example: Print 1 to 5
while(i <= 5) {
System.out.println(i); // Output: 1 2 3 4 5
i++;
}
Reverse Order: Print 30 to 20
while(i >= 20) {
System.out.println(i);
i--;
}
With Conditions: Print 1 to 10 except 4 and 7
while(i <= 10) {
if(i != 4 && i != 7) {
System.out.println(i);
}
i++;
}
3. Do-While Loop
Do-While Loop Syntax
Do-while loop repeats a block of statements while condition is true, but it will execute at least once even if condition is false.
Syntax Structure
datatype variable = startValue;
do {
// Java statements
increment/decrement;
} while(endValue with condition);
Basic Example: Print 1 to 5
do {
System.out.println(i); // Output: 1 2 3 4 5
i++;
} while(i <= 5);
Reverse Order: Print 30 to 20
do {
System.out.println(i);
i--;
} while(i >= 20);
With Conditions: Print 30 to 20 except 25
do {
if(i != 25) {
System.out.println(i);
}
i--;
} while(i >= 20);
🔑 Key Difference: do-while vs while
while loop
Checks condition first, then executes. May not execute at all if condition is false initially.
do-while loop
Executes first, then checks condition. Always executes at least once, even if condition is false.
Day 9 Knowledge Check
What happens if you forget the increment/decrement in a for loop?
Key Takeaways
Loop Types & Usage
- for loop: Known iterations, compact syntax
- while loop: Condition-based, check first
- do-while loop: Execute first, check later
- enhanced for: Arrays/collections (Day 10+)
Loop Components
- Initialization: Starting value (int i = 1)
- Condition: When to continue (i <= 5)
- Update: Change per iteration (i++, i--)
- Body: Code to repeat
Common Patterns
- Ascending: for(int i=1; i<=n; i++)
- Descending: for(int i=n; i>=1; i--)
- Skip Values: Use if(i != value) inside loop
- Custom Step: i = i + 2, i = i + 3
Best Practices
- Avoid Infinite Loops: Always include proper increment/decrement
- Clear Conditions: Use meaningful comparison operators
- Readable Code: Choose appropriate loop type for the task
- Test Edge Cases: Verify loop behavior with boundary values
💡 Practice Assignment: Print 20 to 40 values except 25 and 35 using for loop. Master these loop concepts as they're fundamental for Day 10's Selenium automation!