Author Topic: the Alien WHILE of QB64... a new feature written as old keyword  (Read 6056 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: the Alien WHILE of QB64... a new feature written as old keyword
« Reply #15 on: September 29, 2019, 03:34:26 pm »
And you still haven’t preserved your X/Y values like a pure EXIT would.  In your code, you can tell me which screen you found the color on, but what’s the X/Y position where it’s located on that screen?  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: the Alien WHILE of QB64... a new feature written as old keyword
« Reply #16 on: September 29, 2019, 05:25:32 pm »
Yes Steve
My last code looses informations...
So to correct this my bug I must add 2 lines of code in the IF at the last and a tricky to avoid to use more variables

Code: QB64: [Select]
  1.   DO
  2.     _SOURCE MyImages(Z)
  3.     FOR X = 1 TO Wide
  4.         FOR Y = 1 TO High
  5.             IF POINT(X, Y) = ColorChosen THEN
  6.                                Done = true
  7.                                Y =  Y + High
  8.                                X = X + Wide
  9.             END IF
  10.         NEXT
  11.     NEXT
  12.     IF NOT Done THEN Z = Z + 1
  13. LOOP UNTIL (Z > UBOUND(MyImages)) OR Done
  14.  
  15. IF Z <= UBOUND(MyImages) THEN
  16.     'We found our match.  Z = the image, X/Y = the point where the color was found.
  17. Y = Y -High
  18. X=X -Wide
  19.     'That color wasn't found.
  20.     [ /code]
  21. but it's only a simple example that let to find simple solutions
Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: the Alien WHILE of QB64... a new feature written as old keyword
« Reply #17 on: September 29, 2019, 09:07:21 pm »
And you just corrupted your program’s data! 

At the start of the program, (which we haven’t shared yet on the forums), was:

DIM X AS _UNSIGNED _BYTE, Y AS _UNSIGNED _BYTE, Wide AS _UNSIGNED _BYTE, High AS _UNSIGNED _BYTE
Wide = 250: High = 250

You just overflowed X/Y!!



LOL!  Don’t pay me any real attention; I’m just messing with you.  (Though you may actually have these sort of issues if the code becomes part of a library which others might $INCLUDE into their code.)

Really, I was just illustrating why an EXIT statement might not be a “bad coding practice”, and might instead be the most elegant solution to a problem.  I think we’ve showed that here, and I thank you for playing along and helping me highlight my point so nicely.

There’s nothing in the world wrong with using an EXIT (or even judiciously using a GOTO in limited situations).  You just don’t want to rely on them so much so that your code spaghettifies and becomes unreadable and impossible to follow the flow and intent of it.  ;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: the Alien WHILE of QB64... a new feature written as old keyword
« Reply #18 on: September 30, 2019, 04:30:47 am »
Thanks guys.  As both Steve and Fellippe do not think that EXIT is bad practice, on balance we'll go with them (sorry, Cobalt).  When I did use EXIT, it seemed to be an elegant method.  As with all things, just use it with skill (OK, so some of us aren't so hot on the skill level).  I would never use GOTO in any circumstance (no need for Steve to concoct an illustrative piece of code where it would be required!).


Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: the Alien WHILE of QB64... a new feature written as old keyword
« Reply #19 on: September 30, 2019, 07:18:13 am »
Great Steve
you have checkmated me!
:-)

At last position it is impossible to save Z, X and Y without EXIT or its workaround by using GOTO Label!

Thanks
Programming isn't difficult, only it's  consuming time and coffee