July 23, 2024, 08:28:28 AM

News:

IWBasic runs in Windows 11!


Basic Clarity 4 - For Loops

Started by GWS, June 13, 2012, 10:50:26 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GWS

June 13, 2012, 10:50:26 AM Last Edit: June 13, 2012, 10:57:22 AM by GWS
Last example for today, but I thought I'd offer it, 'cos it's a very neat way in Basic of doing a lot of work with only a few statements ..  ;D


' Basic Clarity 4 ..
' For Loops .

autodefine "off"

def i,n,a[30] as int

n = 25

for i = 1 to n step 2
a[i] = i
print a[i],
next i

do:until inkey$<>""
closeconsole
end


The For .. Next Loop is so simple, and yet it has many applications.

The first line of the loop does several things in one go - it defines the loop 'control variable' in this case 'i'.

It then specifies the start value (in this example 1). (but it can be any value to suit the application).

The loop will run from the start value to the finishing value - in this example the value stored in variable 'n' (ie. 25).

The fact that the 'start' and 'finish' values can be variables, makes the For loop very flexible.

You control the loop increment with the 'step' value - in this case 2 - which will result in the increment stepping through only odd values.

(The loop increment can also be negative to enable you to step backwards).

The statements in the 'body' of the loop are executed at each pass, until the 'Next i' statement is reached The value of the control variable 'i' is then incremented by the 'step' amount, and the loop returns to the For statement.

The loop then tests whether the finishing value has been reached, and if so, the loop ends.

Sometimes when an application is looping many times, you may reach a condition where it is pointless to carry on looping, and you wish to exit the For loop.

Creative does not have an inbuilt 'BreakFor' statement (which IWB does - it's a bigger system  ::))
But it's no problem, 'cos if you want to break out of the loop, just set the control variable value to the finishing value - as in :


for i = 1 to n step 2
a[i] = i
print a[i],
if i = 13 then i = n :' (break the loop when i = 13)
next i


In Basic, there are no { } brackets or 'begin','end' statements to specify the blocks of code to be processed in the loop, and no end of line ';' markers.

Just clean statements of what you wish to do.  :P

You can't beat Basic's simplicity of the programmer's tools of the trade.

[Feel free to comment about any of these examples ..]

best wishes, :)

Graham

Tomorrow may be too late ..

aurelCB

June 13, 2012, 02:07:03 PM #1 Last Edit: June 13, 2012, 02:23:04 PM by aurelCB
Ha breakfor...
of course:

def a[100] as INT
n=100
for i = 1 to n
a[i] = i
print a[i],
if i = 13 then goto break<i> :' (break the loop when i = 13)
next i
label break<i>
do:until inkey$<>""

LarryMc

Quote from: aurelCB on June 13, 2012, 02:07:03 PM
Ha breakfor...
of course:

def a[100] as INT
n=100
for i = 1 to n
a[i] = i
print a[i],
if i = 13 then goto break<i> :' (break the loop when i = 13)
next i
label break<i>
do:until inkey$<>""

From a purist standpoint I'm not sure your way of jumping out of the middle of the loop clears the stack properly.
In small programs it probably doesn't matter.
I think Grahams way is the cleanest way to do it.
That's just my personal opinion.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

GWS

Hi guys,

I'm a great believer in 'if it works - go for it' ..  ;D

However, I've been told by an expert, that my predilection for placing GDI window's buttons on a DirectX screen can not be relied upon to work on all user's machines.  It's always worked for me in Creative, and no-one has ever said it didn't work for them - so I'm not sure  ::)
I do know it doesn't work at all in some languages ..  :(

In the BreakFor case, Aurel's suggestion for leaping out of the loop will probably work.  What state it leaves the innards in I've no idea - maybe it's a case of 'who cares' once you're clear of the loop.

Having grown up with Fortran over many years, we were always told when learning programming, not to jump into or out of loops, so we just avoided doing it without really knowing why ..  :)

Jumping in would clearly not be good, since the loop parameters would not have been set.  Jumping out is possibly not such a problem - I don't know enough about what goes on under the hood.

all the best, :)

Graham
Tomorrow may be too late ..

aurelCB

I can look into source of FOR loop but of course you can always put this type of loop into
subroutine like this:

GOSUB myloop
GOSUB out
'....
'....
SUB myloop
def a[100] as INT
n=100
for i = 1 to n
a[i] = i
print a[i],
if i = 13 then goto break<i> :' (break the loop when i = 13)
next i
label break<i>
RETURN
'....
'....
LABEL out
do:until inkey$<>""