Difference between revisions of "While"

From MiniScript Wiki
Jump to navigation Jump to search
 
Line 2: Line 2:
  
 
Within the loop, execution may skip directly to the next iteration with [[continue]], or jump out of the loop by using a [[break]] statement.
 
Within the loop, execution may skip directly to the next iteration with [[continue]], or jump out of the loop by using a [[break]] statement.
 +
 +
== Example ==
 +
<ms>bunnies = 2
 +
while bunnies < 200
 +
    print bunnies + " bunnies"
 +
    bunnies = bunnies * 2
 +
end while
 +
print "Too many bunnies!"</ms>
 +
 +
'''Output:'''
 +
<pre>2 bunnies
 +
4 bunnies
 +
8 bunnies
 +
16 bunnies
 +
32 bunnies
 +
64 bunnies
 +
128 bunnies
 +
Too many bunnies!</pre>
 +
  
 
[[Category:Language]]
 
[[Category:Language]]
 
[[Category:Keywords]]
 
[[Category:Keywords]]
 
{{stub}}
 

Latest revision as of 18:35, 28 May 2022

while is a keyword used to loop as long as some condition is true. The condition is tested before each iteration of the loop (including the first). When the condition is not true, execution jumps to the next statement after end while.

Within the loop, execution may skip directly to the next iteration with continue, or jump out of the loop by using a break statement.

Example

bunnies = 2
while bunnies < 200
    print bunnies + " bunnies"
    bunnies = bunnies * 2
end while
print "Too many bunnies!"

Output:

2 bunnies
4 bunnies
8 bunnies
16 bunnies
32 bunnies
64 bunnies
128 bunnies
Too many bunnies!