Extension 4: What goes Inside the if() Statement


The if() statement tests for a boolean (true or false) condition, and executes the following code accordingly. The condition that falls within the parentheses is the boolean condition that determines which code path your program will take. But what exactly can go inside the inside the parentheses? As it turns out, any operator-based expression can be used:

  • The double-equals (==): The double-equals is used for comparison (x==5 is logically the same as “x is equal in value to 5″, while the single equals, x=5, assigns the value of 5 to the variable x). This can be used to test for the result of an expression:
    if(x % 2 == 0)     // If x is even
        NSLog(@"x is even.");
    else
        NSLog(@"x is odd.");
    

    Make sure that you use the double equals for a comparison; a single equals is only used for assignment, and unless you are assigning zero to a variable, the expression will always be true (because any non-zero expression is considered true).

  • The not equal to (!=): The not equal to operator is the exact opposite of the double-equals; if the left part is not equal to the right, then execute the following code.
    if (x != 5)
         NSLog(@"x does not equal 5.");
    
  • The Greater-than, Less-than (<, >, <=, >=): These operators work just like they do in real life. They are the less than, greater than, less than or equal to, and greater than or equal to operators, which have not changed since high-school algebra.
    if (x > 5)
         NSLog(@"x is greater than 5.");
    else if (x < 5)
         NSLog(@"x is less than 5.");
    else
         NSLog(@"x is equal to 5.");
    
  • Boolean Operators (&&, ||, !): Three symbols (or pairs of symbols) can also be used to express a boolean condition. These are the and (&&), the or (||), and the not (!). These are just as logical as the if() statement itself:
    if ((x >= 5) && (y < 10))
     	NSLog(@"x is greater than or equal to 5, AND y is less than 10.");
     if (x == 3 || y == 6)
     	NSLog(@"x is equal to 3 OR y is equal to 6");
     if (!(x > 2 && y < 5))
    	NSLog(@"x is NOT greater than 2 AND y is NOT less than 5");
    

    See also this excellent document on Scribd, and of the course the Wikipedia entry for a more technical description of what can be achieved with boolean operators.

When using these expressions in conjunction, don’t hesitate to use parentheses or spaces to make your purpose clear. Although the operator precedence table tells us that the internal parentheses were not needed in the last example, their presence helps clarify the logical intent.

By the way, the Conditional Operator is an oddity (among operators) that could save you a bit of typing:

http://cupsofcocoa.wordpress.com/2010/10/26/oddities-conditional-operator/

About these ads
Leave a comment

6 Comments

  1. Connor

     /  March 24, 2012

    GNUstep shell says error expected identifier of ‘(‘ before ‘{‘ token.
    X is undeclared
    Cannot find interface declaration for NXConstantString.
    What is the problem.

    #import
    int main(int argc, const char *argv[]){
    int result;
    result = 45 * 89;
    printf(“Hello World, Hello World!\n Hello\nThe product of 45 and 89 is %i”,result);
    result = 14 – 2 * 3 + 4 * 5;
    printf(“\n The answer to 14 – 2 * 3 + 4 *5 is %i”, result);
    int a = 45;
    int b = 2;
    result = a * b;
    printf(“\n The answer to a * b, if a = 45 and b = 2 is %i”, result);

    if (x > 5)
    NSLog(@”x is greater than 5.”);
    else if (x < 5)
    NSLog(@"x is less than 5.");
    else
    NSLog(@"x is equal to 5.");

    return 0;
    }

    Reply
    • The issue is with the block of if() statements you have at the bottom. You have to declare x, just like you did with result, a, and b. For example:

      int x = 6;
      if (x > 5)
      NSLog(@”x is greater than 5.”);
      else if (x < 5)
      NSLog(@"x is less than 5.");
      else
      NSLog(@"x is equal to 5.");

      Reply
  2. Connor

     /  March 24, 2012

    GNUstep compiler still says “cannot find interface declaration for NXContantString “

    Reply
  1. Objective-C Lesson 5: Loops « Programming for iOS
  2. Learn Objective-C in 24 Days « Programming for iOS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

  • Welcome

    My goal is to make CupsOfCocoa into a beautiful source for beginners to the iPhone platform to get started. Subscribe below for more, and stay tuned!

  • Contact Me

    If you need to contact me for any reason, feel free to send me an email.
  • The Giving Spirit

    If you've found this site helpful, would you consider donating a little sum? Any amount is appreciated...Thanks so much!

  • Roadmap

  • Enter your email address to follow this blog and receive notifications of new posts by email.

    Join 226 other followers

  • Back to the Past

    November 2010
    S M T W T F S
    « Oct   Dec »
     123456
    78910111213
    14151617181920
    21222324252627
    282930  
  • Time Machine

  • You count!

    • 590,430 views
  • Worldwide Stats

    free counters
Follow

Get every new post delivered to your Inbox.

Join 226 other followers

%d bloggers like this: