Decisions

Notes from April 13, 2005 class

Comparisons

Left-hand side (LHS)Comparison operator
Is 23 (LHS) less than 45 (RHS)?
Right-hand side (RHS)
23<45

 

Operator symbolPurpose
== Tests is LHS is equal to RHS
< Tests if LHS is less than RHS
> Tests if LHS is greater than RHS
<= Tests if LHS is less than or equal to RHS
>= Tests if LHS is greather than or equal to RHS
!= Tests if LHS is not equal to RHS

Assignment vs. comparison

A single equal sign = assigns a value to a variable.

A double equal sign == compares the value of two variables.

The if statement

The test condition
Notice: the Test condition does not end with a semicolon
if (theGlass <= halfEmpty)
The curly braces mark a block of code. If the condition of an if statement is true, JavaScript executes the next line or block of ode following the if statement.{
alert("Pessimist!");
document.write("You are a pessimist.");
}

See page 68 to add an if statement to the temperature calculator we made in class last week.

Logical operators

JavaScript can test for multiple conditions at once.

OperatorSymbolWhat it means
AND&& Both conditions must be met
OR|| Either condition needs to be met
NOT! The condition should not be met

<script language="JavaScript" type="text/javascript">

var myAge=Number(prompt("How old are you?",30));

// AND statement
if (myAge >= 13 && myAge <=19)
{
document.write("You are a teenager.<br />");
}

//OR statement
if (myAge <=12 || myAge >=60)
{
document.write("You may be eligible for some sort of age-based discount.<br />");
}

//NOT statement
if ( myAge!=21)
{
document.write("Did you get a free drink on your birthday? Probably not.<br />");
}

//else
else
{
document.write("Lots of places must've offered you a free drink on your birthday.<br />");
}

//Multiple conditions
if ( (myAge >=16 && myAge <=25) || (myAge >=65 && myAge <= 90) )
{
document.write("I'll bet your car insurance rates are pretty high.<br />");
}

//if, else, and else if
if (myAge == 18)
{
document.write("This is the first year you can vote.<br />");
}
else if (myAge < 18)
{
document.write("You can't vote till you're 18.<br />");
}
else
{
document.write("You've had a few opportunities to vote.<br />");
}

</script>

The switch statement

A switch statement switches to the code where the case matches.

The test expressionThe variable expression being checked.
The case statements Checking for possible values. If a match is found, then execution starts below the case statement...
The break statements ... and execution of the case ends at the break statement.
The default statementThis code executes when none of the case statements matched.

<html>
<body>
<script language="JavaScript" type="text/javascript">

var secretNumber = prompt("Pick a number between 1 and 5:","");
secretNumber = parseInt(secretNumber);

//we'll use 3 as our secret number;
//let's hope no one reads the source code or they'll know!

switch (secretNumber)
{
case "one":
/*There's no break statement here, so if if the case statement for value 1 is matched, execution simply continues until the break statement under case 2, letting us execute the same code for both cases */

case 2:
document.write("Too low!");
break;

case 3:
document.write("You guessed the secret number!");
break;

case 4:
case 5:
document.write("Too high!");
break;

default:
document.write("You did not enter a number between 1 and 5");
break;

}

document.write("<br />Thank you for your participation.");

</script>
</body>
</html>

Coming next: Looping

As promised, now we get to learn about loops.