HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [142]
Figure 2-4:
A die for the true geek gamer.
Binary?
Binary notation is the underlying structure of all data in a computer. It uses ones and zeroes to store other numbers, which you can combine to form everything you see on the computer, from graphics to text to music videos and adventure games. Here’s a quick conversion chart so that you can read the dice:
Die Number Binary Notation
1 001
2 010
3 011
4 100
5 101
6 110
You can survive just fine without knowing binary (unless you’re a computer science major — then you’re expected to dream in binary). Still, it’s kind of cool to know how things really work.
A simple if-else structure isn’t sufficient here because you have six options. (if-else gives you only two choices). Here’s some code that uses another variation of if and else:
This program begins with an ordinary if statement, but it has a number of else clauses. You can include as many else clauses as you want if each includes its own condition.
To see how this program works, imagine the computer generates the value 3.
1. The first condition (die == 1) is false, so the program immediately jumps to the next else.
2. Step 1 sets up another condition (die == 2), which is also false, so control goes to the next else clause.
3. This step has yet another condition (die == 3), which is true, so the code inside this clause executes (alerting the value “011”).
4. A condition has finally been triggered, so the computer skips all the other else conditions and moves to the line after the end if.
5. This step is the last line of code, so the program ends.
Solving the mystery of the unnecessary else
When you use multiple conditions, you can (and should) still indicate an ordinary else clause without a condition as your last choice. This special condition sets up code that should happen if none of the other conditions is triggered. It’s useful as a fallback position, in case you didn’t anticipate a condition in the else if clauses.
If you think carefully about the binary dice program, the else clause seems superfluous. (I love that word.) It isn’t really necessary! You went through all that trouble to create a random number scheme that guarantees you’ll have an integer between 1 and 6. If you checked for all six values, why have an else clause? It should never be needed.
There’s a big difference between what should happen and what does happen. Even if you think you’ve covered every single case, you’re going to be surprised every once in a while. If you use a multiple if structure, you should always incorporate an else clause to check for surprises. It doesn’t need to do much but inform you that something has gone terribly wrong.
Using switch for More Complex Branches
When you have one expression that may have multiple values — as is the case when rolling a die, as described in the preceding sections — you may want to take advantage of a handy tool called switch for exactly this type of situation. Take a look at Figure 2-5, which is a variation of the die roller.
Once again, I start with an ordinary 1–6 integer and assign a new value based on the original roll. This time, I use another structure specialized for “one expression with lots of values” situations. Take a look at the following code:
-->