Game Development Reference
In-Depth Information
Loops
Line 12 has something called a while statement, which indicates the beginning of a
while loop. Loops are parts of code that are executed over and over again. But before we
can learn about while loops, we need to learn a few other concepts first. Those concepts
are blocks, booleans, comparison operators, conditions, and finally, the while statement.
Blocks
A block is one or more lines of code grouped together with the same minimum amount
of indentation. You can tell where a block begins and ends by looking at the line's
indentation (that is, the number of spaces in front of the line).
A block begins when a line is indented by four spaces. Any following line that is also
indented by four spaces is part of the block. A block within a block begins when a line is
indented with another four spaces (for a total of eight spaces in front of the line). The block
ends when there is a line of code with the same indentation before the block started.
Below is a diagram of the code with the blocks outlined and numbered. The spaces have
black squares filled in to make them easier to count.
Figure 4-1: Blocks and their indentation. The black dots represent spaces.
For example, look at the code above. The spaces have been replaced with dark squares to
make them easier to count. Line 12 has an indentation of zero spaces and is not inside any
block. Line 13 has an indentation of four spaces. Since this indentation is larger than the
previous line's indentation, we can tell that a new block has started. Lines 14, 15, 17 and 19
also have four spaces for indentation. Both of these lines have the same amount of
indentation as the previous line, so we know they are in the same block. (We do not count
blank lines when we look for indentation.)
Search WWH ::




Custom Search