java loop programs for practice

Ted 177 Published: 08/23/2024

java loop programs for practice

I'd be happy to provide you with some Java loop programming examples for practice! Here are a few:

Example 1: Printing Numbers using For Loop

public class Main {

public static void main(String[] args) {

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

System.out.println(i);

}

}

}

This program uses a for loop to print the numbers from 0 to 9.

Example 2: Printing Numbers using While Loop

public class Main {

public static void main(String[] args) {

int i = 0;

while (i < 10) {

System.out.println(i);

i++;

}

}

}

This program uses a while loop to print the numbers from 0 to 9.

Example 3: Printing Numbers using Do-While Loop

public class Main {

public static void main(String[] args) {

int i = 0;

do {

System.out.println(i);

i++;

} while (i < 10);

}

}

This program uses a do-while loop to print the numbers from 0 to 9.

Example 4: Printing Numbers using For-Each Loop

public class Main {

public static void main(String[] args) {

for (int i : new int[] {1, 2, 3, 4, 5}) {

System.out.println(i);

}

}

}

This program uses a for-each loop to print the numbers from 1 to 5.

Example 5: Printing Numbers using Nested Loops

public class Main {

public static void main(String[] args) {

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

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

System.out.println(i + "-" + j);

}

}

}

}

This program uses two nested for loops to print the numbers from 0 to 2 and 0 to 3.

Example 6: Printing Numbers using Do-While Loop with Conditional Statement

public class Main {

public static void main(String[] args) {

int i = 0;

do {

System.out.println(i);

i++;

if (i > 5) {

break; // exit the loop when i reaches 6

}

} while (i < 10);

}

}

This program uses a do-while loop with a conditional statement to print the numbers from 0 to 5, and then exits the loop.

Example 7: Printing Numbers using For Loop with Conditional Statement

public class Main {

public static void main(String[] args) {

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

if (i % 2 == 0) { // print only even numbers

System.out.println(i);

}

}

}

}

This program uses a for loop with a conditional statement to print the even numbers from 0 to 9.

I hope these examples help you practice and improve your Java programming skills!

How many types of loop in java

I'm happy to respond in English for you!

Java has several types of loops that can be used to repeat a set of statements. Here are some of the most common ones:

For Loop: This is one of the most widely used loop types. It's useful when you have an array or collection and you want to iterate through each element. The syntax is: for (init; cond; incr) { body }. For example, if you have an array of numbers and you want to find the sum of all elements, you can use a for loop as follows:
int sum = 0;

for (int num : numbers) {

sum += num;

}

While Loop: This type of loop continues as long as the condition is true. It's useful when you want to repeat something until a certain condition is met. The syntax is: while (cond) { body }. For example, if you want to count down from 10 to 1 and print each number, you can use a while loop:
int i = 10;

while (i > 0) {

System.out.println(i);

i--;

}

Do-While Loop: This is similar to the while loop, but it checks the condition after executing the body. The syntax is: do { body } while (cond);. For example, if you want to ask a user for their name until they enter "John", you can use a do-while loop:
String name;

do {

System.out.print("Enter your name: ");

name = input.nextLine();

} while (!name.equals("John"));

Enhanced For Loop (also known as Java 5 for-each loop): This is a variant of the for loop that makes it easier to iterate through collections or arrays without having to keep track of indices. The syntax is: for (variable : collection) { body }. For example, if you have a list of strings and you want to print each one, you can use an enhanced for loop:
List words = Arrays.asList("Hello", "World");

for (String word : words) {

System.out.println(word);

}

Foreach Loop (also known as Java 8 varargs foreach loop): This is another variant of the for loop that allows you to iterate through arrays or collections without having to specify the type explicitly. The syntax is: for (variable : ... ) { body }. For example, if you have a variable number of numbers and you want to sum them up, you can use a foreach loop:
int sum = 0;

for (int num : new int[] {1, 2, 3}) {

sum += num;

}

Labelled Loop: This type of loop allows you to break or continue out of multiple nested loops. The syntax is: label: for (init; cond; incr) { body }. For example, if you have two nested loops and you want to break out of the inner loop when a certain condition is met:
outer: for (int i = 0; i < 10; i++) {

inner: for (int j = 0; j < 10; j++) {

if (i == 5) {

break outer;

}

}

}

In summary, Java provides several types of loops to handle different scenarios. The choice of which one to use depends on the specific problem you're trying to solve and your personal preference as a developer.