#197525 - 2010-01-25 02:48 PM
question about the while loop function
|
richardv
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
|
|
|
|
#197527 - 2010-01-25 04:10 PM
Re: question about the while loop function
[Re: Mart]
|
Glenn Barnas
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 exampleDo
'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$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!
|
|
Top
|
|
|
|
#197528 - 2010-01-25 04:32 PM
Re: question about the while loop function
[Re: Glenn Barnas]
|
Richard H.
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.
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 :(
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
|
|
|
|
#197530 - 2010-01-25 07:31 PM
Re: question about the while loop function
[Re: Glenn Barnas]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
|
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1119 anonymous users online.
|
|
|