Skip to main content

If-Else Statement

The if-else statement is a fundamental control structure in programming that allows you to execute different blocks of code based on specified conditions. It helps you make decisions in your code by evaluating whether a condition is true or false.

What is an If-Else Statement?​

An if-else statement consists of a condition followed by two blocks of code: one block that gets executed if the condition is true (if block), and another block that gets executed if the condition is false (else block). The else block is optional, and you can have multiple if-else statements nested within each other to handle more complex conditions.

Here is the general syntax of an if-else statement:

If-Else Statement Syntax
if (condition) {
// Code block to execute if the condition is true
} else {
// Code block to execute if the condition is false
}

The condition is an expression that evaluates to either true or false. If the condition is true, the code block inside the if block is executed. Otherwise, the code block inside the else block is executed.

Using If-Else Statements​

Let's look at an example to understand how if-else statements work in practice. Suppose we want to write a simple program that checks if a given number is positive or negative:

JavaScript If-Else Example

In JavaScript, you can use an if-else statement to check if a number is positive, negative, or zero:

Check if a number is positive, negative, or zero
let number = 10;

if (number > 0) {
console.log("The number is positive");
} else if (number < 0) {
console.log("The number is negative");
} else {
console.log("The number is zero");
}

In this example:

  • If the number is greater than 0, the message "The number is positive" is printed.
  • If the number is less than 0, the message "The number is negative" is printed.
  • If the number is 0, the message "The number is zero" is printed.

Nested If-Else Statements​

You can nest if-else statements within each other to handle more complex conditions. This allows you to check multiple conditions sequentially and execute different blocks of code based on the outcomes.

Here's an example of a nested if-else statement in JavaScript, Java, and Python that checks the grade of a student based on their score:

JavaScript Nested If-Else Example

Check the grade of a student
let score = 85;
let grade = "";

if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else if (score >= 60) {
grade = "D";
} else {
grade = "F";
}

console.log(`The student's grade is ${grade}`);

In this example, the student's grade is determined based on their score. The if-else statement checks the score against different thresholds to assign the appropriate grade.

Conclusion​

The if-else statement is a powerful tool in programming that allows you to make decisions based on conditions. By using if-else statements, you can control the flow of your program and execute different blocks of code based on the evaluation of conditions. Understanding how to use if-else statements effectively is essential for writing robust and flexible programs.