if/if-else/Nested if -else
Selection Structure
The if Statement
if statement: a control structure that executes a block of
statements only if a certain condition is true
General syntax:
if (<test>) {
<statement(s)> ;
}
Example:
var gpa = 3.25;
if (gpa >= 3.0) {
alert("Good job! Have a cookie.");
}
2
if Statement Flow Chart
3
The if/else Statement
if/else statement: A control structure that executes one block of
statements if a certain condition is true, and a second block of
statements if it is false. We refer to each block as a branch.
General syntax:
if (<test>) {
<statement(s)> ;
} else {
<statement(s)> ;
}
Example:
var gpa = 3.25;
if (gpa >= 3.0) {
alert("Good job! Have a cookie.");
} else {
alert("No cookie for you!");
}
4
if/else Statement Flow Chart
5
Relational Expressions
The <test> used in an if or if/else statement must
evaluate to a Boolean value (true or false).
Relational expressions evaluate to Boolean values and
use the following relational operators:
Operator Meaning Example Value
== equals 1 + 1 == 2 true
!= does not equal 3.2 != 2.5 true
< less than 10 < 5 false
> greater than 10 > 5 true
<= less than or equal to 126 <= 100 false
>= greater than or equal to 5.0 >= 5.0 true
6
Evaluating Relational Expressions
Relational operators have lower precedence than math
operators.
5 * 7 >= 3 + 5 * (7 - 1)
5 * 7 >= 3 + 5 * 6
35 >= 3 + 30
35 >= 33
true
Relational operators should not be "chained" as they can
in algebra. WARNING! JavaScript will NOT complain if
you do so and you may get unexpected results.
2 <= 1 <= 10
false <= 10 ???
true
7
Errors In Coding
Many students new to if/else write code like this:
var percent = 85;
if (percent >= 90) {
alert("You got an A!");
}
if (percent >= 80) {
alert("You got a B!");
}
if (percent >= 70) {
alert("You got a C!");
}
if (percent >= 60) {
alert("You got a D!");
} else {
alert("You got an F!");
}
What will happen? What’s the problem?
You may get too many popup boxes. Try it out!
8
Nested if/else Statements
Nested if/else statement: A chain of if/else that can select
between many different outcomes based on several tests.
General syntax:
if (<test>) {
<statement(s)> ;
} else if (<test>) {
<statement(s)> ;
} else {
<statement(s)> ;
}
Example:
if (number > 0) {
alert("Positive");
} else if (number < 0) {
alert("Negative");
} else {
alert("Zero");
}
9
Nested if/else Variations
A nested if/else can end with an if or an else.
If it ends with else, one of the branches must be taken.
If it ends with if, the program might not execute any branch.
if (<test>) { if (<test>) {
<statement(s)>; <statement(s)>;
} else if (<test>) { } else if (<test>) {
<statement(s)>; <statement(s)>;
} else { } else if (<test>) {
<statement(s)>; <statement(s)>;
} }
10
Nested if/else Flow Chart
if (<test>) {
<statement(s)>;
} else if (<test>) {
<statement(s)>;
} else {
<statement(s)>;
}
11
Nested if/else if Flow Chart
if (<test>) {
<statement(s)>;
} else if (<test>) {
<statement(s)>;
} else if (<test>) {
<statement(s)>;
}
12
Nested if/else Variations
if (place == 1) {
alert("You win the gold medal!");
} else if (place == 2) {
alert("You win a silver medal!");
} else if (place == 3) {
alert("You earned a bronze medal.");
}
Are there any cases where this code will not print a
message?
Yes, if the place variable is not 1, 2, or 3.
How could we modify it to print a message to non-
medalists?
Add an else clause.
13
Sequential if Flow Chart
if (<test>) {
<statement(s)>;
}
if (<test>) {
<statement(s)>;
}
if (<test>) {
<statement(s)>;
}
14
Summary: if/else Structures
Choose exactly 1 set of statements Choose 0, 1, or more set of statements
if (<test>) { if (<test>) {
<statement(s)>; <statement(s)>;
} else if (<test>) { }
<statement(s)>; if (<test>) {
} else { <statement(s)>;
<statement(s)>; }
} if (<test>) {
<statement(s)>;
}
Choose 0 or 1 set of statements
if (<test>) {
<statement(s)>;
} else if (<test>) {
<statement(s)>;
} else if (<test>) {
<statement(s)>;
}
15
Which if/else Construct To Use?
Reading the user's GPA and printing whether the student is on
the dean's list (3.8 to 4.0) or honor roll (3.5 to 3.7)
Printing whether a number is even or odd
Printing whether a user is lower-class, middle-class, or upper-
class based on their income
Determining whether a number is divisible by 2, 3, and/or 5
Printing a user's grade of A, B, C, D, or F based on their
percentage in the course
16
Which if/else Construct To Use?
Reading the user's GPA and printing whether the student is on
the dean's list (3.8 to 4.0) or honor roll (3.5 to 3.7)
if / else if
Printing whether a number is even or odd
if / else
Printing whether a user is lower-class, middle-class, or upper-
class based on their income
if / else if / else
Determining whether a number is divisible by 2, 3, and/or 5
if / if / if
Printing a user's grade of A, B, C, D, or F based on their
percentage in the course
if / else if / else if / else if / else
17