Branching statements in Java are used to alter the normal flow of control in a program. They allow you to jump from one part of the code to another, exit loops early, or return values from methods. These statements enable dynamic control over program execution, such as stopping a loop prematurely or returning early from a method.
Types of Branching Statements in Java:
breakStatement:- What is it?
The break statement is used to exit a loop or a switch statement immediately, transferring control to the statement following the loop or switch.
3. How it works:
When a break statement is encountered, the loop or switch is terminated, and execution continues with the next statement outside of the loop or switch.
Usage in Loops:
java
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exits the loop when i is 5
}
System.out.println(i);
}
// Output: 0, 1, 2, 3, 4
Usage in switch:
java
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
// Output: Wednesday
continueStatement:- What is it?
The continue statement is used to skip the current iteration of a loop and proceed to the next iteration.
3. How it works:
When a continue statement is encountered, the remaining statements inside the loop are skipped for the current iteration, and the loop proceeds with the next iteration.
Example:
java
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue; // Skips the iteration when i is 2
}
System.out.println(i);
}
// Output: 0, 1, 3, 4 (2 is skipped)
returnStatement:- What is it?
The return statement is used to exit from a method and optionally return a value to the method caller.
3. How it works:
When a return statement is encountered, the current method stops executing, and control is returned to the calling method. If the method has a return type (other than void), a value is returned with the return statement.
Example in a Method with a Return Value:
```java
public int add(int a, int b) {
return a + b; // Returns the sum of a and b
}
public static void main(String[] args) {
int result \= add(5, 3);
System.out.println(result); // Output: 8
}
```
Example in a Void Method:
java
public void checkAge(int age) {
if (age < 18) {
System.out.println("Not allowed");
return; // Exits the method early if age is less than 18
}
System.out.println("Welcome!");
}
How Branching Statements Work:
breakStatement:- Used primarily in loops (
for,while,do-while) and switch statements. - Immediately exits the loop or switch, stopping further iterations or case evaluations.
continueStatement:- Used in loops to skip the remaining code in the current iteration and move to the next iteration.
- Useful for skipping specific iterations based on a condition without terminating the entire loop.
returnStatement:- Used in methods to exit and optionally pass a value back to the calling method.
- In
voidmethods,returncan be used to exit the method early without returning a value.
Key Differences:
break: Exits a loop or switch completely.continue: Skips the current iteration of a loop and moves to the next iteration.return: Exits a method and optionally returns a value to the calling method.
Example of break, continue, and return:
public class BranchingExample { public static void main(String[] args) { // Using break for (int i = 0; i < 5; i++) { if (i == 3) { break; // Exits loop when i is 3 } System.out.println(i); // Output: 0, 1, 2 }
// Using continue
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue; // Skips the iteration when i is 2
}
System.out.println(i); // Output: 0, 1, 3, 4
}
// Using return
checkAge(16);
}
public static void checkAge(int age) {
if (age < 18) {
System.out.println("Not allowed");
return; // Exits method if age is less than 18
}
System.out.println("Welcome!");
}
}
Summary:
break: Exits loops or switch statements immediately.continue: Skips the current iteration of a loop and moves to the next iteration.return: Exits a method and optionally returns a value to the calling method.
In conclusion, branching statements in Java are crucial for altering the flow of control in programs. They provide flexibility in how loops and methods are executed, enabling programs to respond dynamically to different conditions or data.