Search⌘ K
AI Features

Entry Controlled Loop: Unfixed Iteration

Explore the concept of entry controlled loops with unfixed iteration in Java. Understand how while and for loops operate differently based on iteration control, and learn to manage loop flow using break and continue statements. This lesson helps you grasp fixed versus unfixed iteration and introduces exit controlled loops for effective code control.

Coding example: 58

The following example deals with another interesting part of code statements, branching statements. By using continue and break keywords, we can control the movement of a loop. First, see the example provided below:

Java
public class ExampleFiftyEight {
public static void main(String[] args) {
System.out.println("Continue statement: ");
//continue statement tells the control to skip the current statement
//the loop executes the remaining statement
for(int i = 0; i < 5; i++){
if(i == 2){
continue;
}
System.out.println(i);
}
System.out.println("Break statement: ");
for(int i = 0; i < 5; i++){
if(i == 2){
break;
//loop terminates when this condition is true
}
System.out.println(i);
}
}
}

Code explanation

In the first case, the continue keyword skips a certain value and the loop continues. In the second case, when the iteration matches the exact value, it no longer moves. The loop terminates at that particular point. Look at the output, ...