Looping Statement:
Looping statements in Java are used to repeat a block of code multiple times. They allow you to automate repetitive tasks and iterate over collections of data. Java provides several looping statements, including the for
loop, while
loop, and do-while
loop.
For Loop:
The for
loop in Java is a control flow statement that allows you to execute a block of code repeatedly for a specified number of times. It consists of three parts: initialization, condition, and increment/decrement. Here's the syntax of the for
loop:
javafor (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}
The initialization
step is executed once at the beginning and is used to initialize the loop control variable. The condition
is checked before each iteration, and if it evaluates to true
, the loop continues. If it evaluates to false
, the loop terminates. After each iteration, the increment/decrement
step is executed, updating the loop control variable.
Here's an example that demonstrates the usage of a for
loop to print numbers from 1 to 5:
javafor (int i = 1; i <= 5; i++) {
System.out.println(i);
}
Output:
1 2 3 4 5
In the example above, the loop control variable i
is initialized to 1. The loop continues as long as i
is less than or equal to 5. After each iteration, i
is incremented by 1 (i++
). The loop prints the value of i
and repeats until the condition becomes false when i
reaches 6.
You can customize the initialization, condition, and increment/decrement according to your specific requirements in the for
loop.
While Loop:
The while
loop in Java is a control flow statement that allows you to repeatedly execute a block of code as long as a given condition is true. Here's the syntax of the while
loop:
javawhile (condition) {
// Code to be executed repeatedly
}
The condition
is evaluated before each iteration. If the condition is true, the code block inside the loop is executed. If the condition is false, the loop is terminated, and the program continues with the next statement after the loop.
Here's an example that demonstrates the usage of a while
loop to print even numbers from 2 to 10:
javaint number = 2;
while (number <= 10) {
System.out.println(number);
number += 2;
}
Output:
2 4 6 8 10
In the example above, the number
variable is initialized to 2. The loop continues as long as number
is less than or equal to 10. Inside the loop, the current value of number
is printed, and then number
is incremented by 2 (number += 2
). This process repeats until number
reaches 12, at which point the condition becomes false, and the loop terminates.
Remember to ensure that your loop has an exit condition, or it could potentially result in an infinite loop, causing your program to run indefinitely.
You can modify the condition and the code block inside the while
loop to suit your specific needs.
Do While Loop:
The do-while
loop in Java is a control flow statement that executes a block of code at least once and then repeatedly executes it as long as a given condition is true. Here's the syntax of the do-while
loop:
javado {
// Code to be executed repeatedly
} while (condition);
The block of code inside the do
statement is executed first, and then the condition
is evaluated. If the condition is true, the loop continues and the block of code is executed again. If the condition is false, the loop is terminated, and the program continues with the next statement after the loop.
Here's an example that demonstrates the usage of a do-while
loop to print numbers from 1 to 5:
javaint i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Output:
1 2 3 4 5
In the example above, the loop first executes the code block inside the do
statement, which prints the value of i
. After each iteration, the i
variable is incremented by 1 (i++
). The loop continues as long as i
is less than or equal to 5. Once i
becomes 6, the condition becomes false, and the loop terminates.
One key difference between the do-while
loop and the while
loop is that the do-while
loop always executes the code block at least once, regardless of the condition. This can be useful when you want to ensure that a certain piece of code runs before checking the condition.
Feel free to customize the code block and the condition inside the do-while
loop to fit your specific requirements.
0 Comments