Download presentation
Presentation is loading. Please wait.
1
Testing and Comparing Values
Kevin Harville
2
If Statements If (true) { alert(“condition is true”); }
3
If on one line If (something == true) alert(“It is true”);
4
If Else Statements if (condition == true) {
alert(“condition is true”); } else { alert(“condition is not true”);
5
Comparisons if (condition == true) == Is equal to != Is not equal to
> Is greater than < Is less than >= Is greater than or equal to <= Is less than or equal to
6
Multiple Comparisons AND && If (a >= 1) && (a <= 7) {
alert(“Both conditions true”) }
7
Multiple Comparisons OR ||
Uses the “pipe” symbol, usually on the right side of the keyboard. If (a <= 1) || (a >= 7) { alert(“At least one condition true”) }
8
Multiple Conditions - switch
The switch statement allows the computer to choose from several paths based on the value of a given variable: We start with the switch keyword followed by the variable to be the basis of the choice… switch(myVariable){ }
9
Multiple Conditions - switch
We start with the switch keyword followed by the variable to be the basis of the choice… Then we list the choices, or cases. switch(myVariable){ case "blue": alert("You chose Blue!"); break; }
10
Multiple Conditions - switch
switch(myVariable){ case "blue": alert("You chose Blue!") break; case "red": alert("You chose Red!") }
11
Breaking out of Switch Break is necessary to keep subsequent lines from executing switch(myVariable){ case "blue": alert("You chose Blue!") break; case "red": alert("You chose Red!") }
12
Use a default choice switch(myVariable){ case "blue":
alert("You chose Blue!"); break; case "red": alert("You chose Red!"); default: alert("You choose something else!"); }
13
Another special feature…
To quickly assign a variable one of two values based on a condition being true: myValue = (x==1) ? "yes" : "no"
14
Summary If If / Else Switch / Case / Break (condition) ? True : False
&& || == < > <= >= !=
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.