Member-only story
Hi everyone, welcome back. In this tutorial, we will be going over if-else statements. The else-if statements can be used to decide whether or not a block of code should be executed. This is great to use for tasks that include choices and decision making. The else-if statements will examine if a certain condition is true and if it is, it will execute the associated block of code with it. With this introduction out of the way, let’s get into it.
If Statement
Let’s start by seeing an example of an if statement and examine what is happening. Take a look at the example below:
int number = 1;
if (number == 1) {
System.out.println("Number is 1");
}Output:
Number is 1.
The first thing we did was create a variable and we set it to 1. Then we have our if statement which has a condition that checks if our created variable is equal to 1. If the condition is satisfied, it will execute the associated code block within the if statement, which is our println statement. As we can see in the output above, we can verify that our code block within the if statement is successfully being executed.
Else-If Statement
Now let’s say we want more than one condition with different code blocks that get…