Challenge Chapter 4 trouble


#1

#include <stdio.h>
#include <objc/objc.h>

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.”);

}

Line 4 error "initializer element is not a compile time constant
Line 5 error "expected identifier or “(”

What is causing these run time errors? My guess is The first expression is true will be printed.


#2

Technically, these are compiler errors, not run-time, since this code won’t compile.

All statements in C code must run inside functions; you dropped the main function definition.

Here’s what your code should look like:


#include <stdio.h>
#include <objc/objc.h>

int main (int argc, const char * argv[])
{    
    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.");
        
    }
}

#3

Thank You for the quick response. I was lost and confused. :slight_smile: It allowed me to do a little more research on the "int main (int argc, const char * argv[]) function.


#4

#include <stdio.h>
#include <objc/objc.h>

int main (int argc, const char * argv[])
{
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.”);

}
}

Ok. I run the above code and the code compiled but paused at the first “if” statement. Thread 1: Stopped at breakpoint 1. Nothing is printed.


#5

That means you unintentionally introduced a breakpoint by clicking in the vertical bar alongside your code (to the left). Very easy to do; look for a blue button-like icon to the left of the if statement (or thereabouts) and click on it again to disable it, or right-click to delete it (you can also simply drag it into the aether to remove it).

Once you’ve done that, you can run again, or select the continue icon (below your code, looks like a CD player play button).


#6

Thank You again. Very nice. I’m assuming that break points are to help debug sections of code.


#7

That’s correct. The book covers debugging with breakpoints later.


#8

No one posted, but the console prints " the first expression is true " when this code is run


#9

I was working through the chapter 4 challenge problem and encountered a problem.

Based on the example, k should be set to 5, and the second expression should be true. However, when I actually run the code, my terminal output shows the first expression as being true. The only way I can see the first expression as true is if the code was (5 <= j - k).

Am I missing something?

Thanks


#10

[quote=“brianb824”]I was working through the chapter 4 challenge problem and encountered a problem.

Based on the example, k should be set to 5, and the second expression should be true. However, when I actually run the code, my terminal output shows the first expression as being true. The only way I can see the first expression as true is if the code was (5 <= j - k).

Am I missing something?

Thanks[/quote]

Right, K is set to 5. You’re also correct in the second expression being true.
But the first expression is satisfied:

which is

The entire if / else if / else statement will break and stop after it finds a condition is met; in this case, the first expression.


#11

[quote=“theplum”][quote=“brianb824”]I was working through the chapter 4 challenge problem and encountered a problem.

Based on the example, k should be set to 5, and the second expression should be true. However, when I actually run the code, my terminal output shows the first expression as being true. The only way I can see the first expression as true is if the code was (5 <= j - k).

Am I missing something?

Thanks[/quote]

Right, K is set to 5. You’re also correct in the second expression being true.
But the first expression is satisfied:

which is

The entire if / else if / else statement will break and stop after it finds a condition is met; in this case, the first expression.[/quote]

Ok,so if I’m following this correctly, only 5 <25 is being evaluated , which is true, so the " - 5" part of the expression is never evaluated?
I tried a little trick to test this - I wrote
if 5 < (j - k)) …

So (j-k) , which is 25 -20 = 5. Therefore the if statement is

if ( 5 < 5) …

However, the output was still the fist statement was true.

But this isn’t true. If it were 5 <= 5 , then this statement would be true.
I thought the boolean operators < and > were very strict and weren’t inclusive?

Its getting late so I must be missing something really obvious here, so pardon my lack of neuronal function


#12

K is 5, not 20. So it’s reading:


#13

K is 5, not 20. So it’s reading:

theplum, thank you for taking the time to clear this up! I realized I was reading the code improperly (and hence I believed the first expression shouldn’t have been satisfied).
edit* So your explanation cleared it up. Thanks!


#14

Yes, you don’t actually need to do anything with this challenge besides work through the logic. If else will run until it finds a true variable and then stops. :smiley:


#15

Again this is my 3rd time reading the book after starting over once I got to the NSarrays chapter. I never understood how the first expression came out to be true until I found out what the ? stood for…duh! :astonished:


#16

What I don’t understand is: how is 5 < 5 true? To me, 5 = 5 is true, but not 5 < 5. What am I missing here?


#17

Where did you see that [color=#FF0000]5 < 5 is true[/color]?

Also when testing for equality, never use the syntax: [color=#FF0000]foo = bar[/color]; always use foo == bar.
So [color=#FF0000]5 = 5[/color] should be 5 == 5.


#18

Oh—duh…now Is see it. Thanks for the reply and the == comment. I was actually wondering If I should have written it that way, now I know…thanks!


#19

Thanks all, managing to grasp everything introduced so far :sunglasses:


#20

This cleared up my understanding too.

Thanks guys!