Lecture Notes Of Day 5
Conditional Statements in JavaScript
Objective:
The objective of today’s lesson
is to understand how to use conditional statements in JavaScript to control the
flow of a program. By the end of the session, students should be able to write
programs that make decisions based on conditions using if, else if, else, and switch
statements.
What Are
Conditional Statements?
Conditional statements in
JavaScript allow a program to execute different code depending on whether a
condition is true or false. These statements control the flow of the program by
allowing it to make decisions.
Types of
Conditional Statements:
1. if
statement
2. else if
statement
3. else
statement
4. switch
statement
Let’s dive into each of these.
1. The if
Statement
The if statement is used to test
a condition. If the condition is true, the code inside the if block will
execute. If the condition is false, the program will skip over the block.
Syntax:
if (condition) {
// code to run if condition is true
}
Example:
let age =
18;
if (age >= 18) {
console.log("You are an adult.");
}
- Explanation:In this example, we are checking if the age is greater than or equal to 18. Since 18 is equal to 18, the condition evaluates to true, and the message "You are an adult." will be printed to the console.
2. The else
Statement
The else statement is used in
conjunction with the if statement. It specifies the code to run if the if
condition is false.
Syntax:
if (condition) {
// code to run if condition is true
} else {
// code to run if condition is false
}
Example:
let age = 16;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
- Explanation:Here, we check if age is greater than or equal to 18. Since the age is 16, which is less than 18, the condition evaluates to false, and the program will execute the code inside the else block, printing "You are a minor."
3. The else
if Statement
The else if statement allows us
to check multiple conditions. It’s used when there are multiple possible
outcomes, and you want to test more than one condition.
Syntax:
if
(condition1) {
// code to run if condition1 is true
} else if
(condition2) {
// code to run if condition2 is true
} else {
// code to run if none of the above
conditions are true
}
Example:
let age = 20;
if (age < 13) {
console.log("You are a child.");
} else if
(age >= 13 && age < 18) {
console.log("You are a teenager.");
} else {
console.log("You are an adult.");
}
- Explanation:In this case, we check the age against multiple conditions:
- If
the age is less than 13, it prints "You are a child."
- If
the age is between 13 and 17 (inclusive), it prints "You are a
teenager."
- Otherwise,
it prints "You are an adult."
Since the age is 20, the
condition for the else block will be true, and "You are an adult."
will be printed.
4. The switch
Statement
The switch statement is another
way to test conditions. It's used when you have multiple possible conditions
based on the same variable or expression. It checks the value of a variable or
expression against multiple case values and executes the block of code
corresponding to the matching case.
Syntax:
switch
(expression) {
case value1:
// code to run if expression === value1
break;
case value2:
// code to run if expression === value2
break;
default:
// code to run if none of the cases
match
}
- expression:
The variable or value to be checked.
- case:
The possible values for the expression.
- break:
Stops further checking once a match is found.
- default:
The block of code to run if no case matches.
Example:
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Thursday");
break;
case 5:
console.log("Friday");
break;
default:
console.log("Weekend");
}
- Explanation:In this example, the switch statement checks the value of day. Since the value of day is 3, the code inside case 3 will execute, printing "Wednesday". If the value of day was not between 1 and 5, it would execute the default block, printing "Weekend".
Important
Notes:
- break in switch:Without the break statement, the program will continue to execute the following cases even if a match is found, which is called "fall-through."
For example:
let x = 2;
switch
(x) {
case 1:
console.log("One");
case 2:
console.log("Two");
case 3:
console.log("Three");
default:
console.log("Default");
}
This will output:
Two
Three
Default
- else
if vs switch:
- Use
if and else if when the conditions are more complex or based on ranges
(like checking if a number is within a certain range).
- Use
switch when you have multiple possible values for a single expression.
Practice
Exercises:
1. Write a
program that checks if a number is positive, negative, or zero.
2. Write a
program that checks if a user is eligible to vote based on their age.
3. Use the switch
statement to print the corresponding month name for a given number (e.g., 1 for
January, 2 for February, etc.).
4. Create a
program that uses else if to categorize a student’s grade (A, B, C, D, or F)
based on their score.
Conclusion:
Conditional statements are a fundamental part of programming that allows you to make decisions within your code. Understanding how to use if, else, else if, and switch will help you write dynamic and interactive programs.
