Conditional Statements
Conditional statements are used to control the flow of execution of the Java compiler based on certain conditions. This implies that we are making a choice based on a certain value or the state of a program. The conditional statements that are available in Java are as follows:
The if statement
The if-else statement
The else-if statement
The switch statement
The if Statement
The if statement tests a condition, and when the condition is true, the code contained in the if block is executed. If the condition is not true, then the code in the block is skipped and the execution continues from the line after the block.
The syntax for an if statement is as follows:
if (condition) { //actions to be performed when the condition is true }
Consider the following example:
int a = 9; if (a < 10){ System.out.println("a is less than 10"); }
Since the condition a<10 is true, the print statement is executed.
We can check for multiple values in the if condition as well. Consider the following...