Page 1 of 1 1
Topic Options
#197525 - 2010-01-25 02:48 PM question about the while loop function
richardv Offline
Just in Town

Registered: 2010-01-25
Posts: 1
Loc: Netherlands
hello,

i have to work with the command WHILE LOOP but its very difficult for me since i am new with kixtart i hope someone could explain me how it works.

thanks,

Richard

Top
#197526 - 2010-01-25 03:05 PM Re: question about the while loop function [Re: richardv]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Hi Richard,

Welcome to the board.

A while loop is not that difficult. It enables you to perform certain tasks until a threshold is reached. The example below will display the contents of $a as long as it is less than 15. You can do any task you want in a while loop. It will fire up each time until the set threshold is reached.

 Code:
$a = 0
While $a < 15
	? "Variable A has a value of: " + $a
	$a = $a + 1
Loop
?"While loop has ended because variable A is no longer less than 15."

Sleep 15
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#197527 - 2010-01-25 04:10 PM Re: question about the while loop function [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Just some additional info -

The While/Loop command pair is similar to the Do/Until command pair - both perform a set of instructions until a test evaluates to true. While/Loop is fairly common, compared to Do/Until, so I'm mentioning it because there is a significant difference between the two that could affect your decision to use one or the other.

A While loop will be executed ZERO or more times, because the test is evaluated BEFORE the loop commands are executed.

The commands in a Do/Until loop will always be executed at least once, because the test is evaluated at the END of the loop.

If you want to run all of the commands at least once, you need to use Do/Until, otherwise While/Loop will usually fit the bill.

In Mart's example, the loop control variable is statically defined, so he knows the loop will be executed at least once. If the value of "$a" were set via external means, you might not know what it contained - what if it were the result of a calculation and was set to 21?

The point is - there are two types of loops to be aware of, and each has their own advantages. You need to choose the loop control based on your needs. Here's an alternative example
 Code:
Do
  'Do you want to quit? (Y/N) '
  Get $Answer
  @CRLF
Until $Answer = 'Y'
This example is controlled by external data, and must execute at least once. It will keep looping until the user enters "Y".
Here's similar code with While/Loop
 Code:
$DefaultAnswer = 'N' ; usually set by reading a config file
$Answer = $DefaultAnswer
While $Answer <> 'Y'
  'Do you want to quit? (Y/N) '
  Get $Answer
  @CRLF
Loop
Here, if the defuault answer is Y, the loop will never process at all. You can try the second example with $DefaultAnswer set to "Y" to see the difference.

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

Top
#197528 - 2010-01-25 04:32 PM Re: question about the while loop function [Re: Glenn Barnas]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
One more example. This is a little more complex as it has two while loops. I'll give you the loop example in plain english, then in KiXtart syntax.

Assume that you have a small glass and a large bottle of beer whose volume is an exact multiple of the glass volume.

 Code:
While there is any beer left in the bottle

   While there is room in the glass
      pour some beer into the glass
   Go back and check the glass

   Drink the beer. Lovely :)

Go back and check the bottle

Damn, no more beer :(


 Code:
While $iBeerInBottle > 0

   While $iBeerInGlass < $iGlassVolume
      $iBeerInBottle=$iBeerInBottle-1
      $iBeerInGlass=$iBeerInGlass+1
   Loop

   "Glug-glug-glug...Ahhhh!"+@CRLF   
   $iBeerInGlass=0

Loop

"Go to supermarket and buy more beer."+@CRLF

Top
#197529 - 2010-01-25 05:26 PM Re: question about the while loop function [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
It may be a more complex example, but certainly ranks high on the Fun level! ;\)

G-
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197530 - 2010-01-25 07:31 PM Re: question about the while loop function [Re: Glenn Barnas]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
 Code:
If @TIME > 18:00:00 AND @TIME < 08:00:00
  "Go to Gas Station and buy more beer."+@CRLF
  While $iMetersToGasStation > 0
    "Step..Step..Step...got..to..get...beer..."+@CRLF
    $iMetersToGasStation=$iMetersToGasStation-1
  Loop
Else
  "Go to supermarket and buy more beer."+@CRLF
EndIf

Top
#197532 - 2010-01-25 07:56 PM Re: question about the while loop function [Re: Arend_]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA
iiiieeeee.... it's a dork convention in here. ;\)
_________________________
(... better days ahead)

Top
#197538 - 2010-01-26 09:22 AM Re: question about the while loop function [Re: Allen]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: Allen
iiiieeeee.... it's a dork convention in here. ;\)


Set phasers to stun

Top
#197542 - 2010-01-26 01:59 PM Re: question about the while loop function [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
ey Cap'n!!
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197543 - 2010-01-26 08:44 PM Re: question about the while loop function [Re: Glenn Barnas]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Lock n' Load, Rock n' Roll!
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 1955 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.07 seconds in which 0.031 seconds were spent on a total of 14 queries. Zlib compression enabled.

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