LESSON 15
.
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define loops in Java programming.
- Use for loops in Java applications.
- Use while loops in Java applications.
- Use do-while loops in Java applications.
- Apply loops to automate repetitive tasks.
3.2 Overview
Java applications often need to repeat instructions multiple times during execution. Loops allow programs to automate repetitive tasks efficiently and reduce unnecessary code duplication.
This lesson introduces learners to loops in Java and explains how repetition structures are used to process data, automate calculations, and control repetitive execution in software applications.
Loops are commonly used in:
- banking systems,
- inventory systems,
- gaming applications,
- reporting systems,
- and enterprise software.
Understanding loops is essential because repetition and automation are fundamental concepts in programming and software development.
KT1501 — Introduction to Loops
A loop is a programming structure used to repeatedly execute a block of code while a condition remains true.
Loops automate repetitive processing and simplify program logic.
Importance of Loops
Loops help developers:
- automate repetitive tasks,
- process collections of data,
- reduce duplicated code,
- and improve application efficiency.
KT1502 — The for Loop
The for loop is commonly used when the number of repetitions is known.
Syntax
for(initialization; condition; update) {
// code
}
Example
public class Main {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}
Output
1
2
3
4
5
Explanation
| Component | Purpose |
|---|---|
| int i = 1 | Initialization |
| i <= 5 | Condition |
| i++ | Increment/update |
Importance of the for Loop
The for loop is commonly used for:
- counting,
- processing arrays,
- calculations,
- and repeated operations.
KT1503 — The while Loop
The while loop executes repeatedly while a condition remains true.
Syntax
while(condition) {
// code
}
Example
public class Main {
public static void main(String[] args) {
int count = 1;
while(count <= 3) {
System.out.println(count);
count++;
}
}
}
Output
1
2
3
Importance of the while Loop
The while loop is useful when:
- repetitions are unknown,
- conditions control execution,
- and continuous processing is required.
KT1504 — The do-while Loop
The do-while loop executes code at least once before checking the condition.
Syntax
do {
// code
} while(condition);
Example
public class Main {
public static void main(String[] args) {
int number = 1;
do {
System.out.println(number);
number++;
} while(number <= 3);
}
}
Output
1
2
3
Difference Between while and do-while
| while Loop | do-while Loop |
|---|---|
| Checks condition first | Executes first before checking |
| May never execute | Executes at least once |
KT1505 — Nested Loops
A nested loop is a loop placed inside another loop.
Nested loops are used when multiple levels of repetition are required.
Example
public class Main {
public static void main(String[] args) {
for(int i = 1; i <= 2; i++) {
for(int j = 1; j <= 3; j++) {
System.out.println(i + " " + j);
}
}
}
}
Output
1 1
1 2
1 3
2 1
2 2
2 3
Importance of Nested Loops
Nested loops are commonly used in:
- matrix processing,
- gaming applications,
- tables,
- and complex data processing.
KT1506 — Loop Control Statements
Java provides loop control statements to manage loop execution.
break Statement
The break statement stops loop execution immediately.
Example
public class Main {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
if(i == 3) {
break;
}
System.out.println(i);
}
}
}
Output
1
2
continue Statement
The continue statement skips the current iteration.
Example
public class Main {
public static void main(String[] args) {
for(int i = 1; i <= 5; i++) {
if(i == 3) {
continue;
}
System.out.println(i);
}
}
}
Output
1
2
4
5
Importance of Loop Control Statements
Loop control statements help developers:
- manage execution flow,
- skip unnecessary processing,
- and optimize loops.
KT1507 — Real-World Use of Loops
Loops are used in:
- ATM systems,
- reporting systems,
- payroll systems,
- inventory applications,
- and enterprise software.
Examples:
- processing transactions,
- generating reports,
- reading records,
- and repeating calculations.
Loops support automation and repetitive processing in software applications.
3.5 Key Notes / Summary
- Loops repeatedly execute blocks of code.
- Java supports for, while, and do-while loops.
- The for loop is useful for controlled repetition.
- The while loop executes while conditions remain true.
- The do-while loop executes at least once.
- Nested loops support multiple levels of repetition.
- break and continue control loop execution.