O'Reilly logo

Objective-C Programming: The Big Nerd Ranch Guide by Aaron Hillegass

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

4if/else

An important idea in programming is taking different actions depending on circumstances. Have all the billing fields in the order form been filled out? If so, enable the Submit button. Does the player have any lives left? If so, resume the game. If not, show the picture of the grave and play the sad music.

This sort of behavior is implemented using if and else, the syntax of which is:

i​f​ ​(​c​o​n​d​i​t​i​o​n​a​l​)​ ​{​
 ​ ​ ​ ​/​/​ ​e​x​e​c​u​t​e​ ​t​h​i​s​ ​c​o​d​e​ ​i​f​ ​t​h​e​ ​c​o​n​d​i​t​i​o​n​a​l​ ​e​v​a​l​u​a​t​e​s​ ​t​o​ ​t​r​u​e​
}​ ​e​l​s​e​ ​{​
 ​ ​ ​/​/​ ​e​x​e​c​u​t​e​ ​t​h​i​s​ ​c​o​d​e​ ​i​f​ ​t​h​e​ ​c​o​n​d​i​t​i​o​n​a​l​ ​e​v​a​l​u​a​t​e​s​ ​t​o​ ​f​a​l​s​e​
}​

You won’t create a project in this chapter. Instead, consider ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required