Java If else Statement
Learners can benefit from this post to better understand Java If Else Statement with examples. Java if statement tests the condition.
True or false is checked based on a boolean condition. A Java if statement can be of various types.
- if statement.
- if-else statement.
- if-else-if ladder.
- nested if statement.
Java If else Conditions
In terms of Java if else conditions, Java supports the usual logical conditions from mathematics:
- Less than: mrx < ample
- Less than or equal to: mrx <= ample
- Greater than: mrx > ample
- Greater than or equal to: mrx >= ample
- Equal to : mrx == ample
- Not Equal to: number 1 != ample
These conditions can be used to perform different actions for different decisions.
In Java, conditional statements are as follows:
- Using if, a block of code is executed if a condition is met.
- The else statement specifies a block of code that will run if the same condition is not true.
- If the first condition is false, use else if to test another condition.
- Switches can be used to specify a variety of alternative code blocks
if Statement Java
For Java if else conditions, use the if statement to specify how Java code will be executed if a condition is met.
if Syntax:
if (Checking Condition) { // If the condition is true, execute this block of code. }
if must be written in lowercase. For if Statement in Java, uppercase letters (If or IF) will generate an error.
Below is an example of testing two values to see if mrx is greater than ample . Whenever the condition is true, print the following text:
Example: 
Example: 
Variables can also be tested:
Example: 
Above Example Explanation:
The example above uses two variables, mrx and ample, to test whether ample is greater than mrx (using the greater than ( > ) operator). As mrx is 10, and ample is 20, and we know that 20 is greater than 10, we print to the screen that “ample is greater than mrx”.
Example: 
The else Statement
As with Java if else conditions, you can specify a block of code to be executed if the condition is false by using the else statement.
Syntax
if (condition) { // code to be executed if the condition is satisfied } else { // code to be executed if the condition is not satisfied }
Example: 
Above Example Explanation:
The if condition is false in the case above because boys (40) are greater than girls (28).
As a result, we go on to the next condition and print “Boys are more in number than girls” on the screen.
The computer would print “Girls are more in number than boys”, if the number of boys were less than 28.
Example: 
The else if
In Java if else statements , we describe a new condition using the else if statement. If the initial condition is false.
Syntax:
if (condition1) { // if condition 1 is true, this block of code is executed } else if (condition2) { // if condition 1 is false and condition 2 is true, this block of code is executed. } else { // block of code that will run if both condition 1 and condition 2 are false }
Example: 
Above Example Explanation:
In the example above, marks (27) are greater than 0 and less than 20, so the first statement is false. The next condition, Whereas the else if statement is true as marks(27) are greater than equal to 20 and less than 35, hence the part of code in the else-if block is executed, so the program will not move to the else statement in this scenario.
However, if the marks were greater than 0 and less than 20, our program would print “You need to work hard to achieve good grades.” Similarly if the marks were greater than 35 , our program would print “Excellent Performance.”
Example: 
Java If-Else (Ternary Operator)
There is also a short-hand if-else, which is known as the ternary operator since it has three operands only. You can use it to replace multiple lines of code with one line. When writing Java if else conditions, it is often used to replace simple if-else statements:
Syntax:
variable = (checking_condition) ? expressionTrue : expressionFalse;
Rather than writing:
Example: 
The following can be written simply:
Example: 
Example: 
This code can also be simplify as:
Example: