4 if/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 will not create a ...

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.