# Thread: programming a "conditional" function

1. ## programming a "conditional" function

I'm supposed to write code for a function that does the following conditional statement:

x ? y : z

I found an explanation stating that this means:

If x is true, then execute y, otherwise execute z.

However, the example given is conditional(2,4,5) = 4

I don't see how there is a statement to evaluate to true or false.

I have no clue why the answer is 4, can anyone explain it?

2. ## I am sure someone will answer before me

They are all pretty excited about simple problems that newbies have

I will give you a hint: to implement your function just put return in front of the conditional statement.

3. That doesn't help me understand how conditional works...

P.S. I'm not so much a newbie. Most of my experience is with C++ and not C and I have never encountered this conditional function before so I'm looking for a good explanation of what it does...

4. The answer is 4 because, in C, every value different than 0 is "true". In your example, x is, indeed, different than 0, so the answer is 4. If it were zero, the answer would be 5.

5. Originally Posted by manav
They are all pretty excited about simple problems that newbies have
Guess what! I am also pretty excited about it

6. Originally Posted by Mr_Miguel
The answer is 4 because, in C, every value different than 0 is "true". In your example, x is, indeed, different than 0, so the answer is 4. If it were zero, the answer would be 5.
Got it. Thank you.

EDIT: Do negative numbers still count as true or does it have to be greater than 0?

7. Yes, negative numbers are also evaluated as "true".

8. Thank you.

Popular pages Recent additions