In Java, three key statements are used to change your code's flow control directly: return, break, and continue.
What is the return Statement in Java?
As you've seen earlier in this section, the return statement is used to exit a method. For a full explanation of the return statement, please refer to this page; however, here is a quick explanation of how it is used in flow control.
How to Use the return Statement in Java?
You can use the return keyword to exit a method at any point. Some key points include:
- If the method is of type
void, then you can writereturnwith nothing else - If the method is of any type other than
void, then you mustreturna variable of that type
Java return Statement Example
Below is an example of the return statement used in a method with no return type (void) and a method with a return type of int. (Return types will be covered in great detail in the upcoming "Methods" section of Java 201.
class Main {
public static void main(String[] args){
demonstrateVoidReturn(5);
int a = multiply(456, 987);
System.out.println("a = " + a);
}
public static void demonstrateVoidReturn(int num){
if (num < 20) {
// you will simply exit this method without
// doing anything else if num is less than 20
return;
} else {
System.out.println("num is >= 20");
}
}
public static int multiply(int a, int b){
int x = a * b;
// now you'll return the int value x to
// the calling method this method
// declares "int" as its return type, and
// you are returning an int, so all is well
return x;
}
}
What is the break Statement in Java
The break statement is used to break out of a loop.
How to Use the break Statement in Java?
The break statement has two forms in Java:
- A
breakwith a label - A
breakwithout a label
A break Statement Example With a Label
A break with a label will break out of the nearest loop and begin execution at the label's location. An example is as follows. Using break statements with labels like this is not common practice and I would not personally recommend it. But since it is an option, you should know about it.
one: {
two:{
if(i==0) {
break one; // breaks to label one, prints message
}
// if i !=0, this will print
System.out.println("Print in label two");
}
System.out.println("Print in label one");
}
A break Statement Example Without a Label
The break without a label breaks out of the inside loop and continues to execute directly outside of it. An example is as follows.
class Main {
public static void main(String[] args) {
for(int i = 0; i < 10; i++){
System.out.println(i);
if (i == 3)
break;
}
}
}
The loop will terminate when i = 3.
What is the continue Statement in Java?
The continue statement in Java will force an early iteration of a loop. This does not exit the loop like the break but skips the remaining code and iterates to the next iteration.
How to Use the continue Statement in Java?
The continue statement can be placed anywhere in a loop and requires no other keyword to be used.
A continue Statement Example in Java
The following example iterates over every number between 0 and 100 but will only print even numbers, thanks to the continue statement.
class Main {
public static void main(String[] args){
// print even number between 0 and 100
for(int i = 0; i <= 100; i++) {
if((i%2) != 0) {
continue; // iterate
}
System.out.println(i);
}
}
}
Experiment with the continue Keyword
In the code editor below, demonstrate your ability to use the continue keyword.
class Main {
public static void main(String[] args) {
// please demonstrate the use of the
// "continue" keyword below within a for loop
}
}
Experiment with the break Keyword
In the code editor below, demonstrate your ability to use the break keyword.
class Main {
public static void main(String[] args) {
// please demonstrate the use of the
// "break" keyword below within a for loop
}
}
Experiment with the return Keyword
In the code editor below, demonstrate your ability to use the return keyword.
class Main {
public static void main(String[] args) {
// please demonstrate the use of the
// "return" keyword below within a for loop
}
}
Summary: Java Control Flow Statements
- There are three important statements:
return,break, andcontinue - The
returnstatement can be used at any time to exit a method - In a method of type
void, you can use thereturnkeyword on its own - In a method of any type but
void, use mustreturna value of that type - The
breakkeyword is used inside a loop to exit it prematurely - The
continuekeyword is used to stop an iteration of a loop prematurely, but will continue onto the next iteration instead of leaving the loop