The This is an Novice programmers often use a sequence of 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 http:\/ <\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":" Nested if and if-else statementsThe 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 functio… Continue reading if-else<\/code> 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:<\/p>\nif (x < 0)\n sign = -1;\nelse\n if (x == 0)\n sign = 0;\n else\n sign = 1;\n<\/pre>\n
if-else<\/code> statement in which the statement following the else<\/code> is itself an if-else<\/code> statement. If x<\/code> is less than zero then sign<\/code> is set to -1, however if it is not less than zero the statement following the else<\/code> is executed. In that case if x<\/code> is equal to zero then sign<\/code> is set to zero and otherwise it is set to 1.<\/p>\nif<\/code> statements rather than use a nested if-else<\/code> statement. That is they write the above in the logically equivalent form:<\/p>\nif (x < 0)\n sign = -1;\nif (x == 0)\n sign = 0;\nif (x > 0)\n sign = 1;\n<\/pre>\n
x<\/code>. Also it is inefficient since all three conditions are always tested.<\/p>\n