Tutorial 4 - Decision Making and Loops
A Numerical Accuracy Problem
Because of the way JavaScript stores real numbers, the equality operator ==, should not be used to compare two real numbers. Instead of doing a direct comparison you should compare the difference of the two numbers to some very small constant. (e.g.
x/y == 0.35
should be programmed as;
abs(x/y - 0.35) < EPSILON (where EPSILON = 0.0000001)
The if-else Statement
if (expression)
statement1; //do this when expression is true
else
statement2; //do this when expression is falseThe else expression may be omitted.
if (expression)
statement1; //do this when expression is true
if (taxable <= WAGE_LIMIT)
taxes = RATE1 * taxable;
else
taxes = RATE2 * (taxable - WAGE_LIMIT) + BRACKET1_TAX;
Compound Statements
A compound statement is any number of single statements contained between braces. Compound statements may be used with if-else statements.
Nested if Statements
Without the braces the logic of the above statement is different.
if (hours >= 40)
if (hours >= 50)
document.write("Bonus");
else // 40 < hours < 50
document.write("Lazy");
if (exp1)Exercise caution when arranging if-else chains. The first expression that is true determines which statements will be executed. The remainder of the statements in the chain will be skipped. If the expressions are not in the correct sequence, you can get an if-else chain where it is impossible to reach the interior statements.
statement1;
else if (exp2)
statement2;
else
statement3;
The switch Statement
switch (expression)
{
case value_1: // values can be different types
sta_1;
sta_2;
.
.
break; // ends the switch statements
case value_2:
sta_m;
sta_n;
.
.
break; // forgetting break causes all remaining ...
. // statements to be executed
.
case value_m: case value_n: case value_o:
sta_w;
sta_x;
.
.
break;
default: // optional
sta_aa;
sta_bb;
}
Example:
var num;Common Programming Errors
for (num=1; num <= 5; num++) {
switch (num) {
case 1:
document.writeln("num = 1");
break;
case 2: case 3:
document.writeln("num = 2 or 3");
break;
default:
document.writeln("num >= 4");
}
}
HTML
Radio Buttons
<SCRIPT LANGUAGE="JavaScript1.2">
<!-- Hide from incompatible browsers
// function to handle radio button
function scoreQuest(answer) {
switch (answer) {
case "a":
alert("Sorry Wrong
Answer :(");
break;
case "b":
alert("Sorry Wrong
Answer :(");
break;
case "c":
alert("Sorry Wrong
Answer :(");
break;
case "d":
alert("Of course,
Captain Christopher Pike :)");
}
}
// Stop hiding from incompatible browsers -->
</SCRIPT>
<!-- Display a question along with four possible
answers using radio buttons -->
<FORM>
<B> Who was the very first captain of the starship
Enterprise?</B><P>
<INPUT TYPE=radio NAME=quest1 VALUE="a"
onClick="scoreQuest(this.value)">Captain
Kirk<BR>
<INPUT TYPE=radio NAME=quest1 VALUE="b"
onClick="scoreQuest(this.value)">Captain
Picard<BR>
<INPUT TYPE=radio NAME=quest1 VALUE="c"
onClick="scoreQuest(this.value)">Captain
Janeway<BR>
<INPUT TYPE=radio NAME=quest1 VALUE="d"
onClick="scoreQuest(this.value)">Captain
Pike<BR>
</FORM>
Make sure the expression is modified inside the loop structure or the
program will loop forever (an infinite loop).
// an example that loops 10 times
var count = 1;
while (count <= 10) {
document.writeln(count);
count++; // increment count
}
// an example that counts backwards
var count = 10;
while (count > 0) {
document.writeln(count);
count--; // increment count
}
var count = 2;
do {
document.writeln("The count = " + count);
count++;
} while (count < 10);
The for Statement
The for statement is generally used in cases when you know in advance
how many loop iterations you want to perform.
The for…in Statement
A loop structure that automatically loops through all of the properties
within a JavaScript object. The syntax is:
function Animal(type, sound, transport) {
this.animal_type = type; // object property
this.animal_sound = sound; // object property
this.animal_transport = transport; //object property
}livestock = new Animal("cow", "moo", "walk"); // instantiate object
for (prop in livestock) {
document.writeln(prop); // print the names of the properties
}
The output of the above loop would be:
animal_type
animal_sound
animal_transport
with Statement
The with statement is used when working with object properties.
Using the with statement saves you from continually retyping the
name of the object when referencing multiple properties from the same object.
The syntax for the with statement is:
with (object) {
statement(s);
}
The following is an example that illustrates the usage of the with
statement:
// accessing properties without the with statementbreak and continue Statements
document.writeln("Mark Twain wrote: ");
document.writeln("Everybody talks about the weather, ");
document.writeln("but nobody does anything about it.");// accessing properties using the with statement
with (document) { // no need to type document.
writeln("Mark Twain wrote: ");
writeln("Everybody talks about the weather, ");
writeln("but nobody does anything about it.");
}
The previous loop results in the following display:
1
2
// A demo of continue
for(var count=1; count <=5; count++) {
if(count == 3)
continue;
document.writeln(count);
}
The previous loop results in the following display:
1
2
4
5
Page 198 numbers 2, 3 and 4