So far we’ve gotten comfortable with the idea that code executes sequentially, one line after another. However what do we do if we want to execute a part of our code only if certain conditions are met? That’s where branching comes in. The statements used for branching are also known as conditional statements.
Generally branching statements come with conditions. The code inside a branch will only execute if the condition inside evaluates to be true.
<aside>
Comparative and logical operators are an essential part of branching, review it if you have any confusion regarding operators.
</aside>
int a = 5;
int b = 2;
// code before the if
if (a > b) {
System.out.println("The conditions have been met");
}
// code after the if
For this segment take the liberty to imagine a bunch of code before and after the if statement. All the code before the if will execute (assuming there’s no branch statements there) when we reach the if statement there is a decision to make. Either the condition inside evaluates to true or it evaluates to false. In this case:
But what if b was 5 and a was 2 instead?
If we were to represent our code as roads, the code without any branching statements will look like this:

There is no choice to make about what path we will take. We will simply move from top to bottom, executing all lines one by one.
The code (or “road”) with our if statement will look like this:

When we get to the if path there are two paths we can take.
if statement is true → we take the circular road, dealing with everything on that path (that is executing all code inside the if block).