Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, loops are used to execute a block of code repeatedly until a specified condition is met. In this tutorial, we will discuss two types of loops: the while
loop and the do...while
loop.
The while
loop repeatedly executes a block of code as long as the specified condition is true. The condition is checked before the loop body is executed. If the condition is false initially, the loop body will not be executed at all.
Syntax:
while (condition) { // code to be executed }
Example:
public class WhileLoopExample { public static void main(String[] args) { int i = 1; while (i <= 5) { System.out.println("Iteration: " + i); i++; } } }
Output:
Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4 Iteration: 5
The do...while
loop is similar to the while
loop but with a key difference: the loop body is executed at least once, even if the condition is initially false. This is because the condition is checked after the loop body is executed.
Syntax:
do { // code to be executed } while (condition);
Example:
public class DoWhileLoopExample { public static void main(String[] args) { int i = 1; do { System.out.println("Iteration: " + i); i++; } while (i <= 5); } }
Output:
Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4 Iteration: 5
In conclusion, while
and do...while
loops in Java are used to execute a block of code repeatedly based on a specified condition. The primary difference between them is that the while
loop checks the condition before executing the loop body, while the do...while
loop checks the condition after executing the loop body at least once.
Using while loop for iterative tasks in Java:
The while
loop is used for repetitive tasks as long as a specified condition is true.
int i = 0; while (i < 5) { System.out.println("Iteration: " + i); i++; }
Infinite while loop and exit conditions in Java:
An infinite while
loop runs indefinitely without an exit condition.
while (true) { // Infinite loop, needs a break statement or another exit condition }
Java do...while loop example:
The do...while
loop guarantees that the block of code is executed at least once before checking the condition.
int i = 0; do { System.out.println("Iteration: " + i); i++; } while (i < 5);
Looping through arrays with while and do...while in Java:
Use while
or do...while
to iterate through array elements.
int[] numbers = {1, 2, 3, 4, 5}; int i = 0; while (i < numbers.length) { System.out.println(numbers[i]); i++; }
Nested while loops in Java:
Nesting while
loops involves placing one loop inside another.
int i = 0; while (i < 3) { int j = 0; while (j < 3) { System.out.println("Nested Loop: " + i + ", " + j); j++; } i++; }
Break and continue statements in while loop in Java:
Use break
to exit the loop and continue
to skip to the next iteration.
int i = 0; while (i < 5) { if (i == 3) { break; // Exit the loop when i is 3 } System.out.println("Iteration: " + i); i++; }
Comparing while and do...while loops in Java:
The primary difference is that while
checks the condition before execution, while do...while
checks after.
// While loop while (condition) { // Code } // Do...while loop do { // Code } while (condition);
Handling user input with while and do...while loops:
while
and do...while
loops are often used to repeatedly prompt the user for input until a valid value is entered.
Scanner scanner = new Scanner(System.in); int userValue; do { System.out.print("Enter a positive number: "); userValue = scanner.nextInt(); } while (userValue <= 0); System.out.println("Valid input: " + userValue);
Java while loop and boolean expressions:
Boolean expressions in while
loops control the loop's execution.
boolean condition = true; while (condition) { // Code condition = false; // Exit condition }
Using while and do...while loops with collections in Java:
Iterate through collections using while
or do...while
loops.
List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); Iterator<String> iterator = names.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }
Looping through linked structures with while in Java:
Traverse linked structures like linked lists using while
loops.
Node current = head; // Assuming head is the starting node of a linked list while (current != null) { System.out.println(current.data); current = current.next; }
Java while loop vs recursion for iteration:
while
loops and recursion both support iteration, but while
loops are generally more straightforward for simple iterations.
// While loop int i = 0; while (i < 5) { System.out.println("Iteration: " + i); i++; } // Recursion void recursiveFunction(int i) { if (i < 5) { System.out.println("Iteration: " + i); recursiveFunction(i + 1); } }