|
| 1 | +# 10. switch Statement |
| 2 | + |
| 3 | +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: |
| 4 | + |
| 5 | +```javascript |
| 6 | +switch (expression) { |
| 7 | + case value1: |
| 8 | + // Code to execute if expression matches value1 |
| 9 | + break; |
| 10 | + case value2: |
| 11 | + // Code to execute if expression matches value2 |
| 12 | + break; |
| 13 | + // Additional cases can be added as needed. |
| 14 | + default: |
| 15 | + // Code to execute if none of the cases match the expression |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +Here's a breakdown of how it works: |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +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. |
| 24 | + |
| 25 | +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. |
| 26 | + |
| 27 | +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. |
| 28 | + |
| 29 | +Here's an example: |
| 30 | + |
| 31 | +```javascript |
| 32 | +let day = "Monday"; |
| 33 | + |
| 34 | +switch (day) { |
| 35 | + case "Monday": |
| 36 | + console.log("It's the start of the workweek."); |
| 37 | + break; |
| 38 | + case "Friday": |
| 39 | + console.log("It's almost the weekend."); |
| 40 | + break; |
| 41 | + default: |
| 42 | + console.log("It's a regular day."); |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +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." |
| 47 | + |
| 48 | +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. |
| 49 | + |
| 50 | +## Example |
| 51 | +```js |
| 52 | +const number = parseInt(prompt('Enter a number between 1 to 7: ')); |
| 53 | + |
| 54 | +switch (number) { |
| 55 | + case 1: |
| 56 | + console.log("Sunday"); |
| 57 | + break; |
| 58 | + case 2: |
| 59 | + console.log("Monday"); |
| 60 | + break; |
| 61 | + case 3: |
| 62 | + console.log("Tuesday"); |
| 63 | + break; |
| 64 | + case 4: |
| 65 | + console.log("Wednesday"); |
| 66 | + break; |
| 67 | + case 5: |
| 68 | + console.log("Thursday"); |
| 69 | + break; |
| 70 | + case 6: |
| 71 | + console.log("Friday"); |
| 72 | + break; |
| 73 | + case 7: |
| 74 | + console.log("Saturday"); |
| 75 | + break; |
| 76 | + default: |
| 77 | + console.log("Invalid Number"); |
| 78 | +} |
| 79 | +``` |
| 80 | +**Output** |
| 81 | +``` |
| 82 | +Enter a number between 1 to 7: 4 |
| 83 | +Wednesday |
| 84 | +``` |
| 85 | +*** |
| 86 | +## Omitting break |
| 87 | +```js |
| 88 | +const number = parseInt(prompt('Enter a number between 1 to 7: ')); |
| 89 | + |
| 90 | +switch (number) { |
| 91 | + case 1: |
| 92 | + console.log("Sunday"); |
| 93 | + case 2: |
| 94 | + console.log("Monday"); |
| 95 | + case 3: |
| 96 | + console.log("Tuesday"); |
| 97 | + case 4: |
| 98 | + console.log("Wednesday"); |
| 99 | + case 5: |
| 100 | + console.log("Thursday"); |
| 101 | + case 6: |
| 102 | + console.log("Friday"); |
| 103 | + case 7: |
| 104 | + console.log("Saturday"); |
| 105 | + default: |
| 106 | + console.log("Invalid Number"); |
| 107 | +} |
| 108 | +``` |
| 109 | +**Output** |
| 110 | +``` |
| 111 | +Wednesday |
| 112 | +Thursday |
| 113 | +Friday |
| 114 | +Saturday |
| 115 | +Invalid Number |
| 116 | +``` |
| 117 | +*** |
| 118 | +## JavaScript switch With Multiple Case |
| 119 | +```js |
| 120 | +const day = prompt('Enter the day: '); |
| 121 | + |
| 122 | +switch (day) { |
| 123 | + case 'Monday': |
| 124 | + case 'Tuesday': |
| 125 | + case 'Wednesday': |
| 126 | + case 'Thursday':; |
| 127 | + case 'Friday': |
| 128 | + console.log("Weekday"); |
| 129 | + break; |
| 130 | + case 'Saturday': |
| 131 | + case 'Sunday': |
| 132 | + console.log("Weekend"); |
| 133 | + break; |
| 134 | + default: |
| 135 | + console.log("Invalid Day"); |
| 136 | +} |
| 137 | +``` |
| 138 | +**Output** |
| 139 | +``` |
| 140 | +Weekday |
| 141 | +``` |
| 142 | +## Simple Calculator |
| 143 | +```js |
| 144 | +// take the operator input |
| 145 | +const operator = prompt('Enter operator ( either +, -, * or / ): '); |
| 146 | + |
| 147 | +// take the operand input |
| 148 | +const number1 = parseFloat(prompt('Enter first number: ')); |
| 149 | +const number2 = parseFloat(prompt('Enter second number: ')); |
| 150 | + |
| 151 | +let result; |
| 152 | +switch(operator) { |
| 153 | + case '+': |
| 154 | + result = number1 + number2; |
| 155 | + break; |
| 156 | + case '-': |
| 157 | + result = number1 - number2; |
| 158 | + break; |
| 159 | + case '*': |
| 160 | + result = number1 * number2; |
| 161 | + break; |
| 162 | + case '/': |
| 163 | + result = number1 / number2; |
| 164 | + break; |
| 165 | + |
| 166 | + default: |
| 167 | + console.log('Invalid operator'); |
| 168 | + break; |
| 169 | +} |
| 170 | +console.log(`${number1} ${operator} ${number2} = ${result}`); |
| 171 | +``` |
| 172 | +**Output** |
| 173 | +``` |
| 174 | +Enter operator: * |
| 175 | +Enter first number: 5 |
| 176 | +Enter second number: 8.8 |
| 177 | +5 * 8.8 = 44 |
| 178 | +``` |
| 179 | +*** |
| 180 | +## Assignment and Task |
| 181 | +**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.** |
| 182 | +### Solution: |
| 183 | +```js |
| 184 | +const number = parseInt(prompt('Enter a number from 1 to 12: ')); |
| 185 | + |
| 186 | +let result; |
| 187 | +switch(number) { |
| 188 | + case 1: |
| 189 | + result = "January"; |
| 190 | + break; |
| 191 | + case 2: |
| 192 | + result = "Febraury"; |
| 193 | + break; |
| 194 | + case 3: |
| 195 | + result = "March"; |
| 196 | + break; |
| 197 | + case 4: |
| 198 | + result = "April"; |
| 199 | + break; |
| 200 | + case 5: |
| 201 | + result = "May"; |
| 202 | + break; |
| 203 | + case 6: |
| 204 | + result = "June"; |
| 205 | + break; |
| 206 | + case 7: |
| 207 | + result = "July"; |
| 208 | + break; |
| 209 | + case 8: |
| 210 | + result = "August"; |
| 211 | + break; |
| 212 | + case 9: |
| 213 | + result = "September"; |
| 214 | + break; |
| 215 | + case 10: |
| 216 | + result = "October"; |
| 217 | + break; |
| 218 | + case 11: |
| 219 | + result = "November"; |
| 220 | + break; |
| 221 | + case 12: |
| 222 | + result = "December"; |
| 223 | + break; |
| 224 | + default: |
| 225 | + console.log('Invalid number'); |
| 226 | + break; |
| 227 | +} |
| 228 | +console.log(result); |
| 229 | +``` |
| 230 | +**Output** |
| 231 | +``` |
| 232 | +Enter a number from 1 to 12: 3 |
| 233 | +March |
| 234 | +``` |
| 235 | +*** |
| 236 | +## p4n Quiz |
| 237 | +**Q. Which of the cases is executed in the following code?** |
| 238 | +```js |
| 239 | +let value = 4; |
| 240 | + |
| 241 | +switch(value) { |
| 242 | + case 1: |
| 243 | + case 2: |
| 244 | + default: |
| 245 | +} |
| 246 | +``` |
| 247 | +1. case 1 |
| 248 | +2. case 2 |
| 249 | +3. case 4 |
| 250 | +4. default |
| 251 | + |
| 252 | +Answer: 4 |
0 commit comments