types of loops in java with example

Evelyn 99 Published: 11/26/2024

types of loops in java with example

I'll respond in English as per your request.

Java is a powerful programming language that provides various control structures, including different types of loops. Loops allow you to execute a block of code repeatedly for a specified number of times or until a certain condition is met. Here are the main types of loops in Java, along with examples:

For Loop: A For loop is used to iterate over an array, collection, or a range of numbers.

Example:

int[] scores = {90, 80, 70, 60};

for (int score : scores) {

System.out.println("Score: " + score);

}

This code will print the scores one by one. The For loop iterates over the scores array and assigns each element to the score variable.

While Loop: A While loop is used when you don't know how many times you'll need to iterate, but you do know the condition for stopping.

Example:

int i = 0;

while (i < 5) {

System.out.println("Iteration: " + i);

i++;

}

This code will print the iteration numbers from 0 to 4. The While loop continues as long as i is less than 5, and increments i by 1 each time.

Do-While Loop: A Do-While loop is similar to a While loop, but it always executes the body at least once.

Example:

int i = 0;

do {

System.out.println("Iteration: " + i);

i++;

} while (i < 5);

This code will also print the iteration numbers from 0 to 4. The Do-While loop is similar to a While loop, but it always executes the body at least once.

Nested Loop: A Nested loop is when you have multiple loops inside each other.

Example:

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 4; j++) {

System.out.println("i: " + i + ", j: " + j);

}

}

This code will print all combinations of i and j values, from 0 to 2 (for i) and 0 to 3 (for j). The inner loop runs for each iteration of the outer loop.

Labelled Loop: A Labelled loop is when you give a name to a loop using the label: syntax.

Example:

outer: for (int i = 0; i < 3; i++) {

for (int j = 0; j < 4; j++) {

if (i == 2) {

break outer;

}

System.out.println("i: " + i + ", j: " + j);

}

}

This code will print all combinations of i and j values, but it will exit the loop as soon as i reaches 2.

These are the main types of loops in Java. Understanding how to use them effectively is crucial for writing efficient and readable code.

types of loops in java with example

I'll respond in English for this one!

Java has several types of loops that can be used to execute a block of code repeatedly. These include:

For Loop: The for loop is used to iterate over an array, collection, or iterable. It consists of three parts: initialization, condition, and increment.

Example:

int[] scores = {90, 80, 70, 60};

for (int score : scores) {

System.out.println("Score: " + score);

}

Output:

Score: 90

Score: 80

Score: 70

Score: 60

While Loop: The while loop is used to execute a block of code as long as a specified condition is true.

Example:

int i = 0;

while (i < 5) {

System.out.println("Hello, World!");

i++;

}

Output:

Hello, World!

Hello, World!

Hello, World!

Hello, World!

Hello, World!

Do-While Loop: The do-while loop is similar to the while loop, but it checks the condition after executing the code block.

Example:

int i = 0;

do {

System.out.println("Hello, World!");

i++;

} while (i < 5);

Output:

Hello, World!

Hello, World!

Hello, World!

Hello, World!

Hello, World!

Nested Loops: You can also use nested loops to iterate over multiple arrays or collections.

Example:

int[][] matrix = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

for (int i = 0; i < matrix.length; i++) {

for (int j = 0; j < matrix[i].length; j++) {

System.out.print(matrix[i][j] + " ");

}

System.out.println();

}

Output:

1 2 3

4 5 6

7 8 9

Infinite Loop: An infinite loop is a loop that continues indefinitely until the program is interrupted or terminated.

Example:

while (true) {

System.out.println("Hello, World!");

}

Note: This type of loop should be used with caution, as it can lead to unexpected behavior and memory leaks if not properly controlled.

These are some common types of loops in Java. Each has its own use cases and scenarios where they might be more suitable than others.