Java loops - while, do-while & for statements


Java provides several control flow statements to manage the flow of your programs. In this comprehensive guide, we'll explore some of the most essential control flow statements related to loops: the while loop, the do-while loop, the for loop, and the for-each loop. By the end, you'll have a deep understanding of how to harness these constructs to iterate over data and control program execution.

 

The While Loop

The while loop is one of the most fundamental looping structures in Java. It repeatedly executes a block of code as long as a specified condition is true. Here's the basic syntax of a while loop:

while (condition) {
    // Code to execute while the condition is true
}

Example: Counting from 1 to 5

int i = 1;
while (i <= 5) {
    System.out.println(i);
    i++;
}

Output

1
2
3
4
5

In this example, the loop will execute five times, printing the numbers 1 through 5.

 

The Do-While Loop

The do-while loop is similar to the while loop, but it guarantees that the block of code is executed at least once, as the condition is checked after the code block. Here's the basic syntax of a do-while loop:

do {
    // Code to execute at least once
} while (condition);

Example: User Input Validation

import java.util.Scanner;

int userChoice;
Scanner scanner = new Scanner(System.in);

do {
    System.out.println("Enter a number between 1 and 10: ");
    userChoice = scanner.nextInt();
} while (userChoice < 1 || userChoice > 10);

System.out.println("You entered a valid number: " + userChoice);

Output

Enter a number between 1 and 10:
11
Enter a number between 1 and 10:
0
Enter a number between 1 and 10:
5
You entered a valid number: 5

In this example, the loop prompts the user for input and continues to do so until a valid number between 1 and 10 is entered.

 

The For Loop

The for loop is a concise way to iterate over a sequence of values, making it particularly useful for iterating over arrays and other collections. Here's the basic syntax of a for loop:

for (initialization; condition; update) {
    // Code to execute while the condition is true
}

Example: Sum of Numbers

int sum = 0;
for (int i = 1; i <= 10; i++) {
    sum += i;
}
System.out.println("The sum of numbers from 1 to 10 is: " + sum);

Output

The sum of numbers from 1 to 10 is: 55

In this example, the loop calculates the sum of numbers from 1 to 10.

 

The For-Each Loop

The for-each loop, also known as the enhanced for loop, simplifies iteration over arrays and collections. It doesn't require explicit initialization, condition, or update statements. Here's the basic syntax of a for-each loop:

for (elementType element : arrayOrCollection) {
    // Code to execute for each element
}

Example: Iterating Over an Array

int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;

for (int num : numbers) {
    sum += num;
}

System.out.println("The sum of numbers in the array is: " + sum);

Output

The sum of numbers in the array is: 15

In this example, the for-each loop simplifies the process of iterating over an array and calculating the sum of its elements.

 

Summary

Control flow statements like while, do-while, for, and for-each loops are indispensable tools in Java for controlling program execution and iterating over data. By mastering these constructs, you'll be well-equipped to handle various tasks, from simple counting to complex data processing, making you a more proficient Java programmer.



Thanks for feedback.



Read More....
Java Exception Handling
HashMap in Java
Java : HashSet and LinkedHashSet
Inner classes in Java
Java Collections Framework
Java Data Types and Variables - Declaration and Initialization