Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help me please ( students digree programe )
#6
Look at the following code:
Code:
dim x as integer
input "Input a number: ", x
if 3 < x < 10 then
  print "x is between 3 and 10"
end if
It is somewhat similar to what you are trying to achieve and it looks like it will print the message "x is between 3 and 10" if it indeed is.
But it won't behave as expected.
IIRC Qbasic "if" statements work by examining each "question" at it's own
and if it's evaluates to true replace it with -1 but if it's false replace it with 0.
So say, if you input the number 7, it is between 3 and 10, but if won't work that way, instead it will do this:
examine first part:

if 3 < x < 10 then

3 is less than 7 so qbasic puts a -1 at it's place. Then we have

if -1 < 10 then

and -1 is of course less than true so it works here.

But what if we input a number that's smaller than 3 into x? Say x=1...

if 3 < x < 10 then

3 < 0 evaluates to false, so qbasic put's a 0 in it's place

if 0 < 10 then

0 < 10 evaluates to true so the message is printed even though x clearly isn't between 3 and 10

Finally we can try with a number that's bigger than 10, say 33:

if 3 < x < 10 then

3 < 10 evaluates to false, so qbasic put's a -1 in it's place

if -1 < 10 then

-1 < 10 evaluates to true so the message is printed even though x clearly isn't between 3 and 10

If you want it to work you have to write
Code:
dim x as integer
input "Input a number: ", x
if 3 < x and x < 10 then
  print "x is between 3 and 10"
end if

which is equal to
Code:
dim x as integer
input "Input a number: ", x
if (3 < x) and (x < 10) then
  print "x is between 3 and 10"
end if

Now it might be a little clearer just read it out : If 3 is smaller than x and x is smaller than 10...
/post]
Reply


Messages In This Thread
help me please ( students digree programe ) - by red_Marvin - 12-17-2006, 12:53 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)