02. Conditional and loop statements
02. Conditional and loop statements
Conditional statements are code expressions used to tell the computer, which block of
code to execute.
In other words, conditional statements determine the flow of your computer program.
If statement
If statement are the basic foundation of conditional statements.
It takes a condition (which should result in a boolean), and a block of code that
executes when the condition’s result is true.
If statement
In this example, we have a variable called age which has a value of 21. Below that, we have
an if statement and a block of code right after it.
The condition of the if statement tells us that, the variable age should be greater than
equals to 17, for the block of code to be executed.
If statement
Now, since age has a value of 21 which is clearly greater than 17, the condition will result in
a true (boolean) and the block of code will now be executed.
So the code below will have an output of: “You can now create an ID Card”
Else statement
Now what happens if age does not meet the required condition ? What if age is less
than 17 ?
Surely, the code below will have no output. But it would be better if we were to give some
kind of message to the user that their age isn’t eligible. For this, we need else statement.
Else statement
An else statement will act as a backup plan for if statements. It does not require a
condition, it only needs a block of code to execute.
The block of code of an else statement executes when the condition of an if statement
does not meet its requirement.
Else statement
As you can see, we’ve added an else statement to the example below. This way, when age
is NOT greater than equals to 17, the else statement’s block of code will be executed. So
the output of the code below will now be “You are not old enough to create an ID card”
Else if statement
Now let’s take a look at a different case, let’s say we want to make a program to check if a
student has a passing grade. In that case, we’re gonna need to have multiple
conditions.
But currently, we can only make 2 possible outcomes. This is where else if statements
come to play.
Else if statement
An else if statement is basically an if statement combined with an else statement.
It will act as a backup plan for an if statement, however it will also need a condition
to be fulfilled.
Else if statement
Take a look at the code, grade has a value of
“B” which means it will not meet the
condition of the if statement.
Definition The if and else blocks are The switch statement has
executed depending on the multiple cases, and the code
condition in the if statement block corresponding to that
case is executed
Default Execution If the condition inside the if- If the condition inside switch
statement is false, then the statements does not match
code block under the else any of the cases, the default
condition is executed statement is executed.
For example in a boolean context, 1 is considered true which means 1 is a truthy value. 0
in a boolean context is considered false which means it is a falsy value.
Truthy and falsy values
At first glance, this seem quite simple but Falsy
Javascript can sometimes become
● "" (empty string)
confusing.
● 0
● null
Here is a list of some falsy and truthy
● undefined
values that can sometimes be confusing.
● NaN
If you're not sure whether a value is
Truthy
truthy or falsy, you can use an if
statement and input your value as the ● " " (blank character string)
condition. ● [] (empty array)
● {} (empty object)
If your value is truthy, then surely the if ● 1
statement should execute the code in the ● "1" (string)
● "0" (string)
block.
● "false" (string)
● "true" (string)
Logical Operators
Logical operators are used to determine the logic between variables or values.
Given that x = 6 and y = 3, the table below explains the logical operators:
Logical Operators Example
Ternary Operator
Short-Circuiting
Short-circuiting is a behavior exhibited by logical operators
(&&, ||) where the evaluation of the second operand is skipped
if the outcome can be determined by evaluating the first
operand alone.
The && Operator
The only difference it has is that it only starts checking the condition after the first
code block execution.
Do … While loop
In this example, the i variable already has a
value of 5.