Jump to content
  • Advertisement
Sign in to follow this  
LAURENT*

Skipping if and while statements despite being true

This topic is 621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So i'm getting ready to wrap up a few features and start my web page design and something bad happens. My if and while statement don't work. All they needed to do was check if a condition was true and they keep skipping it. I tried "If (true), while(true), for(true)" and nothing works. I know that bool variable that they check is true. I put a break point right before the if/while statements are activated and I  know when the bool value is turned to true. I watched this variables and it said it was true. Why is this happening? Has anyone ever encountered a problem like this and how do you fix it?  

Edited by LAURENT*

Share this post


Link to post
Share on other sites
Advertisement

Yes I've encountered this several times. In all cases it was caused by me messing something up.

 

Please post the code, otherwise we have no way of helping out.

Share this post


Link to post
Share on other sites

All they needed to do was check if a condition was true and they keep skipping it. I tried "If (true), while(true), for(true)" and nothing works.

You missed the obvious test of removing the test all together, although I don't think it makes any difference.
 
As it all makes no sense from a logic perspective, and we know a computer is a very logic-driven device, there is only one conclusion you can draw: Your hypothesis is wrong.
 
In other words, the "if" does get taken, it does whatever is in the "then" part, but for some reason, that has no or not enough effect.
You can try to run through it with a debugger to find the spot where it reverts your 'then' code, close inspection after a few hours doing something else also often works.
 
As VildNinja said already, if you want our help, we need some code lines to look at.

Share this post


Link to post
Share on other sites
Drop a breakpoint in your debugger, step through the code one line at a time and verify every value is what you expect.

It is extremely rare for the underlying systems to get this wrong, and it does occasionally happen on newly-released platforms and systems, but sometimes (extremely rarely) the tools will be wrong. After you have verified in your debugger that you haven't messed up the logic -- and you almost certainly have -- then step through the assembly and double check again.

Share this post


Link to post
Share on other sites

 

Also, try doing a full rebuild. Helps in a depressingly large number of cases of completely messed up behavior.

 

http://www.gamedev.net/topic/682628-visual-studios-2010-debugger-variable-doesnt-update/#entry5312556

 

I tried that, it still didn't work. From what I observed I didn't initialize a bool variable HOWEVER it always true by default for some reason. Even in the very early stages of the program's execution as soon as  this bool value is declared it is true and I didn't even set it. I'm like 'awesome bools are automatically true upon being declared". Tracking the bool variable and it behavior shows it remains consistent because I changed it to see if the variable wasn't updating for debug. I need time to prepare my code example. It ain't easy getting spot on info that help you to help me. 

Share this post


Link to post
Share on other sites

From what I observed I didn't initialize a bool variable HOWEVER it always true by default for some reason.

 

bool has three states: false, true, and undefined. I believe VC++ compiler treats 0 as false, 1 as true, and anything else is undefined, however Visual Studio might treat undefined as true (because it's not 0) thus causing this confusion. I'd suggest to set it explicitly to true right before the loop - it should enter your loop.

Share this post


Link to post
Share on other sites

Okay..... Don't get ma

 

 

From what I observed I didn't initialize a bool variable HOWEVER it always true by default for some reason.

 

bool has three states: false, true, and undefined. I believe VC++ compiler treats 0 as false, 1 as true, and anything else is undefined, however Visual Studio might treat undefined as true (because it's not 0) thus causing this confusion. I'd suggest to set it explicitly to true right before the loop - it should enter your loop.

 

I just set it explicitly and well..... it worked. Why does this debugger keep lying to me? Why! I've read around the web a bit and came to the conclusion that I should get my program to work first then clean up everything later including initializing the uninitialized. I didn't follow it at first but later I ended up creating variables for ideas that flopped and were worthless. So I just try to get a feature to work and clean up the mess later right after it works. (sigh) my conclusions are always wrong. shit.

Share this post


Link to post
Share on other sites
Always initialize your variables. Always.

What would the following code print? (This is compiled with VC++ 2015 in Debug mode with default project settings)
 
  bool f;

  // watch window says:
  // f	true (204)	bool
  // see that "204"?
  // this boolean is stored as a byte in memory, so it has 256 possible values, only two of which are true and false.
  // That's 1/128th chance that the bool will even act as a boolean, regardless of not knowing its value!

  if (f == false)
  {
    printf("first case\n"); // not printed
  }
  if (f == true)
  {
    printf("second case\n"); // not printed either.  But clearly a bool is either true or false, right?  WRONG.
  }
  if (f) // this is the same as (f != 0)
  {
    printf("third case\n");
  }
Edited by Nypyren

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!