WDI Fundamentals

WDI Fundamentals Unit 9


By the end of this unit, you'll be able to:
  • Use conditional statements to control the flow of program execution.
  • Use loops to repeat an operation or action based on the value of a condition.

In the previous unit, we spent some time writing very simple programs — sets of instructions that were interpreted by our computer one at a time, in order. Sounds kind of like a recipe, right?

Suppose, however, that we were trying to cook something a little more complicated. For example, consider the following excerpt from "French Toast à la GA:"

...

3) Whisk eggs, milk, honey, and kosher salt until eggs are no longer visible.

4) Dip your bread slices in the custard. If you're using a pre-sliced loaf and the slices are thin, a short dip (just enough to coat both sides) should be enough. However, if your slices are thick, you may want to give the bread a minute or two to soak up the custard.

5) Transfer the slices to your frying pan and cook on a medium-low heat until brown on the bottom.

...

Instructions like these require the cook to make decisions based on available data. In the context of programming, this is called control flow, because it specifies the flow of the computer's actions through the program.

In this unit, we will look at how control flow is implemented in JavaScript. In particular, we'll focus on making decisions (conditionals) and repeated behavior (loops).

Let's get started.