Table of Contents
- Introduction to Loops
- The
for
Loop - The
while
Loop - The
do-while
Loop - Loop Control Statements:
break
andcontinue
- Infinite Loops
- Nested Loops
- Real-World Examples
- Summary and What’s Next
1. Introduction to Loops
Loops are used in programming to execute a block of code repeatedly until a certain condition is met. Java supports three main types of loops:
for
loopwhile
loopdo-while
loop
Each loop type has its specific use case, but all can be used to achieve similar outcomes. Loops help reduce redundancy, automate repetitive tasks, and improve code readability.
2. The for
Loop
The for
loop is best when you know in advance how many times you want to iterate.
Syntax:
for (initialization; condition; update) {
// code block to be executed
}
Example:
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
Explanation:
- Initialization: Runs once at the beginning, e.g.,
int i = 1
. - Condition: Checked before each iteration; loop runs if it’s true.
- Update: Executed after each iteration, e.g.,
i++
.
3. The while
Loop
The while
loop is used when the number of iterations is not known in advance. It checks the condition before the loop body.
Syntax:
while (condition) {
// code block to be executed
}
Example:
int i = 1;
while (i <= 5) {
System.out.println("Count: " + i);
i++;
}
The loop continues as long as i <= 5
is true.
4. The do-while
Loop
The do-while
loop is similar to the while
loop, but it guarantees at least one execution of the loop body, since the condition is evaluated after the loop body.
Syntax:
do {
// code block to be executed
} while (condition);
Example:
int i = 1;
do {
System.out.println("Running: " + i);
i++;
} while (i <= 5);
This loop runs at least once, even if the condition is initially false.
5. Loop Control Statements: break
and continue
The break
Statement
Used to exit the loop prematurely when a certain condition is met.
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
This loop prints numbers 1 to 4 and then exits when i
becomes 5.
The continue
Statement
Skips the current iteration and jumps to the next one.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
This loop prints 1, 2, 4, 5 (skips 3).
6. Infinite Loops
An infinite loop keeps running forever unless forcibly terminated. Can be useful in event-driven or server applications.
Example:
while (true) {
// keep listening for connections
}
Use infinite loops with care, ensuring there’s a break condition or manual stop mechanism.
7. Nested Loops
You can place a loop inside another loop to perform complex iterations like matrix processing or pattern printing.
Example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
System.out.println("i = " + i + ", j = " + j);
}
}
8. Real-World Examples
Printing a Table of a Number
int number = 7;
for (int i = 1; i <= 10; i++) {
System.out.println(number + " * " + i + " = " + (number * i));
}
Counting User Inputs Until a Sentinel Value
Scanner scanner = new Scanner(System.in);
int input;
do {
System.out.print("Enter number (0 to quit): ");
input = scanner.nextInt();
} while (input != 0);
Searching for an Element
int[] arr = {3, 5, 7, 9};
int target = 7;
boolean found = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
found = true;
break;
}
}
System.out.println("Element found: " + found);
9. Summary and What’s Next
In this module, we learned:
- The structure and use cases of
for
,while
, anddo-while
loops - How to control loop flow using
break
andcontinue
- Common looping patterns including nested loops and infinite loops
Understanding loops is essential to solving repetitive tasks and optimizing code efficiency.