# Challenge

is this a something we put in Xcode or just answer?

You don’t enter it.

Is the challenge answer The second expression is true.

According to two pages before the Challenge:

[size=85][quote]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.
Hillegass, Aaron (2013-11-20). Objective-C Programming: The Big Nerd Ranch Guide (2nd Edition) (Big Nerd Ranch Guides) (Kindle Locations 773-775). Pearson Education. Kindle Edition. [/quote][/size]

If you analyze k, the value is 5 (If i > j then 10, else 5), therefore both the first (5 < 25 - 5) and second (25 > 20) expressions are true. Since the test ends when the first expression evaluates as true, the console should print “The First expression is true.”

1 Like

Is there somewhere where we can find the correct answers to the challenges? I love that we have challenges, yet find it not useful unless we know what the answer is. I’d hate to think I’m right about something and then find out I’m wrong when I actually try to code.

I agree with the one who said that the 2nd answer is true. Who’s going to give us the real answer?

Billygoat,

Re-read what ghijkmnop had copied an pasted into their answer. It took me a minute, be cause the math shows that both the first and second expression are true.

In his snipit from the book he relays. “They well each be tested in the order in which they appear until one evaluates as true” then the code stops.

To test this statement, type in the code and switch the first and second statements. When you run it you should see that now it shows the “Second Expression is true”

I posted the re-arrangement below:

[code]int i = 20;
int j = 25;
int k = (i > j) ? 10 : 5;

``````if ( j > i ){//second expression
printf("the second expression is true.");
}else if (5 < j - k) { //first expression
printf("the first expression is true.");
}else {
printf("neither expression is true.");
}[/code]``````

I know there is no such thing as a stupid question but this might be. Can someone please explains where the 10 : 5 came from? How is it relevant?

The colon is part of the ternary operator. It’s a sort of shorthand for an if-then-else statement.
Take this line of code for example…

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

It is identical in operation to this…

``````if (i > j) {
k = 10;
} else {
k = 5;
}``````

If it helps you to verbalize things, you can say it like this…

In the case above, he used the ternary operator as an assignment statement to initialize the variable “k”.

1 Like

I also have a problem on this challenge.

My understanding is like this:

k is assigned under the following circumstances: if i, which is 20, is bigger than j, which is 25, then k is 10, if not then k is 5. since j is 25 which is bigger than i, this statement is false, thus k is 5. Correct?

Now we enter the if statement:

if 5 is smaller than (j 25 - k 5) then “The first expression is true.” But how come 5 is smaller than 20? So it must be false right, that’s why we have to go to the next else if.

Else if j 25 is bigger than i 20, then “The second expression is true.” j 25 is indeed bigger than i which is only 20. So based on this, my assumption is the correct answer should be “THE SECOND EXPRESSION IS TRUE.” Right? Or am I wrong?

But when I copied the whole thing and run it, the console says “The first expression is true.”

So now I’m confused. Which one of my assumption is wrong? Help please.

Substitute to help visualize:

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

if ( 5 < 25 - 5 ) { // First expression
printf("The first expression is true.");
} else if ( 25 > 20 ) { // Second expression
printf("The second expression is true.");
} else {
printf("Neither expression is true.");
}``````

For the expressions, 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 first expression is true.

Question:

[code]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.”);
}[/code]

[ul]20>25 is false thus the condition (i>j) is false.
Hence k is not assigned to 10 but 5.
The value of k is 5.
(j-k) is 25-5 which is 20. The condition (5<20) is true.
Thus the console prints:[/ul]

[ul]Now the second condition is (j>i) which is (25>20).
Even though this is true the message here is not printed because the test ends whenever a conditional is true. Only the code for that conditional is printed even if the stuff following is true later.[/ul]

Please correct me if I’m wrong.

Thanks for this buzkil. When I looked at the challenge initially, I obviously checked the first expression first and when I saw that it was true I didn’t think to check if the other statements might be true also!
So then I typed it up in a little program and confirmed that it was the ‘first expression’. So grateful to this forum as I found out then that another of the statements evaluated to true also - I feel so silly for not thinking of that possibility at the start.

Thanks a mil!!

[quote=“buzkil”]Billygoat,

Re-read what ghijkmnop had copied an pasted into their answer. It took me a minute, be cause the math shows that both the first and second expression are true.

In his snipit from the book he relays. “They well each be tested in the order in which they appear until one evaluates as true” then the code stops.

To test this statement, type in the code and switch the first and second statements. When you run it you should see that now it shows the “Second Expression is true”

I posted the re-arrangement below:

[code]int i = 20;
int j = 25;
int k = (i > j) ? 10 : 5;

``````if ( j > i ){//second expression
printf("the second expression is true.");
}else if (5 < j - k) { //first expression
printf("the first expression is true.");
}else {
printf("neither expression is true.");
}[/code][/quote]``````

Hello, Dears

My thought of the challenge is following.

{

``````int i = 20;
int j = 25;
int k = ( i > j ) ? 10 : 5; //if i > j, that means int k = 10; (1)
// if i < j, that means int k = 5; (2)
// we have (2), that means k = 5

if ( 5 > j - k ) { // First expression, Since k = 5, we have 5 < 25-5 -> 5 < 20. That expression is true!
printf("The first expression is true.");
} else if ( j > i ) { // Second expression. Since 25>20, This express is true too.
printf("The second expression is true.");
} else {
printf("Neither expression is true.");
};

//Since we have both expressoions true, the resut will be first true expression.
//That means the console will say THE FIRST EXPRESSTION IS TRUE.
//If we change '<' to '>' in the FIRST expression, the first expression will become false.
//But the second expression will remain TRUE. At exactly that case the console will say.
//The second expression is true.
``````

}

When I enter this challenge into my Xcode version 8.3.1 it compiles and runs but does not print anything. Here is the code.

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

printf("i = %d\nj = %d\nk = %d\n", i, j, k); // to verify the variables are getting set properly

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");
}

return 0;
``````

The first printf is printing the values of the variables but nothing gets printed from the if else statements.

Ran the debugger and stepped through the program and it goes to the first printf in the if statement but does not print the message!

The first print statement differs from the the ones in the if statement. Can you see the difference?

If you try running your code from the command line, you will see the output from the other print statements.