Java Branching Statements

Java Branching Statements, Java Programming Fundamentals, Java Control Flow, Java break, Java continue, and Java return statements.

We have three types of conditional statements in computer programming:

I. Decision-making/Conditional statements (if, switch)
II. Loop Statements (for, while, do while, and enhanced for)
III. Branching statements (break, continue, and return)

Branching Statements in Java

Branching statements are used to transfer control from one point to another in the code Branching statements are defined by three keywords – break, continue and return

1. The break Statement

The break statement has two forms: unlabeled and labeled.

The break statement is used to stop the execution and comes out of the loop Mostly break statements are used in switch and in loops….

Example:

for (int i=1; i<=10; i++){
System.out.println(i);
if (i==4){
break;
}
}

2. The continue Statement

The continue statement is also the same as the break statement, the only difference is when the break statement executes it comes out from the loop, whereas the continue statement executes comes out from the loop temporarily.

Example:

for (int i=1; i<=10; i++){
if (i%2 != 0){
continue;
}
System.out.println(i);
}

3. The return Statement

The return statement is used in User-defined methods (methods with a return value) and the return statement must be always the last statement in the method.

Example:

public class ControFlow {

public int add(int a, int b){
int result=a+b;
return result;

public static void main(String[] args) {
ControFlow obj = new ControFlow();

int res= obj.add(100, 50);
System.out.println(res);//150

System.out.println(obj.add(100, 200));//300
}

}
}

//Create Object

(ClassName objectName = new ClassName();
ControFlow obj = new ControFlow();

DataType variableName = Object.Method(Values for Arguments)
int res= obj.add(100, 50);

Note: We create code blocks using conditional or decision-making statements (if, else, else if, and switch) and Loop statements (for, while, do while, and enhanced for), but we can’t create code blocks using Branching statements (break, continue, and return), they inserted in conditions and loops.


Java Language Syllabus

Java Videos

Follow me on social media: