[post-views]
In this lesson, you will learn.
Java supports three jump statements: break, continue, and return. These statements transfer control to another part of your program.

In Java, the break statement has three uses.
First, as you have seen, it terminates a statement sequence in a switch statement. Second, it can be used to exit a loop. Third, it can be used as a “civilized” form of goto.
package ch5;
public class BreakExample1 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Breaks out of the loop when i is 5
}
System.out.println("i = " + i);
}
System.out.println("Loop complete.");
}
}
Output
i = 0
i = 1
i = 2
i = 3
i = 4
Loop complete.
In this example, the loop will print the numbers from 0 to 4. When i becomes 5, the break statement is executed, causing an immediate exit from the loop.
package ch5;
public class BreakExample2 {
public static void main(String[] args) {
int count = 0;
while (true) {
if (count == 5) {
break; // Breaks out of the loop when count is 5
}
System.out.println("Count: " + count);
count++;
}
System.out.println("Out of loop.");
}
}
Output
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Out of loop.
Here, the while loop would normally be infinite (while (true)). However, when count reaches 5, the break statement is executed, stopping the loop and allowing the program to continue with the next statement after the loop.
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
case 'C':
System.out.println("Well done");
break;
case 'D':
System.out.println("You passed");
break;
case 'F':
System.out.println("Better try again");
break;
default:
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
In this switch statement, the break keyword is used to terminate a case once it has been matched and its statements executed. Without the break, the program would “fall through” to subsequent cases until it hits another break or the end of the switch.
The continue statement in Java is used within looping constructs (like for, while, or do-while loops) to skip the current iteration and proceed to the next iteration of the loop.
When a continue statement is encountered, the loop skips the remaining code in its body and goes directly to the next iteration.
It is useful when you want to skip certain steps in a loop under specific conditions.
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip the rest of the loop body for even numbers
}
System.out.println(i); // This will print only odd numbers
}
In this example, the loop skips the print statement for even numbers and only prints odd numbers from 1 to 10.
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue; // Skip the iteration when i is 3
}
System.out.println("i = " + i);
}
Here, the loop will not print the number 3, as continue skips the print statement when i is 3.
int i = 0;
do {
i++;
if (i == 4) {
continue; // Skip the iteration when i is 4
}
System.out.println("i = " + i);
} while (i < 5);
In this do-while loop, the number 4 will not be printed, as the loop continues to the next iteration when i is 4.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue; // Skip the iteration when j is 2
}
System.out.println("i = " + i + ", j = " + j);
}
}
This example uses nested loops, and the inner loop skips its iteration when j is 2. So, the pair (i, 2) will not be printed for any value of i.
The return statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method.
public static int square(int number) {
return number * number; // Returns the square of the number
}
public static void main(String[] args) {
int result = square(5);
System.out.println("Square of 5 is: " + result);
}
In this example, the method square calculates the square of a given number and uses the return statement to send this value back to the caller.
public static boolean isEven(int number) {
if (number % 2 == 0) {
return true; // Returns true if number is even
} else {
return false; // Returns false if number is odd
}
}
public static void main(String[] args) {
boolean even = isEven(7);
System.out.println("Number 7 is even: " + even);
}
Here, the isEven method returns true if the number is even and false otherwise. The return statement is used within a conditional structure to return different values based on the number.
public static String checkStatus(int score) {
if (score < 0 || score > 100) {
return "Invalid score"; // Early return if score is out of range
}
if (score >= 50) {
return "Pass";
} else {
return "Fail";
}
}
public static void main(String[] args) {
String status = checkStatus(85);
System.out.println("Status: " + status);
}
n this example, the method checkStatus returns “Invalid score” immediately if the score is out of a valid range (0-100). This is an example of an early return that can simplify the logic by avoiding nested conditional statements.
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.