LEARNING JAVASCRIPT - Trang 79

CHAPTER 4

Control Flow

A common metaphor given to beginning programmers is following a recipe. That

metaphor can be useful, but it has an unfortunate weakness: to achieve repeatable

results in the kitchen, one has to minimize choices. Once a recipe is refined, the idea

is that it is followed, step by step, with little to no variation. Occasionally, of course,

there will be choices: “substitute butter for lard,” or “season to taste,” but a recipe is

primarily a list of steps to be followed in order.
This chapter is all about change and choice: giving your program the ability to

respond to changing conditions and intelligently automate repetitive tasks.

If you already have programming experience, especially in a lan‐

guage with a syntax inherited from C (C++, Java, C#), and are

comfortable with control flow statements, you can safely skim or

skip the first part of this chapter. If you do, however, you won’t

learn anything about the gambling habits of 19th-century sailors.

A Control Flow Primer

Chances are, you’ve been exposed to the concept of a flowchart, which is a visual way

of representing control flow. As our running example in this chapter, we’re going to

write a simulation. Specifically, we are going to simulate a Midshipman in the Royal

Navy in the mid-19th century playing Crown and Anchor, a betting game popular at

the time.
The game is simple: there’s a mat with six squares with symbols for “Crown,”

“Anchor,” “Heart,” “Club,” “Spade,” and “Diamond.” The sailor places any number of

55

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.