Author Topic: Minor bug  (Read 3980 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Minor bug
« 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.
It works better if you plug it in.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Minor bug
« Reply #1 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.  

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Minor bug
« Reply #2 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.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Minor bug
« Reply #3 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?


QB64 is the best!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Minor bug
« Reply #4 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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Minor bug
« Reply #5 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
« Last Edit: September 11, 2019, 11:47:21 am by bplus »

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Minor bug
« Reply #6 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
QB64 is the best!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Minor bug
« Reply #7 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).


« Last Edit: September 12, 2019, 11:01:27 am by bplus »

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Minor bug
« Reply #8 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?)
« Last Edit: September 12, 2019, 05:50:37 pm by Jack002 »
QB64 is the best!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Minor bug
« Reply #9 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. :)

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Minor bug
« Reply #10 on: September 12, 2019, 11:46:55 pm »
I tried it on my c64 emulator, hey Mikey! He likes it! OMG
  [ You are not allowed to view this attachment ]  
QB64 is the best!