Skip to main content

Conditionals

Conditionals allow you to execute different blocks of code based on certain conditions. They are essential for decision-making in programming. This guide will cover the common types of conditional statements and provide detailed examples across multiple languages.

Types of Conditional Statements​

  1. If-Else: Executes a block of code if a condition is true; otherwise, it executes another block.
  2. Else-If Ladder: Used when there are multiple conditions to check.
  3. Switch Case: A more efficient way to handle multiple conditions based on the value of a single variable.
  4. Ternary Operator: A shorthand way to write simple if-else statements.

Conditional Statements in Different Languages​

JavaScript Conditionals Overview​

In JavaScript, you can use if-else, else-if ladder, switch case, and the ternary operator for conditional statements.

1. If-Else Statement​

if-else statements are used to execute a block of code based on a condition. If the condition is true, the code inside the if block is executed; otherwise, the code inside the else block is executed.

If-Else Statement in JavaScript
let number = 10;

if (number > 5) {
console.log("Number is greater than 5");
} else {
console.log("Number is 5 or less");
}

2. Else-If Ladder​

An else-if ladder is used when you have multiple conditions to check. The code block associated with the first true condition is executed.

Else-If Ladder in JavaScript
let grade = 85;

if (grade >= 90) {
console.log("A");
} else if (grade >= 80) {
console.log("B");
} else if (grade >= 70) {
console.log("C");
} else {
console.log("Fail");
}

3. Switch Case​

switch statements are used to select one of many code blocks to be executed. It is more efficient than multiple else-if statements when you have multiple conditions based on the value of a single variable.

Switch Case in JavaScript
let day = "Monday";

switch (day) {
case "Monday":
console.log("Start of the work week");
break;
case "Friday":
console.log("End of the work week");
break;
default:
console.log("Midweek days");
}

4. Ternary Operator​

The ternary operator is a shorthand way to write simple if-else statements. It consists of a condition followed by a ? and two expressions separated by :. If the condition is true, the first expression is executed; otherwise, the second expression is executed.

Ternary Operator in JavaScript
let age = 18;
let canVote = age >= 18 ? "Yes" : "No";
console.log(`Can vote: ${canVote}`); // Output: Can vote: Yes

Visualizing Conditional Flow​

To better understand the logic of if-else statements, consider the following Mermaid diagram that illustrates how a basic conditional check works:

Conclusion​

Conditional statements are fundamental to programming and allow you to control the flow of your code based on specific conditions. By mastering if-else, switch, and ternary operators, you can write more efficient and readable code across different programming languages.


Feedback and Support