In Java, loops are used to repeatedly execute a block of code until a certain condition is met. They provide a way to perform repetitive tasks efficiently and with less code. Java provides three types of loops: for
, while
, and do-while
. Let's explain each of them:
-
for
Loop: Thefor
loop is used when the number of iterations is known beforehand or when iterating over a collection/array. It consists of three parts: initialization, condition, and iteration expression.for (initialization; condition; iteration) { // Code to be executed in each iteration }
The
initialization
statement is executed only once at the beginning. Thecondition
is checked before each iteration, and if it evaluates totrue
, the code block is executed. After each iteration, theiteration
statement is executed, and then the condition is checked again. The loop continues until the condition evaluates tofalse
.
-
while
Loop: Thewhile
loop is used when the number of iterations is not known beforehand and depends on a condition. It repeatedly executes the code block as long as the condition istrue
.while (condition) { // Code to be executed in each iteration }
The
condition
is evaluated before each iteration. If it evaluates totrue
, the code block is executed. If it evaluates tofalse
initially, the code block is never executed.
-
do-while
Loop: Thedo-while
loop is similar to thewhile
loop, but the condition is checked at the end of the loop, ensuring that the code block is executed at least once.do { // Code to be executed in each iteration } while (condition);
The code block is executed first, and then the
condition
is checked. If the condition evaluates totrue
, the loop continues for subsequent iterations. If the condition evaluates tofalse
, the loop terminates.
Loop control statements like break
and continue
can be used to control the flow of execution within loops. break
is used to exit the loop completely, while continue
skips the rest of the current iteration and proceeds to the next iteration.
Here's an example that demonstrates the use of different loops:
for (int i = 1; i <= 5; i++) {
System.out.println("Value of i: " + i);
}
int j = 1;
while (j <= 5) {
System.out.println("Value of j: " + j);
j++;
}
int k = 1;
do {
System.out.println("Value of k: " + k);
k++;
} while (k <= 5);
In this example, each loop prints the values of a variable (i
, j
, and k
) from 1 to 5. The output will display:
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5
Value of j: 1
Value of j: 2
Value of j: 3
Value of j: 4
Value of j: 5
Value of k: 1
Value of k: 2
Value of k: 3
Value of k: 4
Value of k: 5
Loops provide a powerful mechanism for automating repetitive tasks and iterating over collections, making them an essential part of programming in Java.
- Java Program to Display Alphabets (A to Z) using loop
A,B,C.....
- Java Program to Count Number of Digits in an Integer
Enter Number : 9876
your digits count : 4
- Write a program that prompts the user to input an integer and then outputs the number with the digits reversed. For example, if the input is 12345, the output should be 54321.
Enter Number : 9876
your Recerse Number : 6789
- Program to print half pyramid using alphabets
A
B B
C C C
D D D D
E E E E E
- Write a program that reads a set of integers, and then prints the sum of the even and odd integers.(noote : also find sum of the total number : )
Enter Start Loop Number : 1
Emter End Loop Number : 10
Sum of the total Number = 55
Sum of the Even Number = 30
Sum of the Odd Number = 25
- Write a program to print table :
Enter Print Table : 2
2 x 1 = 2
2 x 2 = 4
.
.
2 x 10 = 20