By the end of this lesson, you will be able to:
:information_source: Info What is Control Flow? Control flow is the order in which your computer reads and runs each line of code. It's like following a recipe - sometimes you do every step, and sometimes you skip steps based on what ingredients you have!
Similar to Scratch or MIT AI2, we can use if statements to create a conditional statement.
The most simplest and common syntax of the if statement looks like this:
if (condition) \{
// statements to execute if true
\}
\{ \}
will be executed.\{ \}
will be skipped.var age = 16;
if (age > 12) \{
console.log("Greater than 12!");
\}
console.log("Just a number.");
Output:
Greater than 12!
Just a number.
What happens:
(age > 12)
inside the pair of round brackets is the condition to test.console.log()
.var age = 11;
if (age > 12) \{
console.log("Greater than 12!");
\}
console.log("Just a number.");
Output:
Just a number.
What happens:
(age > 12)
is false because 11 is not greater than 12.console.log()
that was written after the if statement ends.We can also run a block of code when the condition is evaluated to false by using the if...else statement.
Just add else with a pair of curly brackets at the end of the first closing bracket like this:
if (condition) \{
// statements to execute when condition is true
\} else \{
// statements to execute when condition is false
\}
var age = 8;
if (age > 12) \{
console.log("Greater than 12!");
\} else \{
var diff = 12 - age;
console.log(diff + " more years until 12.");
\}
console.log("Just a number.");
Output:
4 more years until 12.
Just a number.
What happens:
(age > 12)
is false because 8 is not greater than 12.console.log("Just a number");
will run as usual because it is outside of the if and else block.Continuing from the previous example, suppose we want to check if the age is less than 5 and when the age is not greater than 12, we can use what we called a nested if...else statement. For example:
var age = 8; // :(
if (age > 12) \{
console.log("Greater than 12!");
\} else \{
if (age < 5) \{
console.log("Hey kid!");
\} else \{
console.log("Between 5 and 12");
\}
\}
console.log("Just a number.");
However, as you can see the code gets harder to read and complicated writing this way! It is not wrong, just not the best way to code your logic.
This is actually a pretty common code. So for this reason, JS provides a simpler way to do the same thing, just like nested if...else statements but better.
Introducing the else if statement:
if (condition1) \{
// Run only this if condition1 is true
\} else if (condition2) \{
// Run only this if condition2 is true
\} else \{
// Run this if all conditions are false
\}
Conditions are checked line-by-line from the top:
You can have as many else if statements as you like to replace the if...else if...else statements.
Thus, we can now simplify the code to this:
var age = 8; // :)
if (age > 12) \{
console.log("Greater than 12!");
\} else if (age < 5) \{
console.log("Hey kid!");
\} else \{
console.log("Between 5 and 12");
\}
console.log("Just a number.");
Output:
Between 5 and 12
Just a number.
This is much cleaner and easier to read!