Welcome to The Coding College! In this tutorial, we will explore the while loop in Java, one of the fundamental control flow statements used for repetitive tasks.
What is a While Loop?
A while
loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The loop continues to execute as long as the condition evaluates to true
.
Syntax
while (condition) {
// Code to be executed
}
- Condition: The loop runs as long as this condition evaluates to
true
. - Body: The block of code inside the
{}
executes in each iteration.
Key Points
- Condition Evaluation: The condition is evaluated before executing the loop body. If the condition is
false
initially, the loop will not execute even once. - Infinite Loop: Ensure the condition eventually becomes
false
; otherwise, the loop will run indefinitely. - Use Cases: While loops are often used when the number of iterations is not predetermined.
Example 1: Basic While Loop
public class WhileExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("Count: " + i);
i++;
}
}
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Example 2: Summing Numbers
public class SumExample {
public static void main(String[] args) {
int sum = 0, i = 1;
while (i <= 10) {
sum += i; // Add i to sum
i++;
}
System.out.println("Sum of numbers from 1 to 10: " + sum);
}
}
Output:
Sum of numbers from 1 to 10: 55
Example 3: Handling User Input
import java.util.Scanner;
public class WhileUserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
System.out.println("Enter a positive number (0 to quit):");
number = scanner.nextInt();
while (number != 0) {
System.out.println("You entered: " + number);
System.out.println("Enter another number (0 to quit):");
number = scanner.nextInt();
}
System.out.println("Program ended.");
scanner.close();
}
}
Output Example:
Enter a positive number (0 to quit):
5
You entered: 5
Enter another number (0 to quit):
10
You entered: 10
Enter another number (0 to quit):
0
Program ended.
Example 4: Infinite Loop
public class InfiniteWhile {
public static void main(String[] args) {
int i = 1;
while (true) {
System.out.println("Iteration: " + i);
i++;
if (i > 5) {
break; // Exit the loop when i is greater than 5
}
}
}
}
Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Note: Use the break
statement to exit an infinite loop when necessary.
Example 5: Nested While Loops
public class NestedWhile {
public static void main(String[] args) {
int i = 1;
while (i <= 3) {
int j = 1;
while (j <= 3) {
System.out.println("i = " + i + ", j = " + j);
j++;
}
i++;
}
}
}
Output:
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3
Example 6: While Loop with Break and Continue
public class WhileBreakContinue {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
if (i == 5) {
System.out.println("Skipping 5");
i++;
continue; // Skip the rest of the loop body for this iteration
}
if (i == 8) {
System.out.println("Breaking loop at 8");
break; // Exit the loop
}
System.out.println("i = " + i);
i++;
}
}
}
Output:
i = 1
i = 2
i = 3
i = 4
Skipping 5
i = 6
i = 7
Breaking loop at 8
Practice Problems
- Write a program to display all even numbers between 1 and 50 using a while loop.
- Create a program to reverse a number entered by the user using a while loop.
- Use a while loop to calculate the factorial of a given number.
Conclusion
The while
loop in Java is an essential tool for executing repetitive tasks when the number of iterations is not predefined. With careful implementation, it can be used to build efficient and effective programs.
For more such tutorials and examples, visit The Coding College. Happy Coding! 🚀