QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Richard Frost on September 07, 2019, 10:08:29 am

Title: Minor bug
Post by: Richard Frost on September 07, 2019, 10:08:29 am
The New Topic button doesn't show up for me under Bugs, so.....

The following line is flagged by the editor as an error:

FOR pass = 1 TO 1 - (LEFT$(m$, 1) = "O")       

With QB4.5,if the expression in brackets is true, it's -1 and the loop gets two passes.
I consider it very minor bug, and maybe I shouldn't be coding thataway anyway.
Title: Re: Minor bug
Post by: bplus on September 08, 2019, 11:07:48 am
Code: QB64: [Select]
  1. 'when does it become a bug?
  2. PRINT "Logic of 2 literals... "
  3. FOR pass = 1 TO 1 - (1 < 10)
  4.     PRINT "Pass "; pass
  5. PRINT: PRINT "Logic and a variable..."
  6. a = 1
  7. FOR pass = 1 TO 1 - (a < 10)
  8.     PRINT "Pass "; pass
  9. PRINT: PRINT "Logic and a variable and an operator..."
  10. a = 1
  11. FOR pass = 1 TO 1 - ((a MOD 2) < 10)
  12.     PRINT "Pass "; pass
  13. PRINT: PRINT "Logic and a variable and a String evaluation..."
  14. s$ = "FEDCBA9876543210": one$ = "1"
  15. FOR pass = 1 TO 1 + INSTR(s$, one$)
  16.     PRINT "Pass "; pass
  17. PRINT: PRINT "Logic and a variable a string eval then logic eval"
  18. FOR pass = 1 TO 1 - ((INSTR(s$, one$) < 10))
  19.     PRINT "Pass "; pass
  20.  
  21.  
  22. 'Oh I bet it is string compare!     Yep!
  23. FOR pass = 1 TO 1 - ("a" = "a")
  24.     PRINT "Pass "; pass
  25.  
  26.  
  27.  
Title: Re: Minor bug
Post by: RhoSigma on September 08, 2019, 01:04:45 pm
Then simply do:

condition = (LEFT$(m$, 1) = "O")
FOR pass = 1 TO 1 - condition
.
.
NEXT pass

that works for me.
Title: Re: Minor bug
Post by: Jack002 on September 11, 2019, 11:00:54 am
I don't understand the intended logic here.
the for statement will step by 1 if not set
So
FOR a = 1 to 0
  print "Test"
next a

or

FOR a = 1 to 1
  print "test"
next a

I had to test it.
The first one didn't run. Odd. I was sure it would. (Maybe it does work in some basics?)
You gave it descending values for A
The print inside the top FOR never fired.
People don't usually use FOR for that, but I suppose it works.
So IF was not an option for some reason? Or CASE?


Title: Re: Minor bug
Post by: Petr on September 11, 2019, 11:29:59 am
Quote
I don't understand the intended logic here.
the for statement will step by 1 if not set
So
FOR a = 1 to 0
  print "Test"
next a

Hi. It is because if is STEP not set, is 1. PLUS 1. 0 is lower than 1, therefore is this nonsense loop, which is not start. For starting it, must step be set as -1:

FOR a = 1 to 0 STEP -1
  print "Test"
next a
Title: Re: Minor bug
Post by: bplus on September 11, 2019, 11:45:47 am
I don't understand the intended logic here.
the for statement will step by 1 if not set
So
FOR a = 1 to 0
  print "Test"
next a

or

FOR a = 1 to 1
  print "test"
next a

I had to test it.
The first one didn't run. Odd. I was sure it would. (Maybe it does work in some basics?)
You gave it descending values for A
The print inside the top FOR never fired.
People don't usually use FOR for that, but I suppose it works.
So IF was not an option for some reason? Or CASE?

Remember a true condition returns -1 so:
If the condition were true it would go from 1 to 1-(-1) or 1 to 2, otherwise just 1 to 1-(0) or 1
Title: Re: Minor bug
Post by: Jack002 on September 11, 2019, 05:45:28 pm
I know true is -1 and false is 0
you negated it so if true it will reverse to 1 and if false still 0
I was taking that into account
Title: Re: Minor bug
Post by: bplus on September 12, 2019, 10:58:08 am
This "minor" problem does not occur just in FOR loops, it occurs when expect a true or false evaluation when comparing strings or string evaluations to literal strings outside IF statements.

Code: QB64: [Select]
  1. ' next line uncommented is Red lined.
  2. 'print "a"="a"
  3.  
  4. a$ = "a"
  5. PRINT a$ = a$ 'OK!
  6.  
  7. ' next line uncommented is Red lined.
  8. 'print a$ = "a"
  9.  
  10. PRINT (LEFT$(a$, 1) = a$) 'OK!
  11.  
  12. ' next line uncommented is Red lined.
  13. 'PRINT (LEFT$(a$, 1) = "a")
  14.  
  15. PRINT (LEFT$("a", 1) = a$) 'OK!
  16.  
  17. PRINT MID$(a$, 1) = a$
  18.  
  19. ' next line uncommented is Red lined.
  20. 'PRINT MID$(a$, 1) = "a"
  21.  
  22. IF MID$(a$, 1) = "a" THEN PRINT "PRINT MID$(a$, 1) = "; CHR$(34); "a"; CHR$(34); " 'does not work but this does? IF MID$(a$, 1) = "; CHR$(34); "a"; CHR$(34); " THEN"
  23.  

Of course there are work arounds but good to know when you will need one (and why).


Title: Re: Minor bug
Post by: Jack002 on September 12, 2019, 05:41:40 pm
I didn't get what you said at first. I see now
Ah, I see.
If I remove the ' it red lines.
Interesting. I don't make a habit of putting tests after print (and I don't put tests into parts of a FOR statement)
I see what you mean. Can we expect all these to work?
= in a print statement just looks odd to me. Print doesn't test things, it reports them. I don't expect anything with = in it to work.

[edit]
I read the entire help file on PRINT, it does not expect a = at all. You were lucky to get anything to work with it.
Also, I see A$ = A$ is ok and A$ = B$ is ok A$ = B is not, it said not the same type, and A= A is ok.
I consider any use of = in it a bug, it should fail. (a no fail when it should?)
Title: Re: Minor bug
Post by: bplus on September 12, 2019, 06:32:41 pm
I didn't get what you said at first. I see now
Ah, I see.
If I remove the ' it red lines.
Interesting. I don't make a habit of putting tests after print (and I don't put tests into parts of a FOR statement)
I see what you mean. Can we expect all these to work?
= in a print statement just looks odd to me. Print doesn't test things, it reports them. I don't expect anything with = in it to work.

[edit]
I read the entire help file on PRINT, it does not expect a = at all. You were lucky to get anything to work with it.
Also, I see A$ = A$ is ok and A$ = B$ is ok A$ = B is not, it said not the same type, and A= A is ok.
I consider any use of = in it a bug, it should fail. (a no fail when it should?)


Hi Jack002,

I think a classic way a function is tested is by PRINTing it without having to set to a variable name first:

PRINT MID$(Source$, x, y)

PRINT myNewFunctionThatReturnsAnInteger(x, y, z)

so sure, why not Boolean evaluations:
PRINT x = 10 '-1 or 0, yes or no

But yeah, I can see throwing errors when not of like Type that is like comparing apples to oranges. :)
Title: Re: Minor bug
Post by: Jack002 on September 12, 2019, 11:46:55 pm
I tried it on my c64 emulator, hey Mikey! He likes it! OMG
  [ This attachment cannot be displayed inline in 'Print Page' view ]