LoginLogin

If Statements Not Printing Text as Expected

Root / Programming Questions / [.]

Alan_RaterinkCreated:
Hello! I am currently building a preferences screen for one of my new games, but the if statement controlling the display of the volume level aren't executing commands properly. Here's my code:
if volprcnt==0 then
 locate 40,6
 print "mute"
elseif volprcnt>=.1 && volprcnt<=.9 then
 locate 40,6
 print volprcnt*100
elseif volprcnt==1 then
 locate 40,6
 print "max"
endif
return
"volprcnt" is a number between 0 and 1 that I am multiplying by 127 to get the value of my "vol" variable on my beep commands. If volume is at zero, it should print mute, and if it's max, it should print max, but it only prints the numbers between 10 and 90 (I am multiplying to remove the decimal from what the user sees). I don't see either word. Why is this? Thank you, Alan

I tried out your code and it worked fine. The problem is somewhere else.

Hello! I am currently building a preferences screen for one of my new games, but the if statement controlling the display of the volume level aren't executing commands properly. Here's my code:
if volprcnt==0 then
 locate 40,6
 print "mute"
elseif volprcnt>=.1 && volprcnt<=.9 then
 locate 40,6
 print volprcnt*100
elseif volprcnt==1 then
 locate 40,6
 print "max"
endif
return
"volprcnt" is a number between 0 and 1 that I am multiplying by 127 to get the value of my "vol" variable on my beep commands. If volume is at zero, it should print mute, and if it's max, it should print max, but it only prints the numbers between 10 and 90 (I am multiplying to remove the decimal from what the user sees). I don't see either word. Why is this? Thank you, Alan
It only prints between 10 and 90 because you check through .1 to .9. You check, through your if statements... 0 .1 to .9 1 You don't ever check 0 to .1, or .9 to 1, so you miss some values.

Hello! I am currently building a preferences screen for one of my new games, but the if statement controlling the display of the volume level aren't executing commands properly. Here's my code:
if volprcnt==0 then
 locate 40,6
 print "mute"
elseif volprcnt>=.1 && volprcnt<=.9 then
 locate 40,6
 print volprcnt*100
elseif volprcnt==1 then
 locate 40,6
 print "max"
endif
return
"volprcnt" is a number between 0 and 1 that I am multiplying by 127 to get the value of my "vol" variable on my beep commands. If volume is at zero, it should print mute, and if it's max, it should print max, but it only prints the numbers between 10 and 90 (I am multiplying to remove the decimal from what the user sees). I don't see either word. Why is this? Thank you, Alan
It only prints between 10 and 90 because you check through .1 to .9. You check, through your if statements... 0 .1 to .9 1 You don't ever check 0 to .1, or .9 to 1, so you miss some values.
True. I was thinking about going in 0.1 increments. It's not the problem though. When plugging in a number like "0.01" doesn't trip anything. I think the problem is elsewhere.

You should be storing your volume as 0 - 127, not as a fraction. When printing, you should convert the value to a percent and then output. Example:
'let VOLUME be the volume in range 0 - 127
IF VOLUME==0 THEN
 LOCATE 40,6
 PRINT "MUTE"
ELSEIF VOLUME==127 THEN
 LOCATE 40,6
 PRINT 'MAX"
ELSE
 PERCENT=ROUND(VOLUME/127*100)
 LOCATE 40,6
 PRINT PERCENT
ENDIF
There might be an issue with rounding errors and precision loss, based on how computers store decimal numbers, so this method should make your volume more accurate.

First of all, thanks for all the help! Secondly, sorry for not making one thing clear earlier. Basically, I am taking my VOL variable and setting it equal to 127 (the maximum volume possible) * VOLPRCNT (which ranges from 0 to 1 by .1 intervals. For example, I initially set it equal to .6 before the user makes any changes, which will increment or decrement by .1), so...
VOL=127*VOLPRCNT
'returns some number between 0 and 127, based on the scale factor (VOLPRCNT)
VOL will only be used by BEEP commands. That's it. Then later in the program, I go to print what the user will see, which is MUTE to 10-90 to MAX. Since VOLPRCNT is somewhere between 0 and 1, I multiply it by 100 to get 10-90. I can't understand why the IF statement doesn't print either word. I just can't for the life of me. I used a == instead of a <.1 because VOLPRCNT is predictable, and I know for a fact that it will at some point equal 0 or 1. It will always equal 0, .1, .2, .3, .4, .5, .6, .7, .8, .9 and 1. So why does this refuse to work? When the user wants to change the volume, I call some user defined functions declared earlier in the program that look as follows...
DEF VOLUP
 IF VOLPRCNT+.1<=1 THEN
  VOLPRCNT=VOLPRCNT+.1
  VOL=127*VOLPRCNT
  BEEP 009, 0, VOL
 ENDIF
END

DEF VOLDOWN
 IF VOLPRCNT-.1>=0 THEN
  VOLPRCNT=VOLPRCNT-.1
  VOL=127*VOLPRCNT
  BEEP 009, 0, VOL
 ENDIF
END
This variable is not changed in any other place in the code. My goal was to use the DEFs only, so I had concise code and so I always knew its location. Does any of this help with determining the problem? Even ahavasandwich said it was good, so I'm completely lost...if there is any other information you'd like, just ask! Thanks again and I hope this structure made a little more sense!

I'm not sure if this is the problem, but there looks to be an issue with the conditional statements in your def blocks. Because you have it set where if VOLPRCNT+.1<=1, then it WILL add 0.1 to 1. Just take out the "=" in both conditional statements and it should work fine. I hope this helps.

I'm not sure if this is the problem, but there looks to be an issue with the conditional statements in your def blocks. Because you have it set where if VOLPRCNT+.1<=1, then it WILL add 0.1 to 1. Just take out the "=" in both conditional statements and it should work fine. I hope this helps.
I actually, instead of using decimals, I made VOLPRCNT equal to 0 through 100, then divided it by 100 when I multiply it by 127 and assign it to VOL. The IF statement I originally had was correct all along, and both DEFs are fully functional. I guess there's some problems calculating decimals...everything is working, but it was that darned decimal. Just though everyone would like to know for future reference and thanks for your expertise! -Alan

Yeah, like Slacker said, never use decimals in an IF statement because SB stores decimals in binary format which turn them into floating points and thus prone to rounding errors. 0.1 is actually 0.0001100110011… in binary, but since SB can only store it (Zero, decimal point, one) in 64-bit, it becomes 0011111110111001100110011001100110011001100110011001100110011010 and ignores further repeating digits. So if you increment VOLPRCNT by 0.1 you are bound to run into problems. No need to learn Japanese, but just know that Ochame is showing why this stopwatch program doesn't work. https://miiverse.nintendo.net/posts/AYIHAAAEAAASVZKdAWi67g

Yeah, like Slacker said, never use decimals in an IF statement because SB stores decimals in binary format which turn them into floating points and thus prone to rounding errors. 0.1 is actually 0.0001100110011… in binary, but since SB can only store it (Zero, decimal point, one) in 64-bit, it becomes 0011111110111001100110011001100110011001100110011001100110011010 and ignores further repeating digits. So if you increment VOLPRCNT by 0.1 you are bound to run into problems. No need to learn Japanese, but just know that Ochame is showing why this stopwatch program doesn't work. https://miiverse.nintendo.net/posts/AYIHAAAEAAASVZKdAWi67g
The use of the 0.1 in the IF isn't necessarily the real problem, as a decent compiler should turn 0.1 into its closest proper floating-point representative at compile time (I would hope SB does this, anyway.) I was moreso referring to the dubieties of the math used in floating point that may have thrown off the answer. Decent advice to try though if something isn't working out.

VOLPRCNT is predictable
Not so much. If you wish to represent just 11 values, equally spaced, use an integer value 0 to 10 inclusive.