Java Branching Statements
Java Control Flow Statements
Branching Statements in Java
Java Branching Statements
- Java provides three branching statements break, continue and return.
- Branching statements in Java are used to change the normal flow of execution based on some condition.
- The return branching statement is used to explicitly return from a method.
- break branching statement is used to break the loop and transfer control to the line immediate outside of loop.
- continue branching statement is used to escape current execution and transfers control back to the start of the loop.
- Java branching statements break and continue statements have two forms: the labeled form and the unlabeled form.
break keyword can also be used inside switch statement to break current choice. In this tutorial you can learn about Java Branching Statements like break Statement, continue Statement and return Statement. This tutorial is also example for branching statements in Java.
- What are the Branching Statements in Java ?
- What are the statements used for branching in Java ?
- What are Jump Statements in Java ?
- How to use break Statement in Java ?
- How to use continue Statement in Java ?
- How to use return Statement in Java ?
- How to use Labeled break and continue Statements in Java ?
- How to use Unlabeled break and continue Statements in Java ?
Branching Statements in Java
Java programming language provides following types of decision making or branching statements in Java . Java programming language provided decision making statements or branching statements as follows .
if statement
An if statement consists of a boolean expression followed by one or more statements .
if...else statement
An if statement can be followed by an optional else statement , which executes when the boolean expression is false .
nested if statement
You can use one if or else if statement inside another if or else if statements .
switch statement
A switch statement allows a variable to be tested for equality against a list of values.
You can read more on Control Flow Statements in Java.
What are the Branching Statements in Java ?
Java provides 3 branching statements named break, continue and return. Branching statements are used to change the normal flow of execution based on some condition. Some branching statements are used in looping statements and others used to change the flow of execution of the program.
What are the statements used for branching in Java ?
In Java , Branching statements allow the flow of execution to jump to a different part of the program. The common branching statements used within other control structures are break , continue , return and goto .
What are Jump Statements in Java ?
In Java, Jump statements are used to unconditionally transfer program control from one point to elsewhere in the program. Jump statements are primarily used to interrupt loop or switch-case instantly. Java supports three jump statements break, continue, and return.
How to use break Statement in Java ?
break statement is used to break the loop and transfer control to the line immediate outside of loop. In the following break statement example you can learn how to use break statement in Java. This explanation also for What is break Statement in Java ?
Java break Statement Example
/* Java break Statement Example Save with file name BreakStatement.java */ public class BreakStatement { public static void main(String args[]) { System.out.println("Java break Statement Example "); int count = 10; // INFINIT LOOP while(true) { if(count == 0) break; System.out.println(count); count--; } } }
How to use continue Statement in Java ?
continue statement is used to escape current execution and transfers control back to the start of the loop. In the following continue statement example you can learn how to use continue statement in Java. This explanation also for What is continue Statement in Java ?
Java continue Statement Example
/* Java continue Statement Example Save with file name ContinueStatement.java */ public class ContinueStatement { public static void main(String args[]) { System.out.println("Java continue Statement Example "); int count = 1; while(count<=100) { if(count <= 10) { System.out.println(count); count++; continue; } else { break; } } } }
How to use return statement in Java ?
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. The return statement can be used to cause execution to branch back to the caller of the method. Thus, the return statement immediately terminates the method in which it is executed. The return statement has two forms:
- One that returns a value
- One that does not returns a value
To return a value, simply put the value (or an expression that calculates the value) after the return keyword. This explanation also for What is return Statement in Java ? and How many forms of return Statement in Java ?. In the following return statement example you can learn how to use return branching statement in Java.
Java return Statement Example
/* Java return Statement Example Save with file name ReturnStatement.java */ public class ReturnStatement { public static void main(String args[]) { System.out.println("Java return Statement Example "); int max = getMaximumNumber(10, 20); if(max==0) System.out.println("Equal Numbers"); else System.out.println("Max Number is : " + max); } // METHOD public static int getMaximumNumber(int x, int y) { if(x > y) return x; else if(x < y) return y; else return 0; } }
How to use Labeled break and continue Statements in Java ?
The most common to use break and continue statement labels with looping statements like for or while. A label statement must be placed just before the statement being labeled, and it consists of a valid identifier that ends with a colon : . The labeled varieties are needed only in situations where you have nested loop and need to indicate which of the nested loops you want to break. The break statement will exit out of the labeled loop, as opposed to the innermost loop. The following branching statements example shows how to use labeled break and continue statements.
Labeled break and continue statements Example
/* Labeled break and continue statements Example Save with file name LabeledStatements.java */ public class LabeledStatements { public static void main(String args[]) { System.out.println("Labeled break and continue statements Example"); outer: for(int i = 1 ; ; i++) { for(int j = 1 ; j < 11 ; j++) { if( j > i) { System.out.println(); continue outer; } System.out.print(" *"); } if(i == 10) break; } } }
How to use Unlabeled break and continue Statements in Java ?
An unlabeled break statement terminates the innermost switch, for, while, or do-while statement. But a labeled break terminates an outer statement. The following unlabeled branching statements example shows how to use unlabeled break and continue statements.
Unlabeled break and continue statements Example
/* Unlabeled break and continue statements Example Save with file name UnlabledStatements.java */ public class UnlabledStatements { public static void main(String args[]) { System.out.println("Unlabeled break and continue statements Example"); int even_odd_numbers[] = {1,2,4,99,7,8}; int even_sum = 0; for(int i = 0; i < even_odd_numbers.length; i++) { if((even_odd_numbers[i] % 2) == 0) { even_sum += even_odd_numbers[i]; continue; } if(even_odd_numbers[i] == 99) { break; } } System.out.println("Even Numbers Sum : " + even_sum); } }
Java Branching Statements Summary
Java provides 3 branching statement named break, continue and return. Branching statements are used to change the normal flow of execution based on some condition. The Java return statement is used to explicitly return from a method.