In the last two modules, we saw how sometimes, not all the code we write will be executed -- programs can be more interesting if they don't do the same thing every time. We've seen how to do this through conditional execution with if/statements and also (if you think about it) with events. If you run our simple car drive program with events but never click on any object, the code that controls that event never happens -- so code that is written is, in a way, conditionally executed only if you click on an object.
However, did you ever think about how we could get a computer to do MORE work (rather than skipping some)? That is, is there a way that we can write a small amount of code, but the computer does it a lot? If you think about it, events are like this. Every time you click on a magnet in our car game, the code in the method handler for an object click is executed. If you click 2 different times, it executes the handler code 2 times. If you click 10 times, then that code will be executed 10 times -- once for each time you click!
This is one of the real powers of computers -- we can give them some instructions, but in some way ask the computer to do those over and over again. Computers are good at doing things repeatedly -- they don't get tired or bored. This is why computers are used to count things like voting ballots, or to count money say at a change counting machine, or to scan and add up the cost of groceries or other items when you check out at a store.
In this module we will learn two kinds of loops -- control structures (like if/elses and doTogethers). We will use counted loop tiles -- where everything on that tile is executed a specific number of times (2 times, 5 times, etc.). We will also use while loop tiles -- where everything on that tile is executed as long as some condition is true (keep playing while you have life points, keep driving as long as the stoplight is not red, etc).
Things you will learn in this module:
Vocabulary:
Abstraction: The process of hiding the complexity or detail of an operation while still presenting its essential characteristics. This is important in helping the human brain be able to work on more complex problems. Putting the many instructions that may carry out a given operation into a method with a name that represents the operation is an example of abstraction.
Counted loop: A control structure (tile) that causes the instructions listed on the tile to be executed some number of times. This number can be be a constant number (like "4" or "10") or it can be a value calculated by a function when the program runs (bird's distance to tree).
Nested loops: A loop is said to be nested inside another loop when one loop tile in on top of another loop tile. This can be done with both counted and while loops, but it is more common to have nested counted loops. The loop tile on top of the other is called the Inner Loop and the base loop is called the Outer Loop.
While loop: A control structure (tile) that causes the instructions listed on the tile to be executed some number of times -- which is not known or fixed beforehand. Instead, a boolean expression is used to control whether the instructions should be executed (again). If the boolean expression is true, then the computer executes the code on the tile -- then control goes back to the top of the tile and the boolean expression is checked again to see if the code on the tile should be repeated. It is possible for the code on a while loop tile to never be executed if the boolean expression evaluates to false when the loop tile is encountered.