1. Loop Control
A loop is used when a block of code needs to be executed several times. Programming languages provide control structures that allow more complicated execution paths.
A loop statement allows a statement or group of statements to be executed multiple times.
Types of Loops in Java
while
Repeatedly executes a target statement as long as a condition is true.
for
Useful when the number of repetitions is known or can be controlled efficiently.
do...while
Executes the loop body at least once because the condition is checked after the body.
Enhanced for
Traverses arrays and collections using a simplified syntax.
2. while Loop
A while loop repeatedly executes a target statement as long as a given condition is true.
Syntax
while(Boolean_expression) {
// Statements
}
The statement may be a single statement or a block of statements. When the Boolean expression is true, the loop body executes. This continues until the expression becomes false, after which control moves to the statement immediately following the loop.
3. while Loop Example
public class Test {
public static void main(String args[]) {
int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
Output
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
4. for Loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times.
It is useful when you know how many times a task is to be repeated.
Syntax
for(initialization; Boolean_expression; update) {
// Statements
}
Flow of Control
- Initialization: Executed first and only once. It declares and initializes loop-control variables.
- Boolean expression: Evaluated next. If true, the loop body executes; if false, control moves past the loop.
- Loop body: Executes when the condition is true.
- Update: Executes after the body and updates loop-control variables.
- The Boolean expression is evaluated again and the process repeats until it becomes false.
5. for Loop Example
public class Test {
public static void main(String args[]) {
for(int x = 10; x < 20; x = x + 1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
Output
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
6. do...while Loop
A do...while loop is similar to a while loop, except that it is guaranteed to execute at least one time.
Syntax
do {
// Statements
} while(Boolean_expression);
The Boolean expression appears at the end of the loop, so the statements execute once before the condition is tested. If the expression is true, control jumps back to the do statement and the loop repeats.
while checks the condition before execution; do...while checks it after execution.
7. do...while Example and Output
public class Test {
public static void main(String args[]) {
int x = 10;
do {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
} while( x < 20 );
}
}
Output
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
8. break Statement
The break statement has two main usages in the source:
- When encountered inside a loop, it immediately terminates the loop and program control resumes at the next statement following the loop.
- It can be used to terminate a case in a
switchstatement.
Syntax
break;
Example
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
}
Output
10 20
9. continue Statement
The continue keyword can be used in any loop-control structure. It causes the loop to immediately jump to the next iteration.
- In a
forloop, control jumps to the update statement. - In a
whileordo/whileloop, control jumps to the Boolean expression.
Syntax
continue;
Example
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
continue;
}
System.out.print( x );
System.out.print("\n");
}
}
}
Output
10 20 40 50
10. Enhanced for Loop
As of Java 5, the enhanced for loop was introduced. It is mainly used to traverse collections of elements, including arrays.
Syntax
for(declaration : expression) {
// Statements
}
Declaration
The newly declared block variable must have a type compatible with the elements of the array being accessed. It is available within the for block and contains the current array element.
Expression
The expression evaluates to the array to be traversed. It can be an array variable or a method call that returns an array.
Example
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String [] names = {"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}
Output
10, 20, 30, 40, 50, James, Larry, Tom, Lacy,
11. Quick Revision
| Topic | Key Point |
|---|---|
| while | Repeats while the Boolean condition is true; may execute zero times. |
| for | Best suited when loop repetition can be controlled with initialization, condition and update. |
| do...while | Executes the body at least once because the condition is checked at the end. |
| break | Immediately terminates the loop and continues after it. |
| continue | Skips the current iteration and moves to the next iteration. |
| Enhanced for | Traverses arrays/collections using for(type item : array) syntax. |
Loop Selection
- while: Use when the number of iterations is not necessarily known in advance.
- for: Use when initialization, condition and update form a clear counting/repetition pattern.
- do...while: Use when the body must execute at least once.
- enhanced for: Use when simply traversing elements of an array or collection.