Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [143]

By Root 1534 0
= “”;

switch(die){

case 1:

output = ”I”;

break;

case 2:

output = ”II”;

break;

case 3:

output = ”III”;

break;

case 4:

output = ”IV”;

break;

case 5:

output = ”V”;

break;

case 6:

output = ”VI”;

break;

default:

output = ”PROBLEM!!!”;

} // end switch

Figure 2-5: Now we have ancient Roman dice. Useful if we come across any ancient Romans.

Creating an expression

The switch structure in the preceding code is organized a little bit differently than the if--else if business.

The switch keyword is followed immediately by an expression in parentheses. The expression is usually a variable with several possible values. The switch structure then provides a series of test values and code to execute in each case.

To create a switch statement:

1. Begin with the switch keyword.

This step sets up the structure. You’ll indent everything until the right brace (}) that ends the switch structure.

2. Indicate the expression.

The expression is usually a variable you want to compare against several values. The variable goes inside parentheses and is followed by a left brace ({).

3. Identify the first case.

Indicate the first value you want to compare the variable against. Be sure the case is the same type as the variable.

4. End the case description with a colon (:).

Be careful! Case lines end with a colon (indicating the beginning of a case) rather than the more typical semicolon. It’s easy to forget this difference.

5. Write code for the case.

You can write as many lines of code as you want inside each case. This code executes only if the expression is equal to the given case. Typically, all the code in a case is indented.

6. Indicate the end of the case with a break statement.

This statement tells the computer to jump out of the switch structure as soon as this case has been evaluated (which is almost always what you want).

7. Repeat with other cases.

Build similar code for all the other cases you wish to test.

8. Trap for surprises with default.

The special case default works like the else in an else if structure: It manages any cases that haven’t already been trapped. Even if you think you have all the bases covered, you should put some default code in place just in case.

You don’t need to put a break statement in the default clause, because it always happens at the end of the switch structure anyway.


Switching with style

The switch structure is powerful, but it can be tricky because the format is a little strange. Here are a few tips to keep in mind:

♦ You can compare any type of expression. If you’ve used another language (like C or Java), you may have learned that switches only work on numeric values. You can use JavaScript switches on any data type.

♦ It’s up to you to get the type correct. If you’re working with a numeric variable and you compare it against string values, you may not get the results you’re looking for.

♦ Don’t forget the colons. At first glance, the switch statement uses semicolons like most other JavaScript commands. Cases end with colons (:). Getting confused is easy to do.

♦ Break each case. Use the break statement to end each case, or you’ll get weird results.

Wouldn’t arrays be better? If you have some programming experience, you may argue that another solution involving something called arrays is better for this particular problem. I tend to agree, but for that solution, go to Chapter 4 of this minibook. Switches and if-else structures have their place, too.


Nesting if Statements

You can combine conditions in all kinds of crazy ways. One decision can include other decisions, which may incorporate other decisions. You can put if statements inside each other to manage this kind of (sometimes complicated) logic.

Figure 2-6 shows a particularly bizarre example. Imagine that you’re watching the coin toss at your favorite sporting event. Of course, a coin can be heads or tails. Just for the sake of argument, the referee also has a complex personality. Sometimes he’s a surfer, and sometimes

Return Main Page Previous Page Next Page

®Online Book Reader