The else if Construct
You have seen how the else
statement comes into play when you have a test against two possible conditions: Either the number is even or it is odd; either the year is a leap year or it is not. However, programming decisions you have to make are not always so black and white. Consider the task of writing a program that displays –1
if a number the user types is less than zero, 0
if the number is equal to zero, and 1
if the number is greater than zero. (This is actually an implementation of what is commonly called the sign function.) Obviously, you must make three tests in this case to determine whether the number that is keyed in is negative, zero, or positive. The simple if-else
construct will not work. Of course, in this ...
Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.