JavaControl Statements

Branching Statements in Java

Learn Java branching statements such as break, continue, and return to control loop execution and program flow effectively.

Branching Statements in JavaLink to this section

Branching statements in Java are used to alter the normal flow of execution of a program. They allow you to jump from one part of the code to another, especially within loops and decision-making structures.

These statements give developers finer control over how and when loops or methods should stop or skip execution.

Types of Branching Statements in JavaLink to this section

Java provides three primary branching statements:

  • break
  • continue
  • return

break StatementLink to this section

The break statement is used to immediately terminate a loop or a switch statement. Once break is executed, control moves outside the loop or switch block.

tip

Use break when you want to stop looping after a specific condition is met.

continue StatementLink to this section

The continue statement skips the current iteration of a loop and moves to the next iteration. Unlike break, it does not terminate the loop.

note

continue is useful when certain values should be ignored during iteration.

return StatementLink to this section

The return statement is used to exit from a method and optionally send a value back to the caller. Once return is executed, the remaining method code is skipped.

warning

Any code written after a return statement inside a method becomes unreachable.

Difference Between break, continue, and returnLink to this section

Understanding the differences helps avoid logical errors:

  • break exits a loop or switch
  • continue skips the current loop iteration
  • return exits a method completely
Branching Statements in Java
Question 1 of 5

Which statement is used to exit a loop completely?

Branching Statements in Java

medium
  1. Write a program that stops printing numbers when it encounters a specific value using break.
  2. Create a loop that skips all even numbers using continue.
  3. Write a method that checks whether a number is prime and returns true or false.
  4. Demonstrate the difference between break and continue using the same loop logic.
  5. Identify a real-world use case where return improves program efficiency.