Control Flow : The order in which instructions are executed within a program.
Control Structures : Expressions that alter the control flow based on given parameters.
Conditional Statements : A control structure that is used to perform different actions based on different conditions.
Else Statement : statement that will evaluate if prior conditions are deemed falsey.
Think of them as a chain. > Fun Fact : Nesting the else/if and else chains are called ‘cuddled else’s’; this is common JS style but functionally they help the reader see more information in a more concise way.
Guard Clauses : useful to refactor conditional logic and to reduce the number of lines in your functions.
When to use If Statements
If we are working with a situation that is mutually exclusive, then we should use an If/Else Statement.
Age old principal for writing good code: DRY (Don’t repeat yourself)!
Loops : Are a fundamental control structure that will repeatedly execute a section of code while a condition is true.
While Loops : Will execute a block of code as long a specified condition is true.
while (condition) {
// code block to be executed
}
let index = 0;
while (index < 10) {
console.log("The number is " + index);
index++;
}
Important Loop Knowledge
Don’t forget to always start your loops with a zero index.
iteration : the act of repeating a procedure; looping is an iterative technique
For Loops For loops can be broken down into three sections:
Translating From One Loop to Another
Here is an example of the same loop expressed as a while and for loop.
function forLoopDoubler (array) {
for (let i = 0; i < array.length; i++>) {
array[i] = array[i] \* 2
}
return array;
};
function forLoopDoubler (array) {
let i = 0;
while (i < array.length>) {
array[i] = array[i] \* 2;
i++;
}
return array;
};
Using Arrays
Working with Arrays