Amazon.com Widgets 1st day with ObjC - HELP
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 10:25:00 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Newbie Corner
| | |-+  1st day with ObjC - HELP
Pages: [1]   Go Down
Print
Author Topic: 1st day with ObjC - HELP  (Read 478 times)
clockwurk
Newbie
*
Posts: 2






« on: November 12, 2011, 06:06:05 PM »

Sorry in advance if this is a stupid question but, this is literally my 1st day learning this...

I'm reading a book and one of the challenges was to figure out what the output window will print out.
I've concluded that it would print "The first expression is true." and "The second expression is true.",
but it only printed "The first expression is true." and I don't understand why.

j is greater than i as well... so why doesn't it print both
"The first expression is true." and "The second expression is true."?

Thanks in advance for the help.


#include <stdio.h>
int main (int argc, const char * argv[])
{


    int i = 20;
    int j = 25;
    int k = ( i > j ) ? 10 : 5;
    if ( 5 < j - k ) {
        printf("The first expression is true.");
    } else if ( j > i ) {
        printf("The second expression is true.");
    } else {
            printf("Neither expression is true.");}
   
}


Logged
Waiting
Global Moderator
Full Member
*****
Posts: 103






« Reply #1 on: November 12, 2011, 06:55:43 PM »

I'm not too sure about how this line works.

  int k = ( i > j ) ? 10 : 5;

However when you run an if statement, if the condition is true the code in the brackets after is completed, it only runs the else part if the first statement is not true.

Hence you get the first output you expect but the else section does not run as you either run the code in brackets OR the else not both, so the next if isn't run so you don't get the 2nd output you expected.
Logged
clockwurk
Newbie
*
Posts: 2






« Reply #2 on: November 12, 2011, 09:14:35 PM »

Ok, got it. That makes sense. Thx! Smiley


As far as:
 int k = ( i > j ) ? 10 : 5;

According to the book, the "?" is asking if ( i > j ) is true or false.
If its true, then the value is 10. If false, the value is 5
Logged
Waiting
Global Moderator
Full Member
*****
Posts: 103






« Reply #3 on: November 12, 2011, 11:09:01 PM »

Ok, got it. That makes sense. Thx! Smiley


As far as:
 int k = ( i > j ) ? 10 : 5;

According to the book, the "?" is asking if ( i > j ) is true or false.
If its true, then the value is 10. If false, the value is 5

Ah right i understand it now, glad you understand the output now.
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.