Program Flow  «Prev  Next»
Lesson 2Conditional Branching Basics
ObjectiveLearn how to control program flow based on a condition in Java

Conditional Branching Basics in Java

Question: How do branches in Java allow a program to alter the flow of execution and run one section of code instead of another?
In Java, branches, also known as conditional statements, allow a program to alter the flow of execution by running one section of code instead of another based on the evaluation of a specific condition. This enables a program to make decisions and execute different code paths depending on the circumstances. The most common branching constructs in Java are if, if-else, and switch.
  1. if statement: The if statement evaluates a boolean expression (a condition), and if the condition is true, it executes the block of code within the statement. If the condition is false, the program skips the if block and continues with the next line of code after the if statement.
    int x = 10;
    
    if (x > 5) {
        System.out.println("x is greater than 5");
    }
    
  2. if-else statement: The if-else statement extends the if statement by providing an alternative block of code to be executed when the condition is false. This allows the program to choose between two different code paths based on the evaluation of a condition.
    int x = 3;
    
    if (x > 5) {
        System.out.println("x is greater than 5");
    } else {
        System.out.println("x is less than or equal to 5");
    }
    


  3. if-else-if ladder: The if-else-if ladder is used when there are multiple conditions to be checked. The program checks each condition in sequence, and if a condition is true, it executes the associated block of code and skips the remaining conditions.
    int x = 15;
    
    if (x < 10) {
        System.out.println("x is less than 10");
    } else if (x < 20) {
        System.out.println("x is between 10 and 19");
    } else {
        System.out.println("x is 20 or greater");
    }
    
  4. switch statement: The switch statement is used to select one of many code paths based on the value of an expression. It is an alternative to if-else-if ladder when there are multiple conditions based on a single value or expression.
    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;
        case 4:
            System.out.println("Thursday");
            break;
        case 5:
            System.out.println("Friday");
            break;
        case 6:
            System.out.println("Saturday");
            break;
        case 7:
            System.out.println("Sunday");
            break;
        default:
            System.out.println("Invalid day");
    }
    
These branching constructs in Java allow a program to make decisions and execute different sections of code based on specific conditions or values. By altering the flow of execution, branches enable more complex and dynamic behavior in Java programs.

Branches allow a Java program to alter the flow of execution and run one piece of code instead of another. The figure below shows how a Java program flows without branches:
Java program without branches
Java program without branches

A Java program without branches executes one statement at a time from start to end. Although technically there is nothing wrong with the program in the figure, it is extremely limited in terms of what it can do. More specifically, there is no way for it to take a different path of execution. The following figure shows how a branch gives the program more freedom:
Java program with branches
Java program with branches containing a conditional branch.


The if-else statement

The if-else statement allows you to conditionally execute one section of code or another. Because of its simplicity and wide range of usefulness, the if-else statement is the most commonly used branch in Java.
The syntax for the if-else statement follows:
if (Condition)
  Statement1
else
  Statement2

If the Boolean Condition is true, Statement1 is executed; otherwise Statement2 is executed.
Following is a simple example:
if (tired)
 timeForBed = true;
else
 timeForBed = false;


If the Boolean variable tired is true, the first statement is executed and timeForBed is set to true.
Otherwise the second statement is executed and timeForBed is set to false.
The else part of the if-else statement is optional. In other words, you can leave it off if you have only a single statement that you want to execute conditionally.
It is also possible to link together a series of if-else statements to get the effect of multiple branches.
Variables: Variables are locations in memory that are used to store information. You can think of variables as storage containers designed to hold data of a certain type.
You are not limited to executing an individual line of code with the if-else statement. To execute multiple lines of code you can use a compound statement, which is a block of code surrounded by curly braces ({}). An if-else statement interprets a compound statement as a single statement. Following is an example that demonstrates using a compound statement:

if (printVitals) {
  System.out.println("Pulse rate: " + pulse);
  System.out.println("Blood pressure: " + pressure);
  System.out.println("Temperature: " + temp);
}

SEMrush Software