else if

What if you have more than two possibilities? You can test for them one by one using else if. For example, suppose a truck belongs to one of three weight categories: floating, light, and heavy.

i​f​ ​(​t​r​u​c​k​W​e​i​g​h​t​ ​<​=​ ​0​)​ ​{​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​A​ ​f​l​o​a​t​i​n​g​ ​t​r​u​c​k​\​n​"​)​;​
}​ ​e​l​s​e​ ​i​f​ ​(​t​r​u​c​k​W​e​i​g​h​t​ ​<​ ​4​0​0​0​0​.​0​)​ ​{​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​A​ ​l​i​g​h​t​ ​t​r​u​c​k​\​n​"​)​;​
}​ ​e​l​s​e​ ​{​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​A​ ​h​e​a​v​y​ ​t​r​u​c​k​\​n​"​)​;​
}​

You can have as many else if clauses as you wish. They will each be tested in the order in which they appear until one evaluates as true. The in the order in which they appear part is important. Be sure to order ...

Get Objective-C Programming: The Big Nerd Ranch Guide 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.