-
Notifications
You must be signed in to change notification settings - Fork 0
Control Flow
2.1 - Boolean Values
2.2 - Logical Operators
2.3 - Conditional Statements
Sometimes we do not want our console to run through every single line of code. There are times when we want the computer to take different actions depending on some situation.
Boolean is a data type that can only be one of two values - true
or false
.
Operator | Name | Description | Example |
---|---|---|---|
== | Equal | Returns true if the operands match | 3 == 3 |
!= | Not Equal | Returns true if the operands do not match | 5 != 3 |
=== | Strictly equal | Returns true if the operands match and are of same type | 5 === 5 |
!== | Not strictly equal | Returns true if the operands do not match or aren’t of same type | 6 !== "6" |
< | Less than | Returns true if the left operand is less than the right operand | 5 < 7 |
<= | Less than or equal | Returns true if the left operand is less than or equal to the right operand | 4 <= 4 |
> | Greater than | Returns true if the left operand is greater than the right operand | 5 > 2 |
>= | Greater than or equal | Returns true if the left operand is greater than or equal to the right operand | 5 >= 4 |
Operator | Name | Description | Example |
---|---|---|---|
&& | And | Returns true if both the left operand AND the right operand are true | expr1 && expr2 |
|| | Or | Returns true if either the left operand OR the right operand is true | expr1 || expr2 |
! | Negation | Returns true if the expression is true | !expr1 |
Conditional statements are used to control a program to do different things depending on some condition. Some examples:
- IF you are feeling cold, THEN wear a sweater.
- IF the string contains more than 140 characters, THEN disable the "Tweet" button, ELSE keep the "Tweet" button enabled.
To specify a block of code to be executed if a condition is true, we use an if
statement.
if (condition) {
block statement
}
For example:
var testGrade = "A";
if (testGrade == "A") {
alert("Great job!");
}
Things to note:
- Conditions are in parentheses
()
- The block statement is in curly braces
{}
- Expressions end with
;
semicolons
We can also make the program execute some code if the condition is NOT met. To do so, we use an else
statement.
var grade = "B";
if (grade == "A") {
alert("Great job! Keep it up!");
} else {
alert("Don't give up! Let's study more!");
}
What result will we get?
We can make our if/else statements more specific by adding additional conditional statements using else if
.
var grade = "B";
if (grade == "A") {
alert("Great job! Keep it up!");
} else if (grade == "B") {
alert("Not too bad!");
} else {
alert("Don't give up! Let's study more!");
}
What result will we get here?
Important Note: The program goes through the code in order, meaning the order of your else if
statements matter!
Write a series of if/else statements and use alert()
to display the student’s test grade based on their test score. (How many different solutions can you come up with?)
Grade | Score |
---|---|
A | 100 - 90 |
B | 89 - 80 |
C | 79 - 70 |
D | 69 - 60 |
F | Below 60 |