Java - Loops
Loops allow you to repeatedly execute a block of code based on a condition. Loops are essential for automating repetitive tasks and iterating over collections of data.
Java provides three types of loops: while loop, do-while loop, and for loop
while Loop
The while loop executes a block of code repeatedly as long as a specified condition is true. The condition is evaluated before each iteration, and if it is true, the loop body is executed. The syntax for the while loop is as follows:
while (condition) {
// Code to be executed
}
Here's an example that demonstrates the usage of the while loop:
class WhileLoop{
public static void main(String[] args){
int count = 1;
while (count <= 5) {
System.out.println("Count: " + count);
count++;
}
}
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
In this example, the while loop continues to execute as long as the condition count <= 5 is true. The count variable is incremented by 1 in each iteration, and the value is printed.
do-while Loop
The do-while loop is similar to the while loop, but the condition is evaluated after each iteration. This means that the loop body is executed at least once before checking the condition. The syntax for the do-while loop is as follows:
do {
// Code to be executed
} while (condition);
Here's an example that demonstrates the usage of the do-while loop:
class DoWhileLoop{
public static void main(String[] args){
int count = 1;
do {
System.out.println("Count: " + count);
count++;
} while (count <= 5);
}
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
In this example, the do-while loop executes the code block first and then checks the condition count <= 5. If the condition is true, the loop continues to execute. Otherwise, the loop terminates.
for Loop
The for loop is used when you know the number of iterations in advance. It allows you to specify the initialization, condition, and increment/decrement in a single line. The syntax for the for loop is as follows:
for (initialization; condition; increment/decrement) {
// Code to be executed
}
Here's an example that demonstrates the usage of the for loop:
class ForLoop{
public static void main(String[] args){
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
}
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
In this example, the for loop initializes the variable i with the value 1, checks the condition i <= 5, and increments i by 1 in each iteration.
Loop Control Statements
Java provides loop control statements that allow you to control the flow of loops:
- break statement: Terminates the loop and transfers control to the statement immediately following the loop.
- continue statement: Skips the current iteration and proceeds to the next iteration of the loop.
Here's an example that demonstrates the usage of loop control statements:
class LoopControl{
public static void main(String[] args){
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip iteration if i is 3
}
System.out.println("Count: " + i);
if (i == 4) {
break; // Terminate the loop if i is 4
}
}
}
}
Output:
Count: 1
Count: 2
Count: 4
In this example, the continue statement is used to skip the iteration when i is equal to 3. The break statement is used to terminate the loop when i is equal to 4.
Nested Loops
You can also nest loops within each other to create more complex looping structures. This allows you to iterate over multiple dimensions or perform iterative tasks within larger iterations.
Here's an example of nested loops:
class NestedLoop{
public static void main(String[] args){
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println("i: " + i + ", j: " + j);
}
}
}
}
Output:
i: 1, j: 1
i: 1, j: 2
i: 1, j: 3
i: 2, j: 1
i: 2, j: 2
i: 2, j: 3
i: 3, j: 1
i: 3, j: 2
i: 3, j: 3
In this example, the outer loop iterates over the values of i, and for each iteration of the outer loop, the inner loop iterates over the values of j. This results in a complete set of combinations of i and j being printed.
Loops are powerful constructs that allow you to automate repetitive tasks and iterate over collections of data. They provide flexibility and control over the flow of your program. Understanding how to use while loops, do-while loops, for loops, and loop control statements is crucial for writing efficient and effective Java programs.
Basic Topics
- Introduction to Java
- Data Types and Variables
- User Input
- Conditional Statements
- Loops
- Methods
- Arrays