Author Topic: Basics of QB64 - Looping around  (Read 368 times)

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2381
  • LET IT = BE
    • QB64.org
Basics of QB64 - Looping around
« on: September 06, 2020, 10:17:50 AM »
Computers are great at doing repetitive tasks for us. Here's how you can loop in BASIC.


Offline bplus

  • Forum Resident
  • Posts: 5219
  • B+ Knot again!
Re: Basics of QB64 - Looping around
« Reply #1 on: September 06, 2020, 10:44:30 AM »
Oh wow, didn't know QB64 has _CONTINUE and can use it in any loop structure.

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2381
  • LET IT = BE
    • QB64.org
Re: Basics of QB64 - Looping around
« Reply #2 on: September 06, 2020, 10:55:05 AM »
Glad there was something new for you, bplus!

_CONTINUE was added back in 2017 actually. It made its debut in v1.2.
« Last Edit: September 06, 2020, 10:57:46 AM by FellippeHeitor »

Offline Dav

  • Seasoned Forum Regular
  • Posts: 295
Re: Basics of QB64 - Looping around
« Reply #3 on: September 06, 2020, 10:59:13 AM »
I had forgotten about _CONTINUE!

Good job, Fellippe as always. You know, for a non-native English speaker your English is very good and clearly understandable.

Thanks again for keeping the tutorials coming.

- Dav

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2381
  • LET IT = BE
    • QB64.org
Re: Basics of QB64 - Looping around
« Reply #4 on: September 06, 2020, 10:59:52 AM »
Quote
Good job, Fellippe as always. You know, for a non-native English speaker your English is very good and clearly understandable.

Thanks, Dav!

Offline wiggins

  • Newbie
  • Posts: 15
Re: Basics of QB64 - Looping around
« Reply #5 on: September 07, 2020, 07:44:51 PM »
I have not used the new commands, so this is really helpful to me.

Very good video. 

Thanks!!!

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2381
  • LET IT = BE
    • QB64.org
Re: Basics of QB64 - Looping around
« Reply #6 on: September 07, 2020, 07:51:11 PM »
I have not used the new commands, so this is really helpful to me.

Very good video. 

Thanks!!!

Glad you enjoyed it, wiggins.

Offline JohnUKresults

  • Newbie
  • Posts: 22
Re: Basics of QB64 - Looping around
« Reply #7 on: September 08, 2020, 07:20:04 AM »
Thanks Fellippe. Good to know about _Continue! Could prove useful.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 356
Re: Basics of QB64 - Looping around
« Reply #8 on: September 12, 2020, 08:10:39 PM »
_CONTINUE is my new toy, I think I have some work for it to do, Thank you.
Andy

Offline TempodiBasic

  • Forum Resident
  • Posts: 1510
Re: Basics of QB64 - Looping around
« Reply #9 on: September 14, 2020, 04:36:58 PM »
Well done Fellippe!
I think that these your efforts to introduce to QB64 programming can be very useful for beginners to programming and beginners to QB64.
It maybe is good thing to group them into a separate section of the QB64 website.
Waiting feedbacks from veterans of the QB64.
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1510
Re: Basics of QB64 - Looping around
« Reply #10 on: September 14, 2020, 04:51:45 PM »
_CONTINUE is a good shortcut to modify your code!

all that you can get with _continue you can get with a IF THEN END IF!
Surely in a large code it is simpler to use
Code: QB64: [Select]
  1. IF condition THEN _CONTINUE
  2. block of code
than
Code: QB64: [Select]
  1.  IF NOT  condition THEN
  2. block of code
and these previous two way are more flexible than
Code: QB64: [Select]
  1. IF condition THEN label [GOTO label]
Programming isn't difficult, only it's  consuming time and coffee

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 356
Re: Basics of QB64 - Looping around
« Reply #11 on: September 14, 2020, 08:01:02 PM »
_CONTINUE is a good shortcut to modify your code!

I just went through and replaced all my:

<loop structure>
    IF condition THEN
        block of code
    END IF
<end loop structure>

with:

<loop structure>
    IF <NOT> condition THEN _CONTINUE
    block of code
<end loop structure>

It shaved off over a couple dozen END IFs in my ap without changing the action. It just requires an opposite condition test.

Doesn't really reduce typing, but cleans up some indentation nesting, which I did find desirable. Leaves a bit more space for comments.
« Last Edit: September 14, 2020, 08:04:38 PM by OldMoses »
Andy

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 2752
    • Steve’s QB64 Archive Forum
Re: Basics of QB64 - Looping around
« Reply #12 on: September 14, 2020, 09:08:07 PM »
I just went through and replaced all my:

<loop structure>
    IF condition THEN
        block of code
    END IF
<end loop structure>

with:

<loop structure>
    IF <NOT> condition THEN _CONTINUE
    block of code
<end loop structure>

It shaved off over a couple dozen END IFs in my ap without changing the action. It just requires an opposite condition test.

Doesn't really reduce typing, but cleans up some indentation nesting, which I did find desirable. Leaves a bit more space for comments.

Only thing to remember:

IF NOT (condition) THEN _CONTINUE....  may not be the same as IF (condition) THEN...(code stuff)

For example:

IF x AND y THEN
    do stuff
END IF

Now, if x = 4, and y = 2, then x AND y = 2, so you DO STUFF.

Now let’s try the other.

IF NOT (x AND y) THEN _CONTINUE
do stuff...

In this case, (x AND y) end up evaluating to 2, same as above, so NOT 2 gives us -3.  -3 is still regarded as a TRUE evaluation, so you now _CONTINUE and skip doing your stuff.

The *ONLY* time you can safely interchange the two methods, is when you’re dealing with binary results.  (0 and -1 only)

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1510
Re: Basics of QB64 - Looping around
« Reply #13 on: September 15, 2020, 09:47:26 AM »
Hi boys
I think that Steve points out a very important issue:
 NOT is not the reverse of AND so when condition is a compound evaluation of conditions we must use the CONST TRUE = -1 and FALSE = 0 (or if you prefer NOT TRUE), otherwise we must specify manually that (NOT compund condition) = 0 as inverse of compound conditions


following the example of Steve
Quote
IF x AND y THEN
    do stuff
END IF

Now, if x = 4, and y = 2, then x AND y = 2, so you DO STUFF.

Now let’s try the other.

IF NOT (x AND y) THEN _CONTINUE
do stuff...
the good conversion ( because the condition is compound) is IF (NOT(x AND y) ) = 0 THEN  or in alternative way
IF (NOT (x AND y)) = False THEN

because as we know also from the wiki is that IF executes the codeBlock_IF when the conditionEvaluated is not 0
Quote
The conditionStatement evaluation by IF must be true (-1) or a non-zero numerical value for the THEN {code} to be executed.
http://qb64.org/wiki/IF...THEN
Programming isn't difficult, only it's  consuming time and coffee

Online Kiara87

  • Newbie
  • Posts: 91
Re: Basics of QB64 - Looping around
« Reply #14 on: September 16, 2020, 03:36:00 PM »
Computers are great at doing repetitive tasks for us. Here's how you can loop in BASIC.



thank you  FellippeHeitor

the great thing is that I can put the subtitles in Italian so I can understand what you say too

I saw in your videos you are very experienced a very good programmer and thanks for sharing your experience
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione