Tải bản đầy đủ - 0 (trang)
switch statements: how to start them

switch statements: how to start them

Tải bản đầy đủ - 0trang

For the moment, I want you to focus on just the first three lines of the code above.

1. Begins with the keyword switch. Bumping up against it is the variable that's being tested,

inside parentheses. Then there's an opening curly bracket.

2. The first possibility, that the variable dayOfWeek has the value "Sat". Begins with the

keyword case. Then the value that is being tried, "Sat". Then a space and a colon.

3. The statement that executes if the test passes—if dayOfWeek does, in fact, have the value

"Sat". This statement is indented. Any number of statements can execute if the test passes.



129



Find the interactive coding exercises for this chapter at:

http://www.ASmarterWayToLearn.com/js/39.html



130



40

switch statements:

How to complete them

In the last chapter you focused on the first three lines of this switch statement.

1

2

3

4

5

6

7

8

9

10

11

12

13



switch(dayOfWk) {

case "Sat" :

alert("Whoopee");

break;

case "Sun" :

alert("Whoopee");

break;

case "Fri" :

alert("TGIF!");

break;

default :

alert("Shoot me now!");

}



In this chapter you'll tackle the rest of the code.

The first line of an if statement is followed by a statement (or statements) that executes if

the condition is true. A switch statement works similarly. On the line below each case clause,

there's a statement (or statements) that executes if the case is true.

Once again, the code that executes if the case is true is indented 2 spaces. (The universal

convention is that it is indented by some amount. Opinions differ on how much is best. I

standardize on a 2-space indentation, which is common but far from universal.)

But why do all of the cases except the last one include a break statement?

JavaScript has an inconvenient quirk: After a true case is found, JavaScript not only

executes the statement(s) immediately below that case. It executes all the statements for all the

cases below it. So after a true case is found and the conditional code executes, you need to

jump out of the switch block by coding a break statement. If, for example, you omit the break

statements in the code above, this happens:

1.

2.

3.

4.



An alert displays saying "Whoopee!"

A second alert displays saying "Whoopee!"

A third alert displays saying "TGIF!"

A fourth alert displays saying "Shoot me now."

Now let's look at line 11.



11 default :

12

alert("Shoot me now!");

13 }



131



The keyword default works like else in an if...else statement. The code that follows it

executes if none of the conditions above it are met. So, in the example above, if dayOfWk isn't

"Sat" or "Sun" or "Fri"—if it's anything other than those three values—an alert displays saying

"Shoot me now."

Note that default is followed by a space and a colon, just like the case clauses above it.

Note also that there's no break statement. That's because default always comes last,

which means there are no statements below it to execute inappropriately.

In a switch statement, default code is optional, just as else code is optional after an if

statement. Without the default code, if none of the cases test true, nothing happens.

When there is no default code, careful coders include a break statement after the last

condition anyway as insurance, even though it may be superfluous. If you decide to add a new

condition to the end later, you won't have to remember to add a break to the block above it in

order to avoid a disastrous cascade of statements.



132



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

switch statements: how to start them

Tải bản đầy đủ ngay(0 tr)

×