JavaScript Editor Javascript validator     Website design 

Team LiB
Previous Section Next Section

Working with Multiple Values

Often you will find yourself working with more complex data. For example, you might want to respond differently to each of the six possible rolls of a die. The Binary Dice program illustrated in Figures 3.7 and 3.8 demonstrates just such a situation.

Click To expand
Figure 3.7: The roll is a 5, and the program shows the binary representation of that value.
Click To expand
Figure 3.8: After rolling again, the program again reports the binary representation of the new roll.

Writing the Binary Dice Program

This program has a slightly more complex if structure than the others, because the binary value should be different for each of six possible outcomes. An ordinary if structure would not be sufficient, but it is possible to put several if structures together to solve this kind of dilemma.

<title>Binary Dice</title>
<h1>Binary Dice</h1>
<h3>Demonstrates multiple if structure</h3>

$roll = rand(1,6);
print "You rolled a $roll";
print "<br>";

if ($roll == 1){
  $binValue = "001";
} else if ($roll == 2){
  $binValue = "010";
} else if ($roll == 3){
  $binValue = "011";
} else if ($roll == 4){
  $binValue = "100";
} else if ($roll == 5){
  $binValue = "101";
} else if ($roll == 6){
  $binValue = "110";
} else {
  print "I don't know that one...";
} // end if

print "<br>";
print "<img src = die$roll.jpg>";
print "<br>";
print "In binary, that's $binValue";
print "<br>";
print "<br>";
print "<br>";

Refresh this page in the browser to roll another die.


Using Multiple else if Clauses

The binaryDice program still has only one if structure, but that structure has multiple else clauses. The first condition simply checks to see if $roll is equal to one. If it is, the appropriate code runs (assigning the binary representation of 1 to the $binValue variable.) If the first condition is false, the program looks at all the successive if else clauses until it finds a condition that evaluates to true. If none of the conditions is true, the code in the else clause will be executed.


You may be surprised that I even put an else clause in this code. Since you know the value of $roll must be between one and six, and we've checked each of those values, the program should never need to evaluate the else clause. Things in programming don't always work out the way you expect, so it's a great idea to have some code in an else clause even if you don't expect to ever need it. It's much better to get a message from your program explaining that something unexpected occurred than to have your program blow up inexplicably while your users are using it.

The indentation for a multiple-condition if statement is useful so you can tell which parts of the code are part of the if structure, and which parts are meant to be executed if a particular condition turns out to be true.

Team LiB
Previous Section Next Section

JavaScript Editor Javascript validator     Website design