Difference between revisions of "While"
Jump to navigation
Jump to search
(Created page with "<c>while</c> 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 firs...") |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
<c>while</c> is a [[:Category:Keywords|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 <c>end while</c>. | <c>while</c> is a [[:Category:Keywords|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 <c>end while</c>. | ||
− | Within the loop, execution may | + | 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]] | ||
− | |||
− |
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!