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 won’t create a project in this chapter. Instead, consider the code examples carefully based on what you’ve learned in the last two chapters.

Here’s an example of code using if and else:

f​l​o​a​t​ ​t​r​u​c​k​W​e​i​g​h​t​ ​=​ ​3​4​5​6​3​.​8​;​

/​/​ ​I​s​ ​i​t​ ​u​n​d​e​r​ ​t​h​e​ ​l​i​m​i​t​?​
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​(​"​I​t​ ​i​s​ ​a​ ​l​i​g​h​t​ ​t​r​u​c​k​​n​"​)​;​
}​ ​e​l​s​e​ ​{​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​t​ ​i​s​ ​a​ ​h​e​a​v​y​ ​t​r​u​c​k​​n​"​)​;​
}​

If you don’t have an else clause, you can just leave that part out:

f​l​o​a​t​ ​t​r​u​c​k​W​e​i​g​h​t​ ​=​ ​3​4​5​6​3​.​8​;​

/​/​ ​I​s​ ​i​t​ ​u​n​d​e​r​ ​t​h​e​ ​l​i​m​i​t​?​
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​(​"​I​t​ ​i​s​ ​a​ ​l​i​g​h​t​ ​t​r​u​c​k​​n​"​)​;​
}​

The conditional expression is always either true or false. In C, it was decided that 0 would represent false, and anything that is not zero would be considered true.

In the conditional in the example above, the < operator takes a number on each side. If the number on the left is less than the number on the right, the expression evaluates to 1 (a very common way of expressing trueness). If the number on the left is greater than or equal to the number on the right, the expression evaluates to 0 (the only way to express falseness).

Operators often appear in conditional expressions. Table 4.1 shows the common operators used when comparing numbers (and other types that the computer evaluates as numbers):

Table 4.1  Comparison operators

<Is the number on the left less than the number on the right?
>Is the number on the left greater than the number on the right?
<=Is the number on the left less than or equal to the number on the right?
>=Is the number on the left greater than or equal to the number on the right?
==Are they equal?
!=Are they not equal?

The == operator deserves an additional note: In programming, the == operator is what’s used to check for equality. We use the single = to assign a value. Many, many bugs have come from programmers using = when they meant to use ==. So stop thinking of = as the equals sign. From now on, it is the assignment operator.

Some conditional expressions require logical operators. What if you want to know if a number is in a certain range, like greater than zero and less than 40,000? To specify a range, you can use the logical AND operator (&&):

i​f​ ​(​(​t​r​u​c​k​W​e​i​g​h​t​ ​>​ ​0​.​0​)​ ​&​&​ ​(​t​r​u​c​k​W​e​i​g​h​t​ ​<​ ​4​0​0​0​0​.​0​)​)​ ​{​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​T​r​u​c​k​ ​w​e​i​g​h​t​ ​i​s​ ​w​i​t​h​i​n​ ​l​e​g​a​l​ ​r​a​n​g​e​.​​n​"​)​;​
}​

Table 4.2 shows the three logical operators:

Table 4.2  Logical operators

&&Logical AND -- true if and only if both are true
||Logical OR -- false if and only if both are false
!Logical NOT -- true becomes false, false becomes true

(If you are coming from another language, note that there is no logical exclusive OR in Objective-C, so we won’t discuss it here.)

The logical NOT operator (!) negates the expression contained in parentheses to its right.

/​/​ ​I​s​ ​i​t​ ​n​o​t​ ​i​n​ ​t​h​e​ ​l​e​g​a​l​ ​r​a​n​g​e​?​
i​f​ ​(​!​(​(​t​r​u​c​k​W​e​i​g​h​t​ ​>​ ​0​.​0​)​ ​&​&​ ​(​t​r​u​c​k​W​e​i​g​h​t​ ​<​ ​4​0​0​0​0​.​0​)​)​)​ ​{​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​T​r​u​c​k​ ​w​e​i​g​h​t​ ​i​s​ ​n​o​t​ ​w​i​t​h​i​n​ ​l​e​g​a​l​ ​r​a​n​g​e​.​​n​"​)​;​
}​

Boolean variables

As you can see, expressions can become quite long and complex. Sometimes it is useful to put the value of the expression into a handy, well-named variable.

B​O​O​L​ ​i​s​N​o​t​L​e​g​a​l​ ​=​ ​!​(​(​t​r​u​c​k​W​e​i​g​h​t​ ​>​ ​0​.​0​)​ ​&​&​ ​(​t​r​u​c​k​W​e​i​g​h​t​ ​<​ ​ ​4​0​0​0​0​.​0​)​)​;​
i​f​ ​(​i​s​N​o​t​L​e​g​a​l​)​ ​{​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​T​r​u​c​k​ ​w​e​i​g​h​t​ ​i​s​ ​n​o​t​ ​w​i​t​h​i​n​ ​l​e​g​a​l​ ​r​a​n​g​e​.​​n​"​)​;​
}​

A variable that can be true or false is a boolean variable. Historically, C programmers have always used an int to hold a boolean value. Objective-C programmers typically use the type BOOL for boolean variables, so that’s what we use here. (BOOL is just an alias for an integer type.)

A syntax note: if the code that follows the conditional expression consists of only one statement, then the curly braces are optional. So the following code is equivalent to the previous example.

B​O​O​L​ ​i​s​N​o​t​L​e​g​a​l​ ​=​ ​!​(​(​t​r​u​c​k​W​e​i​g​h​t​ ​>​ ​0​.​0​)​ ​&​&​ ​(​t​r​u​c​k​W​e​i​g​h​t​ ​<​ ​ ​4​0​0​0​0​.​0​)​)​;​
i​f​ ​(​i​s​N​o​t​L​e​g​a​l​)​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​T​r​u​c​k​ ​w​e​i​g​h​t​ ​i​s​ ​n​o​t​ ​w​i​t​h​i​n​ ​l​e​g​a​l​ ​r​a​n​g​e​.​​n​"​)​;​

However, the curly braces are necessary if the code consists of more than one statement.

B​O​O​L​ ​i​s​N​o​t​L​e​g​a​l​ ​=​ ​!​(​(​t​r​u​c​k​W​e​i​g​h​t​ ​>​ ​0​.​0​)​ ​&​&​ ​(​t​r​u​c​k​W​e​i​g​h​t​ ​<​ ​ ​4​0​0​0​0​.​0​)​)​;​
i​f​ ​(​i​s​N​o​t​L​e​g​a​l​)​ ​{​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​T​r​u​c​k​ ​w​e​i​g​h​t​ ​i​s​ ​n​o​t​ ​w​i​t​h​i​n​ ​l​e​g​a​l​ ​r​a​n​g​e​.​​n​"​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​m​p​o​u​n​d​ ​t​r​u​c​k​.​​n​"​)​;​
}​

Why? Imagine if you removed the curly braces.

B​O​O​L​ ​i​s​N​o​t​L​e​g​a​l​ ​=​ ​!​(​(​t​r​u​c​k​W​e​i​g​h​t​ ​>​ ​0​.​0​)​ ​&​&​ ​(​t​r​u​c​k​W​e​i​g​h​t​ ​<​ ​ ​4​0​0​0​0​.​0​)​)​;​
i​f​ ​(​i​s​N​o​t​L​e​g​a​l​)​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​T​r​u​c​k​ ​w​e​i​g​h​t​ ​i​s​ ​n​o​t​ ​w​i​t​h​i​n​ ​l​e​g​a​l​ ​r​a​n​g​e​.​​n​"​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​m​p​o​u​n​d​ ​t​r​u​c​k​.​​n​"​)​;​

This code would make you very unpopular with truck drivers. In this case, every truck gets impounded regardless of weight. When the compiler doesn’t find a curly brace after the conditional, only the next statement is considered part of the if construct. Thus, the second statement is always executed. (What about the indention of the second statement? While indention is very helpful for human readers of code, it means nothing to the compiler.)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset