## Re: Variable out of loop

Expand Messages
• ... Like Stefan, I like the WHILE/WEND approach. a=1 while a
Message 1 of 21 , Jan 28, 2013
• 0 Attachment
--- In libertybasic@yahoogroups.com, "henriknilson" wrote:
>
> Hello
>
> In the following code "a" which is out of loop equal to "11", while the last value of it inside the loop is "10".
> Why is it always plus one when it is out of loop in all kind of basics, please?
>
> Thank you
>
> 'The code
> for a=1 to 10
> print a
> next
> print
> print a' I suppose this value should be 10 instead of 11
>

Like Stefan, I like the WHILE/WEND approach.
a=1
while a <= 10
print a
a=a+1
wend
end
• Verdict? I think NEXT is Not Guilty As Rod has pointed out, it s duty is to increment. The FOR...TO decides whether to execute the loop. We decide what
Message 2 of 21 , Jan 28, 2013
• 0 Attachment
Verdict?

I think NEXT is 'Not Guilty'

As Rod has pointed out, it's duty is to increment.
The FOR...TO decides whether to execute the loop.
We decide what should happen in between.

for x=1 to 10
do something
NEXT

Normally the value of x is not important after NEXT.
However, to be safe, we decide to EXIT the loop when x reahes upper limit.

For x=1 to 10
do something
if x=10 then EXIT for
next

Besides other alternatives already mentioned by contributors, the following is easy to understand:

x=0
do
x=x+1
print x
loop until x=10
print x

I still prefer good old FOR...NEXT. There is no need to initialize (x=0) or increment (x=x+1).
• But the last value of “a” in your code out of loop will be plus one, exactly like for next loop, just have a look at your code after adding additional line
Message 3 of 21 , Jan 28, 2013
• 0 Attachment
But the last value of “a” in your code out of loop will be
plus one, exactly like for next loop, just have a look at your code after
Stefan used for next loop with fixing last value of the
variable inside the loop.
Ralph used while/wend approach as you see below with wanted
result.

Thank you

a=1
while a <= 10
print a
a=a+1
wend
print
print a' additional line out of loop, "a" will be =11 instead of 10
end
'code ended

'Stefan’s code

for i = 1 to 10
a = i
print i
next
print
print a' the value of "a" out of loop will be= to 10
end
'code ended

'Ralph’s code

i=0
while i<10
i=i+1
print i
wend
print
print i ' the value of "i" out of loop will be= to 10
'code ended

________________________________
To: libertybasic@yahoogroups.com
Sent: Tuesday, January 29, 2013 1:57 AM
Subject: [libertybasic] Re: Variable out of loop

--- In libertybasic@yahoogroups.com, "henriknilson" wrote:
>
> Hello
>
> In the following code "a" which is out of loop equal to "11", while the last value of it inside the loop is "10".
> Why is it always plus one when it is out of loop in all kind of basics, please?
>
> Thank you
>
> 'The code
> for a=1 to 10
> print a
> next
> print
> print a' I suppose this value should be 10 instead of 11
>

Like Stefan, I like the WHILE/WEND approach.
a=1
while a <= 10
print a
a=a+1
wend
end

[Non-text portions of this message have been removed]
• Thank you mike, your “do” code is very interested to me… ________________________________ From: green8819 To:
Message 4 of 21 , Jan 28, 2013
• 0 Attachment
code is very interested to me…

________________________________
From: green8819 <mike4612@...>
To: libertybasic@yahoogroups.com
Sent: Tuesday, January 29, 2013 6:30 AM
Subject: [libertybasic] Re: Variable out of loop

Verdict?

I think NEXT is 'Not Guilty'

As Rod has pointed out, it's duty is to increment.
The FOR...TO decides whether to execute the loop.
We decide what should happen in between.

for x=1 to 10
do something
NEXT

Normally the value of x is not important after NEXT.
However, to be safe, we decide to EXIT the loop when x reahes upper limit.

For x=1 to 10
do something
if x=10 then EXIT for
next

Besides other alternatives already mentioned by contributors, the following is easy to understand:

x=0
do
x=x+1
print x
loop until x=10
print x

I still prefer good old FOR...NEXT. There is no need to initialize (x=0) or increment (x=x+1).

[Non-text portions of this message have been removed]
• ... Generally speaking, every loop construct has its purpose. For finite loops, where you exactly know the limits FOR..NEXT is best used. For loops where you
Message 5 of 21 , Jan 29, 2013
• 0 Attachment
Am 29.01.2013 07:54, schrieb Henrik:
> Thank you mike, your “do”
> code is very interested to me…
>

Generally speaking, every loop construct has its purpose.

For finite loops, where you exactly know the limits FOR..NEXT is best used.

For loops where you don't know the end limit, but an end condition, you
use WHILE..WEND or DO..LOOP.

Everything can be found in the help file under "Liberty BASIC v4.04 Help
=> Language Syntax and Usage => Logic and Structure => Looping Structures".

If you just read through the help file, you will have a brief knowledge
for what command to use in the current situation.

If you complain that the help file is lacking a search feature, this has
been taking care of already, see http://basic.wikispaces.com/HelpSearch

--
Stefan Pendl
http://stefanpendl.runbasichosting.com/

LB 4.04 Pro ... http://www.libertybasic.com/assist.html
LB 4.04 ....... http://www.libertybasic.com/lb404setup.exe

LB Community Wiki .............. http://basic.wikispaces.com/
LB Programmer's Encyclopedia ... http://lbpe.wikispaces.com/
LB Bug Tracker ................. http://libertybasicbugs.wikispaces.com/

Books at http://www.lulu.com/ and http://www.amazon.com/
Alyce Watson ... APIs for Liberty BASIC
Carl Gundel .... Beginning Programming with Liberty BASIC

Windows 7 Home Premium 64-bit SP1
AMD Turion X2 RM-70 2GHz, 4GB RAM
• Thank you Stefan, I found it starts with : Looping Liberty BASIC provides three constructions for looping. One is FOR/NEXT another is WHILE/WEND, and the third
Message 6 of 21 , Jan 29, 2013
• 0 Attachment
Thank you Stefan, I found it starts with :
Looping
Liberty BASIC provides three constructions for looping. One is
FOR/NEXT another is WHILE/WEND, and the third is DO LOOP.
I have to study them
very well.
Thanks again Stefan for the link of searching thru html.

________________________________
From: Stefan Pendl <pendl2megabit@...>
To: libertybasic@yahoogroups.com
Sent: Tuesday, January 29, 2013 1:21 PM
Subject: Re: [libertybasic] Re: Variable out of loop

Am 29.01.2013 07:54, schrieb Henrik:
> Thank you mike, your “do”
> code is very interested to me…
>

Generally speaking, every loop construct has its purpose.

For finite loops, where you exactly know the limits FOR..NEXT is best used.

For loops where you don't know the end limit, but an end condition, you
use WHILE..WEND or DO..LOOP.

Everything can be found in the help file under "Liberty BASIC v4.04 Help
=> Language Syntax and Usage => Logic and Structure => Looping Structures".

If you just read through the help file, you will have a brief knowledge
for what command to use in the current situation.

If you complain that the help file is lacking a search feature, this has
been taking care of already, see http://basic.wikispaces.com/HelpSearch

--
Stefan Pendl
http://stefanpendl.runbasichosting.com/

LB 4.04 Pro ... http://www.libertybasic.com/assist.html
LB 4.04 ....... http://www.libertybasic.com/lb404setup.exe

LB Community Wiki .............. http://basic.wikispaces.com/
LB Programmer's Encyclopedia ... http://lbpe.wikispaces.com/
LB Bug Tracker ................. http://libertybasicbugs.wikispaces.com/

Books at http://www.lulu.com/ and http://www.amazon.com/
Alyce Watson ... APIs for Liberty BASIC
Carl Gundel .... Beginning Programming with Liberty BASIC

Windows 7 Home Premium 64-bit SP1
AMD Turion X2 RM-70 2GHz, 4GB RAM

[Non-text portions of this message have been removed]
Your message has been successfully submitted and would be delivered to recipients shortly.