Thread: Very simple, possibly trick question.

  1. #16
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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. #17
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    LOL ill be sure to ASK about any poorly written questions on the FINAL exam!

  3. #18
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    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. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That sounds correct to me, some arbitrary while loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    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. #21
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    Last edited by claudiu; 04-23-2010 at 03:27 PM.

  7. #22
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    SECOND EDIT: Nevermind I'm an idiot. It always evaluates to 1. I don't know what's wrong with me today. DOH

  8. #23
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    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. #24
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    Last edited by claudiu; 04-23-2010 at 04:04 PM.

  10. #25
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    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. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by claudiu View Post
    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. #27
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094

    Thumbs up

    Quote Originally Posted by tabstop View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM