Conditionals (If / Then / Else)
If, Then, Otherwise
Most code can be boiled down to "If this is true, do this."
First, let's look at how to compare values to see if something is true.
Comparison Operators
Operator | Name | Example |
== | Equals | x == y |
=== | Equals (exact) | x === y |
!= | Does Not Equal | x != y |
> | Greater Than | x > y |
< | Lesser Than | x < y |
>= | Greater Than or Equal To | x >= y |
<= | Lesser Than or Equal To | x <= y |
When comparing using an equals sign, remember to use two equal signs. Using only one tells the program to store/overwrite a variable instead of comparing it.
Using Comparison Operators
An if statement asks if a comparison is TRUE.
The commands in the brackets will only run if the comparison in the parentheses is true.
Example of true statements:
You might want the program to so something by default or when the comparison is false. Use an if/else statement by using the keyword "else" followed by commands in curly brackets:
The commands in the else brackets will only run for the times the comparison is false. Writing this code outside of an else statement will apply it in all cases.
There are also ways to do this while checking for a few different comparisons, using an if/else if/else statement:
Format:
Example:
There can be multiple else if statements.
Note: Each comparison goes in order, so if a comparison is true in the first if statement, it does not get checked in the else if and else statements that follow. Code outside of the if/else if/else statement will be applied to all cases.
Switch / Case Statements
Using else if statements can get tedious, especially when you are comparing something to a specific number or string.
A switch/case statement takes in the variable to compare and compares it to defined "cases," running the commands within those cases. Instead of an else statement, a default case can be defined.
Format:
Example:
Last updated