Java supports the usual logical conditions from mathematics:
- Less than: a < b
- Less than or equal to: a <= b
- Greater than: a > b
- Greater than or equal to: a >= b
- Equal to a == b
- Not Equal to: a != b
In programming, it's often desirable to execute a certain section of code based upon whether the specified condition is true or false during the run time. For such cases, control flow statements are used. There are various types of if statement in java.
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
The Java if statement tests the condition. It executes the if block if condition is true.
The if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.
Using Ternary Operator
We can also use the ternary operator (? :) to perform the task of if...else statement. It is a shorthand way to check the condition. If the condition is true, the result of ? is returned. But, if the condition is false, the result of : is returned.
if-else-if ladder Statement
The if-else-if ladder statement executes one condition from multiple statements.
Nested if statement
The nested if statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true.