Jump statement are used to transfer the control of execution from one part of the program to another. They help you skip certain code blocks or exit loop early.
Types of jump statement:
Break:
Immediately terminate the loop or switch. Control moves to the first statement after the loop or switch.
Example:
public class TwentySeven{
public static void main (String[] args){
for (int no=1;no<=5;no++){
if(no==3){
break;
}
System.out.println("Hello");
}
}
}
Continue:
Skips the current iteration of a loop and moves to the next iteration. Commonly used with condition inside loops.
Example:
public class TwentySeven{
public static void main (String[] args){
for (int no=1;no<=5;no++){
if(no==3){
continue;
}
System.out.println("Hello");
}
}
}
Return:
Used to exit from a method immediately. Can also return a value to the caller if the method is not void.
Example:
public class TwentySeven{
public static void main (String[] args){
System.out.println("Hello!!!");
if(true){
return;
}
System.out.println("Hello");
}
}
Note: break and continue are mostly used inside loops and switch return is used inside method to exit and optionally send back a value.
Top comments (0)