If statements executing regardless of false conditions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PatrickF
    New Member
    • Dec 2009
    • 2

    If statements executing regardless of false conditions

    Hi, I am writing a simulator for a game. I have been stuck at a couple if statements executing regardless of false conditions every time the flow of execution reaches it. Here's the format with psuedo-variables:

    Code:
    if(timeA - timeB >= 3 + count * 3)
    {
         x += y;
         count++;
    }
    Then, I have another one with almost the exact variables. I am writing this in BlueJ and used the debugger to make sure that indeed, timeA - timeB < 3 + count * 3 when x += y and count++ executes. Frustrated, I even put

    Code:
    System.out.println((timeA - timeB) + " >=" + (3 + count * 3));
    right before x += y and sure enough, I would get things printed like 5.43 >= 20 so I know for sure it's not an issue of not knowing my variables.

    Is it possible that BlueJ or it's terminal window/debugger is simply bugged or is there something special about having if statements right next to each other? Or is it something very simple that I overlooked.
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    Is it possible that BlueJ or it's terminal window/debugger is simply bugged
    Yes. But that is the very last thing to consider.

    The instinctive cry of "it's a bug" is wrong on a couple of counts: that a bug would exist within a wildly used tool in such a commonplace circumstance is hugely unlikely. It involves not only the software's authors screwing up but then a whole series of users not seeing that, or not realising or not saying anything.

    The second reason is methodological: problem solving involves ... solving problems. But a bug in the compiler is not something that you can really solve. To repeat, it is possible, but if your aim is problem solving, then you should look for other things that the problem can be: things that are solvable.

    (Sorry to harp on about that)

    Your use of System.out.prin tln() was the best thing to do. (Or use a debugger). If it gives output that you can't understand then post the code here so that others can see the problem for themselves. It's important that such code should be compilable and runnable by others (and hence shouldn't have a whole lot of dependencies on other things or random statements that have nothing to do with the if statements.)

    Quite likely it will be a code example that you have to construct specifically to illustrate the problem. (See SSCCE.org) This involves a bit of work, but it often leads you to spotting the cause of the problem.

    (My guess - for whatever it's worth - is that the culprit is a stray semicolon.)

    Comment

    • PatrickF
      New Member
      • Dec 2009
      • 2

      #3
      Thank you for your response. I was deserving of the criticism as I did jump to conclusions.
      But mostly, what was probably the most insignificant thing in your post, put in parenthesis, saves my day. It's a wonder how two darned stray semicolons got in my code, and even greater a wonder how I was oblivious to it after a few hours of staring at my code. I can't thank you enough for your help to this amateur programmer.

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        Originally posted by PatrickF
        Thank you for your response.
        You're welcome.

        (umm... for some reason the forum software doesn't like replies that are too short...)

        Comment

        Working...