Conditional Statements
Conditional statements are a crucial aspect of programming that allow us to control the flow of
our code based on certain conditions. They help us make decisions and execute different sets of
instructions based on whether a condition is true or false. Conditional statements help us to
automate decision-making processes, respond to user input based upon inputs & conditions,
handle errors, and control the overall flow of the code. Without conditional statements, programs
would be limited to executing a fixed sequence of instructions without any flexibility.
Example:
Let’s consider a scenario where we want to create a program that determines whether a student
has passed or failed an exam based on their score. We can use a conditional statement to check if
the score is above a certain passing threshold. If it is, we display a message saying
“Congratulations! You passed.” Otherwise, we display a message saying “Sorry, you failed.
Better luck next time.”
Types of conditional statements
● If Statement
● If-Else Statement
● Else-If Statement
● Switch Statement
● Ternary Operator
If Statement
The if statement executes a block of code only if the condition is true.
, Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
let age = 18;
if (age >= 18) {
console.log("You are eligible for voting");
}
If…Else Statements
If-else statements expand on the basic if statement by providing an alternative block of code to
execute when the condition is false. This allows us to handle different outcomes based on
whether the condition evaluates to true or false. If the condition is true, the code inside the if
block is executed; otherwise, the code inside the else block is executed.
Syntax:
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
Example:
let age = 16;
Conditional statements are a crucial aspect of programming that allow us to control the flow of
our code based on certain conditions. They help us make decisions and execute different sets of
instructions based on whether a condition is true or false. Conditional statements help us to
automate decision-making processes, respond to user input based upon inputs & conditions,
handle errors, and control the overall flow of the code. Without conditional statements, programs
would be limited to executing a fixed sequence of instructions without any flexibility.
Example:
Let’s consider a scenario where we want to create a program that determines whether a student
has passed or failed an exam based on their score. We can use a conditional statement to check if
the score is above a certain passing threshold. If it is, we display a message saying
“Congratulations! You passed.” Otherwise, we display a message saying “Sorry, you failed.
Better luck next time.”
Types of conditional statements
● If Statement
● If-Else Statement
● Else-If Statement
● Switch Statement
● Ternary Operator
If Statement
The if statement executes a block of code only if the condition is true.
, Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
let age = 18;
if (age >= 18) {
console.log("You are eligible for voting");
}
If…Else Statements
If-else statements expand on the basic if statement by providing an alternative block of code to
execute when the condition is false. This allows us to handle different outcomes based on
whether the condition evaluates to true or false. If the condition is true, the code inside the if
block is executed; otherwise, the code inside the else block is executed.
Syntax:
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
Example:
let age = 16;