Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [10]
BOOL isNotLegal = !((truckWeight > 0.0) && (truckWeight < 40000.0));
if (isNotLegal)
printf("Truck weight is not within legal range.\n");
However, the curly braces are necessary if the code consists of more than one statement.
BOOL isNotLegal = !((truckWeight > 0.0) && (truckWeight < 40000.0));
if (isNotLegal) {
printf("Truck weight is not within legal range.\n");
printf("Impound truck.\n");
}
Why? Imagine if you removed the curly braces.
BOOL isNotLegal = !((truckWeight > 0.0) && (truckWeight < 40000.0));
if (isNotLegal)
printf("Truck weight is not within legal range.\n");
printf("Impound truck.\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.)
else if
What if you have more than two possibilities? You can test for them one-by-one using else if. For example, imagine that a truck belongs to one of three weight categories: floating, light, and heavy.
if (truckWeight <= 0) {
printf("A floating truck\n");
} else if (truckWeight < 40000.0) {
printf("A light truck\n");
} else {
printf("A heavy truck\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 your conditions so that you don’t get a false positive. For instance, if you swapped the first two tests in the above example, you would never find a floating truck because floating trucks are also light trucks. The final else clause is optional, but it’s useful when you want to catch everything that did not meet the earlier conditions.
For the More Curious: Conditional (ternary) operator
It is not uncommon that you will use if and else to set the value of an instance variable. For example, you might have the following code:
int minutesPerPound;
if (isBoneless)
minutesPerPound = 15;
else
minutesPerPound = 20;
Whenever you have a scenario where a value is assigned to a variable based on a conditional, you have a candidate for the conditional operator, which is ?. (You will sometimes see it called the ternary operator).
int minutesPerPound = isBoneless ? 15 : 20;
This one line is equivalent to the previous example. Instead of writing if and else, you write an assignment. The part before the ? is the conditional. The values after the ? are the alternatives for whether the conditional is found to be true or false.
If this notation strikes you as odd, there is nothing wrong with continuing to use if and else instead. I suspect over time you will embrace the ternary operator as a concise way to do conditional value assignment. More importantly, you will see it used by other programmers, and it will be nice to understand what you see!
Challenge
Consider the following code snippet:
int i = 20;
int j = 25;
int k = ( i > j ) ? 10 : 5;
if ( 5 < j - k ) { // first expression
printf("The first expression is true.");
} else if ( j > i ) { // second expression
printf("The second expression is true.");
} else {
printf("Neither expression is true.");
}
What will be printed to the console?
5
Functions
Back in Chapter 3, I introduced the idea of a variable: a name associated with a chunk of data. A function is a name associated with a chunk of code. You can pass information to a function. You can make the function execute code. You can make a function return information to you.
Functions are fundamental to programming, so there’s a lot in this chapter – three new projects, a new tool, and many new ideas. Let’s get started with an exercise that demonstrates what functions