|
1 | | -# intro-to-flow-control.js |
| 1 | +# JavaScript Flow Control |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +* About |
| 6 | +* If Statements |
| 7 | +* If/Else Statements |
| 8 | +* If/Else if/Else Statements |
| 9 | +* Ternary Operators |
| 10 | +* Switch Statments |
| 11 | +* Resources |
| 12 | + |
| 13 | +## About |
| 14 | + |
| 15 | +Flow Control allows the excution of code only under certain conditions. In Ruby, we used case statments, if statments, if/else statements, if/elsif/else statements, ternary operators, and case statements to control what code runs when. JavaScript has similar methods to control what blocks of code to execute: if statements, if/else statements, if/else if/else statements, ternary operators, and switch statements. |
| 16 | + |
| 17 | +## If Statements |
| 18 | + |
| 19 | +Evaluates the code wrapped in parenthesis to either true or false. If `true`, the code block is executed: |
| 20 | + |
| 21 | +Syntax: |
| 22 | + |
| 23 | +```javascript |
| 24 | +if (conditionToTestIsTrue) { |
| 25 | + // code to be executed here |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Example: |
| 30 | + |
| 31 | +```javascript |
| 32 | +var num = 7; |
| 33 | + |
| 34 | +if (num < 10) { |
| 35 | + console.log(num + " is less than ten"); |
| 36 | +} |
| 37 | + |
| 38 | +// This will print "7 is less than ten" to the console |
| 39 | +``` |
| 40 | + |
| 41 | +## If/Else Statements |
| 42 | + |
| 43 | +You will often see an `If` statement used in combination with an `else` clause. An `else` clause is a fallback to an `if` statement and will only get executed if the previous `if` statement is false. |
| 44 | + |
| 45 | +Syntax: |
| 46 | + |
| 47 | +```javascript |
| 48 | +if (conditionToTestIsTrue) { |
| 49 | + // codition is false hence code is not executed |
| 50 | +} else { |
| 51 | + // code to be executed because previous `if` condition is false |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Example: |
| 56 | + |
| 57 | +```javascript |
| 58 | +var num = 11; |
| 59 | + |
| 60 | +if (num < 10) { |
| 61 | + console.log(num + " is less than ten"); |
| 62 | +} else { |
| 63 | + console.log(num + " is more than or equal to ten"); |
| 64 | +} |
| 65 | + |
| 66 | +// This will print "11 is more than or equal to ten" to the console |
| 67 | +``` |
| 68 | + |
| 69 | +## If/Else if/Else Statements |
| 70 | + |
| 71 | +`if` statements can also be combined with an `else if` clause. This is like an else statement, but with its own condition. It will only run if its condition is true, and the previous statement's condition was false. |
| 72 | + |
| 73 | +Note: An important thing to keep in mind when transitioning to JavaScript from Ruby is that the `elsif` becomes two full words: `else if`. |
| 74 | + |
| 75 | +Syntax: |
| 76 | + |
| 77 | +```javascript |
| 78 | +if (conditionToTestIsTrue){ |
| 79 | + // codition is false hence code is not executed |
| 80 | +} else if (thisCondionIsTrue) { |
| 81 | + // excute this code if previous `if` statement is false |
| 82 | +} else { |
| 83 | + // excute this code if the 2 conditions above are false |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +Example: |
| 88 | + |
| 89 | +```javascript |
| 90 | +var num = 10; |
| 91 | + |
| 92 | +if (num < 10) { |
| 93 | + console.log(num + " is less than ten"); |
| 94 | +} else if (num == 10) { |
| 95 | + console.log(num + " is equal to ten"); |
| 96 | +} else { |
| 97 | + console.log(num + " is more than ten"); |
| 98 | +} |
| 99 | + |
| 100 | +// This will print "10 is equal to ten" to the console |
| 101 | +``` |
| 102 | + |
| 103 | +## Ternary Operator |
| 104 | + |
| 105 | +The ternary operator is used as a shortcut for the `if-else` statement. You've probably seen it before in Ruby looking something like this: |
| 106 | + |
| 107 | +```ruby |
| 108 | +cart = ["graphic t-shirt", "aluminum water bottle"] |
| 109 | + |
| 110 | +puts cart.empty? ? "Please add something to your cart." : "You're ready to check out." |
| 111 | + |
| 112 | +# Above prints: |
| 113 | +# You're ready to check out. |
| 114 | +``` |
| 115 | + |
| 116 | +This operator tests a condition; if the condition is true, it returns a certain value, otherwise it returns a different value: |
| 117 | + |
| 118 | +Syntax: |
| 119 | + |
| 120 | +```javascript |
| 121 | +conditionToTest ? valueToBeReturnedIfTrue : valueToBeReturnedIfFalse |
| 122 | +``` |
| 123 | + |
| 124 | +Example: |
| 125 | + |
| 126 | +```javascript |
| 127 | +var num = 7; |
| 128 | +num > 10 ? console.log(num + " is bigger than 10") : console.log(num + " is not bigger than 10"); |
| 129 | +// this will print "7 is not bigger than 10" |
| 130 | +``` |
| 131 | + |
| 132 | +## Switch Statements |
| 133 | + |
| 134 | +Switch statements acts like a big if/else if/else chain. The switch expression is evaluated once and the value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed. |
| 135 | + |
| 136 | +Syntax: |
| 137 | + |
| 138 | +```javascript |
| 139 | +switch (expression) { |
| 140 | + case n: |
| 141 | + // code to be executed if case n is true |
| 142 | + break; // break out of switch statement once code executed |
| 143 | + case m: |
| 144 | + // code to be executed if case m is true |
| 145 | + break; // break out of switch statement once code executed |
| 146 | + default: // all other cases |
| 147 | + // code to be executed if case n and case m false |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +First Example: |
| 152 | + |
| 153 | +```javascript |
| 154 | +var diet = "vegetarian"; |
| 155 | + |
| 156 | +switch (diet) { |
| 157 | + case "vegetarian": |
| 158 | + console.log("Where do you get your protein? :P "); |
| 159 | + case "gluten free": |
| 160 | + console.log("Wow, that eliminates all the best foods! :( "); |
| 161 | + default: |
| 162 | + console.log("Sounds like you're an omnivore."); |
| 163 | +} |
| 164 | + |
| 165 | +// Output: |
| 166 | +// Where do you get your protein? :P |
| 167 | +// Wow, that eliminates all the best foods! :( |
| 168 | +// Sounds like you're an omnivore. |
| 169 | + |
| 170 | +// Why???? |
| 171 | +// Because no breaking statement is found it executes all of the cases after the true case (which is case 1) |
| 172 | +``` |
| 173 | + |
| 174 | +Second Example: |
| 175 | + |
| 176 | +```javascript |
| 177 | +var diet = "vegetarian"; |
| 178 | + |
| 179 | +switch (diet) { |
| 180 | + case "vegetarian": |
| 181 | + console.log("Where do you get your protein? :P "); |
| 182 | + case "gluten free": |
| 183 | + console.log("Wow, that eliminates all the best foods! :( "); |
| 184 | + break; |
| 185 | + default: |
| 186 | + console.log("Sounds like you're an omnivore."); |
| 187 | +} |
| 188 | + |
| 189 | +// Output: |
| 190 | +// Where do you get your protein? :P |
| 191 | +// Wow, that eliminates all the best foods! :( |
| 192 | + |
| 193 | +// Why???? |
| 194 | +// Because break statement is found at the second case so execution stops there |
| 195 | +``` |
| 196 | + |
| 197 | +## Resources |
| 198 | + |
| 199 | +* [Codecademy - if/if else/if else if else](http://www.codecademy.com/glossary/javascript/if-statement) |
| 200 | +* [MDN - if..else](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) |
| 201 | +* [Codecademy - Ternary Operator](http://www.codecademy.com/glossary/javascript/ternary-operator) |
| 202 | +* [Codecademy - Switch Statements](http://www.codecademy.com/glossary/javascript/switch-statements) |
0 commit comments