Java Control Statement
2. Looping Statements
In Java, loops are used to runs a block of code repeatedly until a given condition evaluates to false. The same code can be reused and executed multiple times, which reduces code duplication and improving code reusability.
- while loop
- do-while loop
- for loop
- Nested loop
- Enhanceh for loop
1. while loop statement:
The while loop in Java first checks a condition and then continues executing the block of code until the condition becomes false.
Syntax:
while (condition/boolean expression) {
// block of code to be executed
// The loop will continue as long as the condition is true
}Example:
public class HelloWorld {
public static void main(String[] args) {
int number = 1;
while (number <= 5) {
System.out.println("Number: " + number);
number++;
}
}
}
output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5Explanation:
- The variable number is initialized to 1.
- The while loop runs as long as number
<= 5. - During each iteration, it prints the value of number and increments it by 1.
- The loop stops when number becomes 6
Note: If the number is not increased by 1, the condition will remain true, and the loop will run forever.
2. do-while loop statement:
A do-while loop in Java runs a block of code at least once, then keeps repeating it as long as the condition is true.
Syntax:
do {
//block of code to be executed
//The loop will continue as long as the condition is true
}
while (condition);Example:
public class HelloWorld {
public static void main(String[] args) {
int number = 1;
do {
System.out.println("Number: " + number);
number++;
} while (number <= 5);
}
}
output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5Explanation:
- The variable number is initialized to 1.
- The do block executes first, so it prints the value of number at least once.
- After printing, number is increased by 1.
- The condition number
<= 5is checked after each iteration. - The loop continues until number becomes 6, at which point the condition becomes false and the loop stops.
Note: It runs the block of code at least once, even if the condition is false.
3. for loop statement:
It is used to execute a block of code repeatedly for a specific number of times, based on an initialization, condition, and update expression.
Syntax:
for (initialization; condition; update) {
// code to be executed
}
//Initialization : sets the starting value of the loop variable.
//Condition : is checked before each iteration to decide whether the loop should continue or stop.
//Update : changes the loop variable after each iteration to move the loop forward.Example:
public class HelloWorld {
public static void main(String[] args) {
for (int count = 1; count <= 5; count++) {
System.out.println("Number: " + count);
}
}
}
output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5Explanation:
- The variable count is initialized to 1.
- The loop runs as long as count
<= 5. - In each iteration, it prints the value of count.
- After each iteration, count is increased by 1 using count++.
- The loop stops when count becomes 6, because the condition becomes false.
4. Nested loop statement:
It is a loop inside another loop. It is used when one loop depends on another loop to repeat a task multiple times.
Syntax:
for (initialization; condition; update) {
// outer loop code
for (initialization; condition; update) {
// inner loop code
}
}Example:
public class HelloWorld {
public static void main(String[] args) {
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
System.out.println("Row: " + row + ", Column: " + column);
}
}
}
}
output
Row: 1, Column: 1
Row: 1, Column: 2
Row: 1, Column: 3
Row: 2, Column: 1
Row: 2, Column: 2
Row: 2, Column: 3
Row: 3, Column: 1
Row: 3, Column: 2
Row: 3, Column: 3Explanation:
- The outer loop (row) runs 3 times.
- For each value of row, the inner loop (column) also runs 3 times.
- The inner loop completes all its iterations for every single iteration of the outer loop.
- This is why nested loops are useful for working with rows and columns (like tables or patterns).
5. Enhanced for loop statement:
It is a control statement used to iterate each elements of an array or collection one by one without using an index.
Syntax:
for (dataType variable : arrayOrCollection) {
// code to be executed
}Example:
public class HelloWorld {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int number : numbers) {
System.out.println("Number: " + number);
}
}
}
output:
Number: 10
Number: 20
Number: 30
Number: 40
Number: 50
Explanation:
- The array numbers stores multiple integer values.
- The enhanced for loop takes each element from the array one by one.
- The variable number temporarily holds each value during iteration.
- It prints each value without using an index.
- The loop automatically stops after all elements are processed.
Difference between for loop and enhanced for loop:
A for loop uses an index and allows full control over initialization, condition, and update, which helps in accessing elements by their position.
An enhanced for loop (for-each loop) goes through each element directly without using an index, making it easier and more readable for arrays and collections.
See you in next page....🙂