Part 04 - Flow Control - Loops
|Definition: For loop|
A loop whose body gets obeyed once for each item in a sequence.
for loop in Boo is not like the
for loop in languages like C and C#. It is more similar to a
The most common usage for a
for loop is in conjuction with the
range function creates an enumerator which yields numbers.
join function in this case, will create a string from an enumerator.
range can be called 3 ways:
range(start, end, step)
To be used in a
for loop is quite easy.
|Practically as fast as C#'s|
|Definition: While loop|
A structure in a computer program that allows a sequence of instructions to be repeated while some condition remains true.
while loop is very similar to an
if statement, except that it will repeat itself as long as its condition is true.
In case you didn't guess,
i += 1 adds
A keyword used to resume program execution at the end of the current loop.
continue keyword is used when looping. It will cause the position of the code to return to the start of the loop (as long as the condition still holds).
This skips the
i is even, causing only the odds to be printed out.
i % 2 actually takes the remainder of
i / 2, and checks it against 0.
unless loop is very similar to other languages do-while statement.
Normally, this would be a simple while loop.
This is a good method of doing things if you want to accomplish something at least once or have the loop set itself up.
pass keyword is useful if you don't want to accomplish anything when defining a code block.
- print out all the numbers from 10 to 1.
- print out all the squares from 1 to 100.
Go on to Part 05 - Containers and Casting