Skip to content

Latest commit

 

History

History
204 lines (195 loc) · 3.87 KB

10. switch statement.md

File metadata and controls

204 lines (195 loc) · 3.87 KB

10. switch Statement

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

Programming 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

Programiz 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