Skip to content

Latest commit

 

History

History
252 lines (230 loc) · 6.23 KB

File metadata and controls

252 lines (230 loc) · 6.23 KB

10. switch Statement

In JavaScript, the switch statement is used for making decisions based on the value of an expression. It provides a way to compare a single value against multiple possible case values and execute code blocks based on which case value matches the value of the expression. Here's the basic syntax of the switch statement:

switch (expression) {
    case value1:
        // Code to execute if expression matches value1
        break;
    case value2:
        // Code to execute if expression matches value2
        break;
    // Additional cases can be added as needed.
    default:
        // Code to execute if none of the cases match the expression
}

Here's a breakdown of how it works:

  1. The switch keyword is followed by an expression enclosed in parentheses. This expression is the value that you want to compare against the different case values.

  2. Inside the switch block, you define one or more case blocks. Each case block specifies a value that you want to compare with the expression. If the expression matches a case value, the code inside that case block will be executed.

  3. After each case block, you typically include a break statement. This break statement is used to exit the switch statement after executing the code in the matching case. Without a break statement, the switch statement would continue to execute the code in subsequent case blocks.

  4. You can also include a default block at the end of the switch statement. The default block is executed if none of the case values match the expression.

Here's an example:

let day = "Monday";

switch (day) {
    case "Monday":
        console.log("It's the start of the workweek.");
        break;
    case "Friday":
        console.log("It's almost the weekend.");
        break;
    default:
        console.log("It's a regular day.");
}

In this example, the switch statement compares the value of the day variable against different cases and prints a message based on the value of day. Since day is "Monday" in this case, it will print "It's the start of the workweek."

The switch statement is useful when you have multiple conditions to check against a single expression, and it can provide a cleaner and more efficient way to handle these situations compared to using a series of if/else if statements.

Example

const number = parseInt(prompt('Enter a number between 1 to 7: '));

switch (number) {
    case 1:
        console.log("Sunday");
        break;
    case 2:
        console.log("Monday");
        break;
    case 3:
        console.log("Tuesday");
        break;
    case 4:
        console.log("Wednesday");
        break;
    case 5:
        console.log("Thursday");
        break;
    case 6:
        console.log("Friday");
        break;
    case 7:
        console.log("Saturday");
        break;
    default:
        console.log("Invalid Number");
}

Output

Enter a number between 1 to 7: 4
Wednesday

Omitting break

const number = parseInt(prompt('Enter a number between 1 to 7: '));

switch (number) {
    case 1:
        console.log("Sunday");
    case 2:
        console.log("Monday");
    case 3:
        console.log("Tuesday");
    case 4:
        console.log("Wednesday");
    case 5:
        console.log("Thursday");
    case 6:
        console.log("Friday");
    case 7:
        console.log("Saturday");
    default:
        console.log("Invalid Number");
}

Output

Wednesday
Thursday
Friday
Saturday
Invalid Number

JavaScript switch With Multiple Case

const day = prompt('Enter the day: ');

switch (day) {
    case 'Monday':
    case 'Tuesday':
    case 'Wednesday':
    case 'Thursday':;
    case 'Friday':
        console.log("Weekday");
        break;
    case 'Saturday':
    case 'Sunday':
        console.log("Weekend");
        break;
    default:
        console.log("Invalid Day");
}

Output

Weekday

Simple Calculator

// take the operator input
const operator = prompt('Enter operator ( either +, -, * or / ): ');

// take the operand input
const number1 = parseFloat(prompt('Enter first number: '));
const number2 = parseFloat(prompt('Enter second number: '));

let result;
switch(operator) {
    case '+':
        result = number1 + number2;
        break;
    case '-':
        result = number1 - number2;
        break;
    case '*':
        result = number1 * number2;
        break;
    case '/':
        result = number1 / number2;
        break;

    default:
        console.log('Invalid operator');
        break;
}
console.log(`${number1} ${operator} ${number2} = ${result}`);

Output

Enter operator: *
Enter first number: 5
Enter second number: 8.8
5 * 8.8 = 44

Assignment and Task

Can you use switch statements to create a program that takes the number input from the user from 1 to 12. And, print the corresponding month based on the input value.

Solution:

const number = parseInt(prompt('Enter a number from 1 to 12: '));

let result;
switch(number) {
    case 1:
        result = "January";
        break;
    case 2:
        result = "Febraury";
        break;
    case 3:
        result = "March";
        break;
    case 4:
        result = "April";
        break;
    case 5:
        result = "May";
        break;
    case 6:
        result = "June";
        break;
    case 7:
        result = "July";
        break;
    case 8:
        result = "August";
        break;
    case 9:
        result = "September";
        break;
    case 10:
        result = "October";
        break;
    case 11:
        result = "November";
        break;
    case 12:
        result = "December";
        break;
    default:
        console.log('Invalid number');
        break;
}
console.log(result);

Output

Enter a number from 1 to 12: 3
March

p4n Quiz

Q. Which of the cases is executed in the following code?

let value = 4;

switch(value) {
  case 1:
  case 2:
  default:
}
  1. case 1
  2. case 2
  3. case 4
  4. default

Answer: 4