This article presents a comprehensive overview of the following concepts:
- What is If, if-else, and else if statement
- Basic syntax of If, if-else, and else if statements in Java
- How to Use If, if-else, and else if statements in Java
Let’s start!
What is if Statement in Java
It is used to test whether the condition is true or not and as a result it returns a Boolean value. The code within the body of “if-statement” executes only when the Boolean expression returns a true value.
Syntax
The basic syntax of the “if statement” will look something like this:
{
//statement(s);
}
The code written inside the body of the if-statement will execute only if the condition is true.
How if statement works
The below snippet shows how if-statement works in Java.
Example
This example takes a value from the user and prints “You Entered an Even Number” if the entered number is divisible by 2.
The above code snippet provides the following output:
Now, what if someone enters an odd number, how if statement deals with a false condition? Let’s try it!
The output shows that the if-statement doesn’t deal with the false statements.
What is if-else Statement in Java
To address the false statements Java provides else statement. So the combination of if and else statements can tackle both true and false statements.
Syntax
The basic syntax of the if-else statement will be:
{
statement or block of statements; //condition = true
}
else
{
statement or block of statements; //condition = false
}
How if-else works in Java
Let’s extend the above example a little bit more and to test how else statement works:
Example
The below snippet will execute the body of if-statement when the number is divisible by 2 and an else statement will execute if the number is not divisible by 2:
Now if a user will enter an odd number then it will be addressed in the else part:
Let’s move one step further to understand what is else-if and how it works in Java.
What is else-if in Java
Now, what if we want to perform multiple tasks based on the different conditions? Well! In java, we can use the else-if statement in such situations. The below snippet shows the basic syntax of else-if statement in java:
{
statement(s);
}
else if(condition)
{
statement(s)
}
else
{
statement(s)
}
Example
Let’s consider an example that takes an input from the user:
When the user entered number is divisible by 3 then it prints “the entered number is divisible by 3”
When the number is divisible by 5 then it prints “the entered number is divisible by 5”
else it should print “you entered a number neither divisible by 3 nor by 5”:
double number;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a Number");
number = scan.nextInt();
if(number%3 ==0)
{
System.out.println("The number is divisible by 3");
}
else if(number%5 ==0)
{
System.out.println("The number is divisible by 5");
}
else
{
System.out.println("You entered a number that is neither divisible by 3 nor divisible by 5");
}
}
The above code snippet provides the following output:
The output authenticates that the above given code is working properly.
Conclusion
The Java if statement gets executed only if the specified condition is true, on the other hand, the else condition gets executed if the condition is false. Moreover, to tackle more than two conditions “else if” can be used. This article presents a thorough overview of what is if-else, and else-if statements and how to use these statements in Java.