|
| 1 | +// Controlflow |
| 2 | + |
| 3 | +// Conditional Statements |
| 4 | +// In JavaScript we have the following conditional statements: |
| 5 | +// Very often when you write code, you want to perform different actions for different decisions. |
| 6 | +// You can use conditional statements in your code to do this. |
| 7 | + |
| 8 | + |
| 9 | +// Use if to specify a block of code to be executed, if a specified condition is true |
| 10 | +// Use else to specify a block of code to be executed, if the same condition is false |
| 11 | +// Use else if to specify a new condition to test, if the first condition is false |
| 12 | +// Use switch to specify many alternative blocks of code to be executed |
| 13 | + |
| 14 | + |
| 15 | +// The if Statement |
| 16 | +// Use the if statement to specify a block of JavaScript code to be executed if a condition is true. |
| 17 | + |
| 18 | +// Syntax |
| 19 | +// if (condition) { |
| 20 | +// // block of code to be executed if the condition is true |
| 21 | +// } |
| 22 | + |
| 23 | +let hour = 2; |
| 24 | +if (hour < 18) { |
| 25 | + var greeting = "Good day"; |
| 26 | +} |
| 27 | + |
| 28 | +console.log(greeting); |
| 29 | + |
| 30 | +// The else Statement |
| 31 | +// Use the else statement to specify a block of code to be executed if the condition is false. |
| 32 | + |
| 33 | +// Syntax |
| 34 | +// if (condition) { |
| 35 | +// // block of code to be executed if the condition is true |
| 36 | +// } else { |
| 37 | +// // block of code to be executed if the condition is false |
| 38 | +// } |
| 39 | +hour = 20; |
| 40 | +if (hour < 18) { |
| 41 | + greeting = "Good day"; |
| 42 | +} else { |
| 43 | + greeting = "Good evening"; |
| 44 | +} |
| 45 | + |
| 46 | +console.log(greeting); |
| 47 | + |
| 48 | + |
| 49 | +// The else if Statement |
| 50 | +// Use the else if statement to specify a new condition if the first condition is false. |
| 51 | + |
| 52 | +// Syntax |
| 53 | +// if (condition1) { |
| 54 | +// // block of code to be executed if condition1 is true |
| 55 | +// } else if (condition2) { |
| 56 | +// // block of code to be executed if the condition1 is false and condition2 is true |
| 57 | +// } else { |
| 58 | +// // block of code to be executed if the condition1 is false and condition2 is false |
| 59 | +// } |
| 60 | +let time = 9; |
| 61 | +if (time < 10) { |
| 62 | + greeting = "Good morning"; |
| 63 | +} else if (time < 20) { |
| 64 | + greeting = "Good day"; |
| 65 | +} else { |
| 66 | + greeting = "Good evening"; |
| 67 | +} |
| 68 | + |
| 69 | +console.log(greeting); |
| 70 | + |
| 71 | + |
| 72 | +// if there is oneline in any conditional blokc we can skip {} |
| 73 | + |
| 74 | +if (time < 10) greeting = "Good morning"; |
| 75 | +else if (time < 20) greeting = "Good day"; |
| 76 | +else greeting = "Good evening"; |
| 77 | + |
| 78 | + |
| 79 | +console.log(greeting); |
| 80 | + |
| 81 | +//If there is more than one line in any conditional blokc we must be write code with in {} |
| 82 | + |
| 83 | +if (time < 10) { |
| 84 | + greeting = "Good morning"; |
| 85 | + console.log("Have a great day"); |
| 86 | +} else if (time < 20) { |
| 87 | + greeting = "Good day"; |
| 88 | + console.log("Have a great day"); |
| 89 | +} else { |
| 90 | + greeting = "Good evening"; |
| 91 | + console.log("Have a great day"); |
| 92 | +} |
| 93 | + |
| 94 | +console.log(greeting); |
| 95 | + |
| 96 | + |
| 97 | +// Excercise: |
| 98 | +// Find largest number form 3 |
| 99 | +let num1 = parseInt(prompt("Enter Number 1: ")); |
| 100 | +let num2 = parseInt(prompt("Enter Number 2: ")); |
| 101 | +let num3 = parseInt(prompt("Enter Number 3: ")); |
| 102 | + |
| 103 | +if (num1 >= num2 && num1 >= num3) { |
| 104 | + console.log(`${num1} is grater than ${num2}, ${num3}`); |
| 105 | +} else if (num2 >= num3) { |
| 106 | + console.log(`${num2} is grater than ${num1}, ${num3}`); |
| 107 | +} else { |
| 108 | + console.log(`${num3} is grater than ${num1}, ${num2}`); |
| 109 | +} |
| 110 | + |
| 111 | +//Nested if |
| 112 | +if (num1 >= num2) { |
| 113 | + if (num1 >= num3) { |
| 114 | + console.log(`${num1} is grater than ${num2}, ${num3}`); |
| 115 | + } else { |
| 116 | + console.log(`${num3} is grater than ${num1}, ${num2}`); |
| 117 | + } |
| 118 | +} else if (num2 >= num3) { |
| 119 | + console.log(`${num2} is grater than ${num1}, ${num3}`); |
| 120 | +} else { |
| 121 | + console.log(`${num3} is grater than ${num1}, ${num2}`); |
| 122 | +} |
| 123 | + |
| 124 | +// The JavaScript Switch Statement |
| 125 | +// Use the switch statement to select one of many code blocks to be executed. |
| 126 | + |
| 127 | +// Syntax |
| 128 | +// switch(expression) { |
| 129 | +// case x: |
| 130 | +// // code block |
| 131 | +// break; |
| 132 | +// case y: |
| 133 | +// // code block |
| 134 | +// break; |
| 135 | +// default: |
| 136 | +// // code block |
| 137 | +// } |
| 138 | + |
| 139 | +// This is how it works: |
| 140 | +// The switch expression is evaluated once. |
| 141 | +// The value of the expression is compared with the values of each case. |
| 142 | +// If there is a match, the associated block of code is executed. |
| 143 | +// If there is no match, the default code block is executed. |
| 144 | + |
| 145 | +let currentMonth; |
| 146 | +switch(new Date().getMonth()){ |
| 147 | + case 0: |
| 148 | + currentMonth = "January"; |
| 149 | + break; |
| 150 | + case 1: |
| 151 | + currentMonth = "February"; |
| 152 | + break; |
| 153 | + case 2: |
| 154 | + currentMonth = "March"; |
| 155 | + break; |
| 156 | + case 3: |
| 157 | + currentMonth = "April"; |
| 158 | + break; |
| 159 | + case 4: |
| 160 | + currentMonth = "May"; |
| 161 | + break; |
| 162 | + case 5: |
| 163 | + currentMonth = "June"; |
| 164 | + break; |
| 165 | + case 6: |
| 166 | + currentMonth = "July"; |
| 167 | + break; |
| 168 | + case 7: |
| 169 | + currentMonth = "August"; |
| 170 | + break; |
| 171 | + case 8: |
| 172 | + currentMonth = "September"; |
| 173 | + break; |
| 174 | + case 9: |
| 175 | + currentMonth = "Octobor"; |
| 176 | + break; |
| 177 | + case 10: |
| 178 | + currentMonth = "November"; |
| 179 | + break; |
| 180 | + case 11: |
| 181 | + currentMonth = "December"; |
| 182 | + break; |
| 183 | + default: |
| 184 | + currentMonth = "Something is wrong"; |
| 185 | + break; |
| 186 | + |
| 187 | +} |
| 188 | + |
| 189 | +console.log(currentMonth); |
| 190 | + |
| 191 | +// excercise: |
| 192 | +let score = parseFloat(prompt("Enter your score: ")); |
| 193 | + |
| 194 | + |
| 195 | +if(score < 0 || 100 < score){ |
| 196 | + console.log("Invalid score Input."); |
| 197 | +}else if(score < 40){ |
| 198 | + console.log("Your Grade is F."); |
| 199 | +}else if(score < 45){ |
| 200 | + console.log("Your Grade is D"); |
| 201 | +}else if(score < 50){ |
| 202 | + console.log("Your Grade is C"); |
| 203 | +}else if(score < 55){ |
| 204 | + console.log("Your Grade is C+"); |
| 205 | +}else if(score < 60){ |
| 206 | + console.log("Your Grade is B-"); |
| 207 | +}else if(score < 65){ |
| 208 | + console.log("Your Grade is B"); |
| 209 | +}else if(score < 70){ |
| 210 | + console.log("Your Grade is B+"); |
| 211 | +}else if(score < 75){ |
| 212 | + console.log("Your Grade is A-"); |
| 213 | +}else if(score < 80){ |
| 214 | + console.log("Your Grade is A"); |
| 215 | +}else{ |
| 216 | + console.log("Your Grade is A+"); |
| 217 | +} |
| 218 | + |
| 219 | + |
| 220 | +// excercise |
| 221 | + |
| 222 | +num1 = parseFloat(prompt("Plese Enter Number 1: ")); |
| 223 | +num2 = parseFloat(prompt("Plese Enter Number 2: ")); |
| 224 | + |
| 225 | +let option = parseInt(prompt(`Enter the Option: |
| 226 | + 1. Add |
| 227 | + 2. Subtract |
| 228 | + 3. Multiply |
| 229 | + 4. Division`)); |
| 230 | + |
| 231 | + |
| 232 | +let res = null; |
| 233 | +if(!isNaN(num1) && !isNaN(num2) && !isNaN(option)){ |
| 234 | + switch(option){ |
| 235 | + case 1: |
| 236 | + res = num1 + num2; |
| 237 | + break; |
| 238 | + case 2: |
| 239 | + res = num1 - num2; |
| 240 | + break; |
| 241 | + case 3: |
| 242 | + res = num1 * num2; |
| 243 | + break; |
| 244 | + case 4: |
| 245 | + res = num1 / num2; |
| 246 | + break; |
| 247 | + default: |
| 248 | + res = "Invalid Option"; |
| 249 | + break; |
| 250 | + } |
| 251 | +}else{ |
| 252 | + console.log("Invalid Input"); |
| 253 | +} |
| 254 | + |
| 255 | +if(res == null) console.log("No Result") |
| 256 | +else console.log("Result is: ", res); |
| 257 | + |
| 258 | + |
| 259 | + |
| 260 | + |
0 commit comments