Page 2 of 2 <12
Topic Options
#199052 - 2010-07-20 03:14 PM Re: Basic Questions [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Here's an alternative to GOTO to exit a loop:
 Code:
; permanent loop with conditional exit
$Tag = 1
While $Tag     ; Loop forever while Tag is true
  ; do stuff here...
  If <EXIT CONDITION TEST>  ; if some condition exists where we should exit
    $Tag = 0
  EndIf
Loop
Tag represents a TRUE condition upon entry to the loop, but can be set to FALSE within the loop by any number of tests and conditons. Another common condition - perform a loop until any of several conditions exist.. simply add multiple exit condition tests. Any that sets Tag to false will cause an exit from the loop.

If you need to test for exit conditions at the top of the loop, simply move the tests to the top, and enclose your code in If $Tag / EndIf. The exit status will be tested upon entry, and the code executed only if Tag is true.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#199070 - 2010-07-22 05:01 PM Re: Basic Questions [Re: Allen]
sb1920alk Offline
Fresh Scripter

Registered: 2010-07-08
Posts: 15
Loc: USA
 Originally Posted By: Allen
Be careful of that "true"... it's not doing exactly what you think it is. It's only working because Kix is so forgiving.

Can you be more specific?

@Richard...ok I see what you mean.

@Glenn...that's pretty much what my first thought was. The problem is it doesn't exit the loop immidiately. For example:
 Code:
$Tag = 1
While $Tag     ; Loop forever while Tag is true
  ; do stuff here that should happen on every loop...
  If <EXIT CONDITION TEST>  ; if some condition exists where we should exit
    $Tag = 0
  EndIf
  ; do MORE stuff here that depends on the stuff earlier in the loop, but needs to not happen after the tag to exit the loop has been triggered.
Loop

It's not going to happen often, but it just seems unnecessary to force a certain style of coding in a free-format scripting language.

Top
#199071 - 2010-07-22 05:21 PM Re: Basic Questions [Re: sb1920alk]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4561
Loc: USA
Kix does not have a built in true and false... This script may help explain it... give it a whirl to see the results... or change the words in your code to see it always resolve to true.

 Code:
$exitloop=true
? $exitloop
$exitloop=false
? $exitloop
$exitloop=foobar
? $exitloop


Basically anything you put there will make exitloop evaluate to "true".

The good news... it's easy to work around... just add these to your script and the true and false evaluate properly.

 Code:
function true()
  $true=not 0
endfunction

function false()
  $false=not 1
endfunction



Top
#199075 - 2010-07-22 05:48 PM Re: Basic Questions [Re: sb1920alk]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Pretty simple, actually..
 Code:
$Tag = 1
While $Tag
  ; always do this stuff...
  If <EXIT CONDITION TEST> ; put your test here!
    $Tag = 0
  Else
    ; do this stuff unless we've been terminated
  EndIf
Loop
Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 2 of 2 <12


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 422 anonymous users online.
Newest Members
min_seow, Audio, Hoschi, Comet, rrosell
17881 Registered Users

Generated in 0.053 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org