binpress

Learn Objective-C Lesson 4: if() statements and Booleans

As mentioned before, boolean values are simply true-or-false. In Objective-C, unlike many other languages, they are represented as YES or NO:

  1. BOOL trueOrFalse = YES;
  2. BOOL gameOver = NO;

Internally, however, they are stored as zero and one.

if() Statements

The if() statement is used to check for conditions. Just like we use if in normal English, if() in code is used to test for a condition—they test for the value of a boolean (or any int—in this case, a zero is considered false; any non-zero value is true).

Here is a simple example of booleans:

  1. #import <Foundation/Foundation.h>
  2.  
  3. int main (int argc, const char * argv[]) {
  4.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  5.  
  6.     BOOL trueOrFalse = YES;
  7.     if (trueOrFalse)
  8.         NSLog(@"trueOrFalse is true.");
  9.     if (1)
  10.         NSLog(@"1 is considered to be true.");
  11.     [pool drain];
  12.     return 0;
  13. }

The output is:

  1. trueOrFalse is true.
  2. 1 is considered to be true.
  3. Simple enough, and quite logical.

Obviously, if the condition was false, the statements following would not be executed. The following example demonstrates:

  1. #import <Foundation/Foundation.h>
  2.  
  3. int main (int argc, const char * argv[]) {
  4.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  5.  
  6.     BOOL trueOrFalse = YES;
  7.     if (trueOrFalse)
  8.         NSLog(@"trueOrFalse is true.");
  9.     if (1)
  10.         NSLog(@"1 is considered to be true.");    // If false, statement following is not executed
  11.     trueOrFalse = NO;
  12.     if (trueOrFalse)
  13.         NSLog(@"I lied. trueOrFalse is not true.");
  14.     if (0)
  15.         NSLog(@"This line should not appear.");
  16.     [pool drain];
  17.     return 0;
  18. }

Unsurprisingly, the results are the same as above.

One important point to make here: if you only have one statement after the if(), you can just leave it like it is above. But if you have more than one statement after the if(), you must enclose them within braces:

  1. if (trueOrFalse) {
  2.     NSLog(@"trueOrFalse is true.");
  3.     NSLog(@"This second line must be within the braces.");
  4. }

Otherwise, the second (or additional) statements will be executed, regardless of if the condition was true or not.

Extending the if() Statement In real life though, there are often alternatives: if something is true, do “action1“; else, do “action2“. Objective-C lets you model that quite simply:

  1. trueOrFalse = YES;
  2. if (trueOrFalse == YES)          // The double-equals sign is a comparison; versus a single equals, which is an assignment. More on this in the next Extension.
  3.     NSLog(@"If true, print this");     // This gets printed
  4. else
  5.     NSLog(@"Else, print this");        // This does not get printed
  6. trueOrFalse = NO;
  7. if (trueOrFalse)
  8.     NSLog(@"If true, print this");    // This does not get printed
  9. else
  10.     NSLog(@"Else, print this");       // This gets printed

Output is:

  1. If true, print this
  2. Else, print this
  3. This makes logical sense.

You can also extend this by using else if(). This is easier to explain with code:

  1. int value = 5;
  2.     if (value > 0)
  3.         NSLog(@"value is greater than zero.");
  4.     else if (value == 0)
  5.         NSLog(@"value is equal to zero.");
  6.     else
  7.         NSLog(@"value is less than zero.");

Output is:

value is greater than zero.

You can have as many else if()s as you want; they simply follow each other:

  1. int value = 5;
  2. if (value == 6)
  3.     NSLog(@"value is equal to 6.");
  4. else if (value == 0)
  5.     NSLog(@"value is equal to 0.");
  6. else if (value == 2)
  7.     NSLog(@"value is equal to 2.");
  8. else if (value == 10)
  9.     NSLog(@"value is equal to 10.");
  10. else
  11.     NSLog(@"value does not equal 6, 0, 2, or 10");     // This line is the output.

Pitfalls

There are a few issues that may arise with if()statements.

  • Forgetting braces: If you have more than one statement that you want to execute given a certain condition, you must enclose them within curly braces.
  • Using too many instances of if(): If you want to have a collection of related paths (if…else if…else), you must remember to use else if(). Using a chain of if()s is a completely different thing, logically. Think about it.
  • Forgetting the last else: The final else is a “catchall” statement that is executed if none of the previous if() or else if() statements are true. Don’t forget the else; otherwise, you may never get any output. For example, in the last code example, the last else statement was needed; otherwise, it might have appeared that the code was broken.

Conclusion

As you can see, the if() statement is quite simple, but very powerful—it defines “paths” down which your code can travel, based on the value of a condition. In the next Extension, we will be looking at what these conditions can be.

This post is part of the Learn Objective-C in 24 Days course.

Author: Feifan Zhou

Scroll to Top