Java Control Statements
Java supports control statements that are used to control flow of the program's execution based on conditions. Here, in this section we will discuss about the most commonly used control statements:
- Nested if
1. Java If Statement
Java if statement is a conditional statement which tests a boolean expression and executes only if the condition is true. below is the syntax to declare if statement.
Java If Example
In the above example, we used two if statements, but only first one executes because its conditional expression returns true. As we said, if executes only when its condition is true.
2. Java If-Else Statement
Java if statement supports else statement alongside if which is optional. Else is a block which can be used to execute the statements when the if condition is false. Below is the syntax to declare if-else statement.
Java If-Else Example
3. Java Nested If Statement
We can put if statement inside another if to create nested if. Below is the syntax to declare nested if statement.
Java Nested If Example
4. Java If-Else-If Statement
Java allows to put conditional expression with else to create more conditional flow. It is used to execute one condition from multiple conditions. See the below syntax.
Java If-Else-If Example
Notice: We have multiple conditions here, but once a condition is satisfied all other conditions are not evaluated.