Computer Science Canada loop |
Author: | jayshaw94 [ Thu Apr 21, 2011 12:03 pm ] |
Post subject: | loop |
When running this code below. If the person types in an answer that is within the limit (0,0.5,1,2) I still get the error message. What am I doing wrong? Thanks in advance! var nojp:int var op1:real procedure OpeningDeductions put "Enter the number of Jumbo post(s) required" get nojp put "Enter either 0, 0.5, 1, or 2" loop get op1 if op1 not = 0 or op1 not = 0.5 or op1 not = 1 or op1 not = 2 then put "Error please enter the following 0, 0.5, 1, or 2" put op1 end if exit when op1 = 0 or op1 = 0.5 or op1 = 1 or op1 =2 end loop end OpeningDeductions |
Author: | Tony [ Thu Apr 21, 2011 12:19 pm ] |
Post subject: | RE:loop |
Because testing for equality of real values is a bad idea http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_computers Quote: To a rough approximation, the bit representation of an IEEE binary floating-point number is proportional to its base 2 logarithm, with an average error of about 3% |
Author: | jayshaw94 [ Thu Apr 21, 2011 12:30 pm ] |
Post subject: | Re: loop |
sorry Tony not sure what you mean. The program runs but I still get the error message if for instance i type in 5 I'll get the error message as I should. But if i type in one of the options available I get the error message but it continues onto the next line of code |
Author: | Insectoid [ Thu Apr 21, 2011 12:46 pm ] |
Post subject: | RE:loop |
Say someone enters 1. Now we check. Does it equal 0? No. Does it equal 0.5? No. Does it equal 1? Yes! The test fails, and we therefore get your error message. 'or' is obviously not working here. Try something else. |
Author: | Tony [ Thu Apr 21, 2011 12:47 pm ] | ||
Post subject: | RE:loop | ||
oh, that.
is guaranteed to always be true, for any value entered. edit: damn you Insectoid! |
Author: | Insectoid [ Thu Apr 21, 2011 12:48 pm ] |
Post subject: | RE:loop |
I win again! |
Author: | jayshaw94 [ Thu Apr 21, 2011 1:48 pm ] |
Post subject: | Re: loop |
thanks guys what other syntax would i be able to use? |
Author: | Tony [ Thu Apr 21, 2011 2:00 pm ] |
Post subject: | RE:loop |
OR could work, if you ask the right question. Keep in mind that this is a mathematical OR. In speech "or" often means "xor" instead, thus the confusion. Some references or xor and not |
Author: | jayshaw94 [ Thu Apr 21, 2011 3:27 pm ] |
Post subject: | Re: loop |
Thank you Tony I'll look into it some more thanks again |