Nested if and if-else statements
The if-else
statement allows a choice to be made between two possible alternatives. Sometimes a choice must be made between more than two possibilities. For example the sign function in mathematics returns -1 if the argument is less than zero, returns +1 if the argument is greater than zero and returns zero if the argument is zero. The following C++ statement implements this function:
if (x < 0) sign = -1; else if (x == 0) sign = 0; else sign = 1;
This is an if-else
statement in which the statement following the else
is itself an if-else
statement. If x
is less than zero then sign
is set to -1, however if it is not less than zero the statement following the else
is executed. In that case if x
is equal to zero then sign
is set to zero and otherwise it is set to 1.
Novice programmers often use a sequence of if
statements rather than use a nested if-else
statement. That is they write the above in the logically equivalent form:
if (x < 0) sign = -1; if (x == 0) sign = 0; if (x > 0) sign = 1;
This version is not recommended since it does not make it clear that only one of the assignment statements will be executed for a given value of x
. Also it is inefficient since all three conditions are always tested.
http:/
#TC1017 #Mastery18 by Carlos Adrian is licensed under a Creative Commons Attribution 4.0 International License.