In this tutorial, you'll learn about two types of loops in Java: the while loop and the do-while loop. These loops are essential for executing a block of code repeatedly as long as a specified condition is true. Understanding how to use these loops will help you write more efficient and effective Java programs.
Loops are fundamental constructs in programming that allow you to repeat a set of instructions until a specific condition is met. The while loop and the do-while loop are both used for this purpose, but they have subtle differences in how they operate. In this tutorial, we'll explore these loops with examples and best practices.
The while loop repeatedly executes a block of code as long as a specified condition is true. It checks the condition before executing the loop body.
1while (condition) {2// Code to be executed3};
true or false). The loop continues to execute as long as this condition remains true.Let's look at a simple example where we use a while loop to count from 1 to 5.
1public class WhileLoopExample {2public static void main(String[] args) {3int i = 1;4while (i <= 5) {5System.out.println("Count: " + i);6i++;7}8}9}
Count: 1 Count: 2 Count: 3 Count: 4 Count: 5
In this example, the variable i is initialized to 1. The while loop checks if i is less than or equal to 5. If the condition is true, it prints the current value of i and increments i by 1. This process repeats until i becomes greater than 5.
1public class InfiniteLoop {2public static void main(String[] args) {3int i = 1;4while (i <= 5) {5System.out.println("Count: " + i);6}7}8}
To avoid infinite loops, ensure that the loop variable is updated correctly within the loop body.
The do-while loop is similar to the while loop, but it executes the loop body at least once before checking the condition. This means that even if the condition is false initially, the loop will execute at least one time.
1do {2// Code to be executed3} while (condition);
do statement is executed first.Here's an example where we use a do-while loop to count from 1 to 5.
1public class DoWhileLoopExample {2public static void main(String[] args) {3int i = 1;4do {5System.out.println("Count: " + i);6i++;7} while (i <= 5);8}9}
Count: 1 Count: 2 Count: 3 Count: 4 Count: 5
In this example, the variable i is initialized to 1. The loop body executes first, printing "Count: 1". Then it checks if i is less than or equal to 5. If the condition is true, it continues to execute the loop body and increments i. This process repeats until i becomes greater than 5.
do-while loop always executes at least once because it checks the condition after executing the loop body.do-while loop when you want to ensure that the code block is executed at least once, regardless of the initial condition.Let's create a practical example where we use both while and do-while loops to sum numbers from 1 to 10.
1public class SumNumbers {2public static void main(String[] args) {3int number = 1;4int sumWhile = 0;56// Using while loop7while (number <= 10) {8sumWhile += number;9number++;10}11System.out.println("Sum using while loop: " + sumWhile);1213// Reset variables for do-while loop14number = 1;15int sumDoWhile = 0;1617// Using do-while loop18do {19sumDoWhile += number;20number++;21} while (number <= 10);22System.out.println("Sum using do-while loop: " + sumDoWhile);23}24}
Sum using while loop: 55 Sum using do-while loop: 55
In this example, we first use a while loop to sum numbers from 1 to 10. Then we reset the variables and use a do-while loop to perform the same task. Both loops produce the same result, demonstrating their similarity in functionality.
| Loop Type | Syntax | Execution Order |
|---|---|---|
| While Loop | while (condition) { ... } | Checks condition before execution |
| Do-While Loop | do { ... } while (condition); | Executes at least once, checks condition after |
Now that you've learned about while loops and do-while loops, you're ready to explore another type of loop in Java: the for loop. The for loop is often used when the number of iterations is known beforehand. In the next tutorial, we'll dive into how to use for loops effectively in your Java programs.
Stay tuned for more Java tutorials!