Page 1 of 1 1
Topic Options
#18072 - 2002-02-27 10:32 PM Exit a While Loop
garyl Offline
Lurker

Registered: 2002-02-27
Posts: 1
How do you break out of a while loop without exiting the script?
Top
#18073 - 2002-02-28 02:11 AM Re: Exit a While Loop
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Well, there are a bunch of ways to do this. Here's a quick and dirty way to get out of an infinite loop:


break on

while forever
 
 if $i = 100
  goto "GetOut"
 endif
 
 $i = $i + 1

loop

:GetOut

?"i=$i"

exit

But normally, one would include a condition in the WHILE statement that would break out of the loop gracefully, like this:

break on

while $i < 100
 
 $i = $i + 1

loop

?"i=$i"

exit

Or, if you don't really have anything handy to loop against, you can always just create a flag and set it to false insode the loop, like this:

break on

$flag = 1 ; true

while $flag

 $i = $i + 1

 if $i = 100
  
  $flag = 0 ; false

 endif

loop

?"i=$i"

exit

Hope this helps

-Shawn

[ 28 February 2002: Message edited by: Shawn ]

Top
#18074 - 2002-02-28 02:29 AM Re: Exit a While Loop
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
OK, so I (nearly) got hung, shot, and pissed on, for using GOTO to exit a loop (see Kixtart - enhancement for RETURN command) but Shawn can do it with impunity?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#18075 - 2002-02-28 03:52 AM Re: Exit a While Loop
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
You right Les, I don't know what the hell came over me. I read the question quickly and for some reason, my first thought, was that Gary was caught in some kinda "hard-to-get-out-of" loop, and GOTO just seemed like the right thing to say at the time.

But by the same token, like a good kixter, i did present the other (better?) alternatives. Even going so far as to calling the alternatives more "normal" and "gracefull" ...

But the thoughts and mental images I had, of Gary being caught-up in some kinda of infinite loop, going 'round and 'round and 'round, forever and ever, it was just too much for me to bear ...

So your Honour ... I plead ... NOT GUILTY ... by reason of insanity!

-Shawn

[ 28 February 2002: Message edited by: Shawn ]

Top
#18076 - 2002-02-28 03:58 AM Re: Exit a While Loop
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
The Crown withdraws...

Jocularity aside, the best way out of a loop-of-death, is to not get into one in the first place.

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#18077 - 2002-02-28 04:02 AM Re: Exit a While Loop
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
[shawn purses lips and nods head in agreement]
Top
#18078 - 2002-02-28 06:30 AM Re: Exit a While Loop
MCA Offline
KiX Supporter
*****

Registered: 2000-04-28
Posts: 5152
Loc: Netherlands, EU
Dear,

Welcome to the board.

We just missing following example:

  • based on @error status. mostly used by reading file contents
    code:

    IF (Open(1,"c:\input.txt") = 0)
    $result=ReadLine(1)
    WHILE (@error = 0)
    ? $result
    $result=ReadLine(1)
    LOOP
    IF Close(1)
    ENDIF
    ENDIF



greetings.
_________________________
email scripting@wanadoo.nl homepage scripting@wanadoo.nl | Links | Summary of Site Site KiXforms FAQ kixtart.org library collection mirror MCA | FAQ & UDF help file UDF kixtart.org library collection mirror MCA | mirror USA | mirror europe UDF scriptlogic library collection UDFs | mirror MCA

Top
Page 1 of 1 1


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

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.147 seconds in which 0.105 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