# Very simple, possibly trick question.

This is a discussion on Very simple, possibly trick question. within the C Programming forums, part of the General Programming Boards category; Re-reading the question, I think you're right laserlight. Disgusting question, though. It can be interpreted in both ways though. Because ...

1. Re-reading the question, I think you're right laserlight. Disgusting question, though. It can be interpreted in both ways though. Because of course, such an if statement is useless and it should be changed by removing the if statement completely, as it always evaluates to true. So what should it be changed to. The first statement is written in C...

Well... Like I said, I guess your assumption is right. I probably would have misread the question in highschool/university and answered E. Usually those misreads would cost me a lot of points. But in this case, I would've kicked the professor's ass for telling me my answer was wrong.

2. LOL ill be sure to ASK about any poorly written questions on the FINAL exam!

3. 16) Which of the following is true of the while statement?

A)When executed, its loop body always gets executed at least once.
B)When executed, its loop body always gets executed at least twice.
C)They must be written with curly braces.
D)When executed, its loop body can be executed at most 100 times.
E)When executed, its loop body has the potential to get executed more than 100 times.

Am I correct?
My logic is while loops are not going to be executed if the conditions are never met (for example while (x > 10) if x= 1 then the loop will never execute)

So A, and B are incorrect...

C says that while loops NEED to be written with curly brackets, which is false.

D says that it can be executed at most 100 times but that is veryyy hard to believe b/c the whole point of using programming for solving complex time consuming mathematical equations is to be able to calculate a ridiculous number of times at a ridiculous speed, and if it had a limit of 100 that wouldnt be cool!!! S0 I would say thats false and I am 95% sure this is false.

Therefore, E.

4. That sounds correct to me, some arbitrary while loop.

5. What will this segment of code return for cnt?

cnt=(-1 < (1< 0));

A)-1
B)0
C)1
D)2
E)None of the Above

I dont understand the logic behind it when I tried compiling and running it with a printf in there.... sometimes its 0 sometimes its 1 <--those values are 0=FALSE and 1= TRUE right?

But the inner paren's confuse me b/c it does not work like that for all cases....

For example: cnt=(-1 < ( 5 < 0)); This outputs 1 for count however, 5 is NOT less the zero so if thats false, why would it output 1 for 'true'?

6. Generally in C, 0 = false, non-zero = true.

So your first would evaluate to cnt = (-1< 'some random non-zero value'). Could be true could be false depending on what that value is. That's why you are getting different results when running the program multiple times.

EDIT: Sorry I was reading the condition backwards. it's actually cnt = (-1 < 0) which evaluates to true. So, the answer is E.

7. SECOND EDIT: Nevermind I'm an idiot. It always evaluates to 1. I don't know what's wrong with me today. DOH

8. What do you mean it always evaluates to 1?

cnt = (-1 < (2<1);

Your telling me that no matter where the inner parents are, it will always evaluate to true no matter what? I tried some different cases, plugging in different numbers, and moving around the parens, but the answers really seem unpredictable:/

Is there any way to know for sure?

9. Well 2<1 evaluates to 0 ALWAYS. Then cnt becomes (-1 < 0) which is true and I am not entirely sure but I think the reason is that implicit true conditions evaluate to 1. Someone else should probably give some more insight on this.

I have tried it on two machines and I always get 1, even with different values, as long as they respect the order provided in the example.

If you change the parens OF COURSE you get another value because you change the conditions.

10. THANKS!
I finally got it!

I created something similar looking to ((5>4)<3<(1>(0>3))) <-- and I simply followed the logic that you gave and I haven't failed yet....

Thanks again!

11. Originally Posted by claudiu
Well 2<1 evaluates to 0 ALWAYS. Then cnt becomes (-1 < 0) which is true and I am not entirely sure but I think the reason is that implicit true conditions evaluate to 1. Someone else should probably give some more insight on this.

I have tried it on two machines and I always get 1, even with different values, as long as they respect the order provided in the example.

If you change the parens OF COURSE you get another value because you change the conditions.
So when deciding is something is true or false, C will treat any non-zero value as true and zero as false. However, when it is generating a value using a logical operator (like < or <=), the generated values are always either 0 or 1. [Note that functions are not required to obey this principle -- strcmp doesn't have to give 1/0/-1; and is_____ from <ctype.h> may give any nonzero value for true.]

12. Originally Posted by tabstop
So when deciding is something is true or false, C will treat any non-zero value as true and zero as false. However, when it is generating a value using a logical operator (like < or <=), the generated values are always either 0 or 1. [Note that functions are not required to obey this principle -- strcmp doesn't have to give 1/0/-1; and is_____ from <ctype.h> may give any nonzero value for true.]
Ok, so I did not dream about this after all. Thanks for clearing it up tabstop.

Page 2 of 2 First 12
Popular pages Recent additions