1. break
Used to immediately stop a loop or switch.
for(int i=1; i<=5; i++) {
if(i == 3) {
break;
}
System.out.println(i);
}
Output:
1
2
When i == 3, loop stops completely.
2. continue
Skips current iteration and moves to next iteration.
for(int i=1; i<=5; i++) {
if(i == 3) {
continue;
}
System.out.println(i);
}
Output:
1
2
4
5
3 is skipped, but loop continues.
3. return
Ends the method execution.
public int add(int a, int b) {
return a + b;
}
After return, nothing else in the method executes.
return;
Used in void methods.
A return statement in Java can return:
1. Primitive values
return 10;
return 3.14;
return true;
return 'A';
2. Objects
return "Hello";
return new Student();
Can return:
String
Arrays
Collections
Custom objects
Any class object
3. Arrays
return new int[]{1,2,3};
4. null
For reference types only.
return null;
Not allowed for primitive return types.
5. Nothing (void methods)
public void show() {
return;
}
or simply:
public void show() {
}
4.throw statement
A throw statement also exits the method immediately, similar to return.
That’s why they may feel similar.
Example:
public int test() {
throw new RuntimeException();
// no code after this executes
}
But:
return → sends result back
throw → sends exception object
5.yield
- Used in switch expressions
- Used in switch expressions
- Only inside switch expression block
- Produces switch expression result
int day = 2;
String result = switch(day) {
case 1 -> "Monday";
case 2 -> {
yield "Tuesday";
}
default -> "Invalid";
};

Top comments (0)