Conditional statements

Conditional statements are programming constructs that allow programmers to make decisions and execute different blocks of code based on a certain condition. There are three main types of conditional statements: if-else statements, switch statements, and ternary operators.

  1. If-else statements: An if-else statement is used to execute a block of code if a certain condition is true, and a different block of code if the condition is false. The general syntax of an if-else statement is as follows:

if (condition) {
    // block of code to execute if the condition is true
} else {
    // block of code to execute if the condition is false
}
  1. Switch statements: A switch statement is used to execute different blocks of code based on the value of a variable or expression. The general syntax of a switch statement is as follows:

switch (expression) {
    case value1:
        // block of code to execute if expression is equal to value1
        break;
    case value2:
        // block of code to execute if expression is equal to value2
        break;
    default:
        // block of code to execute if none of the above cases are true
        break;
}
  1. Ternary operators: A ternary operator is a shorthand way of writing an if-else statement that evaluates a single expression and returns one of two possible values depending on whether the condition is true or false. The general syntax of a ternary operator is as follows:

condition ? value1 : value2

If the condition is true, the operator returns value1; otherwise, it returns value2.

Conditional statements are essential in programming because they allow programs to make decisions and take different actions based on different inputs or conditions.

Here are some examples of conditional statements in JavaScript:

  1. If statement:

The if statement is used to execute a block of code only if a specific condition is true.

Example:

let age = 18;
if(age >= 18) {
  console.log("You are eligible to vote");
}

In this example, if the age variable is greater than or equal to 18, the message "You are eligible to vote" will be displayed in the console.

  1. If-else statement:

The if-else statement is used to execute a block of code if a specific condition is true and another block of code if the condition is false.

Example:

let num = 5;
if(num % 2 === 0) {
  console.log(num + " is an even number");
} else {
  console.log(num + " is an odd number");
}

In this example, if the num variable is divisible by 2, the message "5 is an odd number" will be displayed in the console.

  1. If-else-if statement:

The if-else-if statement is used to execute different blocks of code for different conditions.

Example:

let grade = 80;
if(grade >= 90) {
  console.log("You got an A");
} else if(grade >= 80) {
  console.log("You got a B");
} else if(grade >= 70) {
  console.log("You got a C");
} else {
  console.log("You failed");
}

In this example, if the grade variable is greater than or equal to 90, the message "You got an A" will be displayed in the console. If the grade is between 80 and 89, the message "You got a B" will be displayed, and so on.

Switch Statement Example:

let day = "Monday";

switch(day) {
  case "Monday":
    console.log("Today is Monday");
    break;
  case "Tuesday":
    console.log("Today is Tuesday");
    break;
  case "Wednesday":
    console.log("Today is Wednesday");
    break;
  default:
    console.log("Today is some other day");
}

In this example, we have a switch statement that checks the value of the variable day. Depending on the value of day, it will execute the corresponding code block. If day is "Monday", it will print "Today is Monday" to the console. If day is "Tuesday", it will print "Today is Tuesday", and so on. If day doesn't match any of the cases, it will execute the default case and print "Today is some other day".

Ternary Operator Example:

let age = 20;

let message = (age >= 18) ? "You are an adult" : "You are not an adult";

console.log(message);

In this example, we have a ternary operator that checks if the variable age is greater than or equal to 18. If it is, it will set the value of the variable message to "You are an adult". If it's not, it will set the value of message to "You are not an adult". The ternary operator is a shorthand way of writing an if-else statement.

These are just a few examples of conditional statements in JavaScript. There are many other types of conditional statements, which can be used to create more complex logic in your code.

Last updated