In earlier article we have discussed about if statement that allow you to execute a set of statements if a condition or expression evaluates to true. If there is another course of action to be followed if the expression evaluates to false.
To resolve this, there are another form of if that allows for this kind of either-or condition by providing an else clause. Following is the syntax of if-else statement:
If (expression)
Statement 1;
Else
Statement 2;
According to above code fragment, if the expression evaluates to true statement-1 is executed, otherwise statement-2 is executed. These statements can be a single , compound or may be a null statement.
If (expression) {
If (expression)
Statement 1;
[Else
Statement 2;]
}
Else
Statement 2;
In if statement, either there can be if statement in its body of if or in its body of else or in both.
The part in [] is optional and inner if’s can themselves be nested ifs, but the inner if must terminate before an outer if.
If (condition ) statement 1;
Else
If (condition ) statement 2;
Else
If (condition ) statement 3;
Else statement 4;
………….
The expressions are evaluated from the top downward. As soon as an expression evaluates to true, the statement associated with it is executed and the rest of the ladder is bypassed. If none of the expressions are true, the final else gets executed. If the final else is missing, no action takes place if all other conditions are false.
Although it is technically correct to have indentation in the if-else-if ladder as shown above, however, it generally leads to overly deep indentation.
To resolve this, there are another form of if that allows for this kind of either-or condition by providing an else clause. Following is the syntax of if-else statement:
If (expression)
Statement 1;
Else
Statement 2;
According to above code fragment, if the expression evaluates to true statement-1 is executed, otherwise statement-2 is executed. These statements can be a single , compound or may be a null statement.
Nested If
A nested if is an if that has another if in its if’s body or in its else’s body. One of the example among nested if’s is in following form:If (expression) {
If (expression)
Statement 1;
[Else
Statement 2;]
}
Else
Statement 2;
In if statement, either there can be if statement in its body of if or in its body of else or in both.
The part in [] is optional and inner if’s can themselves be nested ifs, but the inner if must terminate before an outer if.
If-Else-If ladder
A common programming construct in JAVA is the if-else-if ladder, which is often also called the if-else-if staircase because of its appearance. Following is the syntax:If (condition ) statement 1;
Else
If (condition ) statement 2;
Else
If (condition ) statement 3;
Else statement 4;
………….
The expressions are evaluated from the top downward. As soon as an expression evaluates to true, the statement associated with it is executed and the rest of the ladder is bypassed. If none of the expressions are true, the final else gets executed. If the final else is missing, no action takes place if all other conditions are false.
Although it is technically correct to have indentation in the if-else-if ladder as shown above, however, it generally leads to overly deep indentation.
No comments:
Post a Comment