Module 4: Control Flow - Conditional Execution and Loops

Cards (29)

  • if statement
    first and simplest control flow instruction
    basic form: checks a given condition, and depending on its Boolean value, either executes a block of code, or skips it
  • if statement syntax
    if (condition) {
    (block of code)
    }
  • True
    variables and constants declared using let and const inside a block are local, that is, their scope is limited to that particular block
  • True
    using data blocks, we can make the constants and variables declared inside them local (not visible to the outside)
  • if ... else statement

    • allows us to add a second code block that will be executed only when the initial condition is NOT met. Both blocks of code are separated using the else keyword.
    • most universal construction
  • if ... else syntax 

    if (condition) {
    condition - true code }
    else {
    condition - false code
    }
  • ifelseif statement

    An else segment can have an if or if … else statement inside it, and it’s possible to nest any number of if … else statements
  • ifelseif syntax

    if (conditions_1) {
    code
    } else if (condition_2) {
    code
    } else if (condition_3) {
    code
    } else {
    code
    }
  • conditional (ternary) operator
    • allows us to perform one of two actions based on the value of the first operand
    • Most often it is used as an alternative to the if ... else
    • it is simply shorter and a bit more readable than the if ...else construction
  • the first operand (before the question mark) is the condition to verify, the second operand (between the question mark and the colon) will be returned if the condition is true, and the third operand (after the colon) if the condition is false.
    let price = 100;
    let shippingCost = price > 50 ? 0 : 5;
    console.log(`price = ${price}, shipping = ${shippingCost}`); // -> price = 100, shipping = 0
  • True
    conditional operator returns the values of the second or third operand, but if they are complex expressions, it will calculate them first
  • switchcase statement
    somewhat similar to nested if … else statements, but instead of evaluating different expressions, switch evaluates one conditional expression and then tries to match its value to one of the given cases. 
  • switchcase syntax
    switch (expression) {
    case first_match:
    code
    break;
    case second_match:
    code
    break;
    default:
    code
    }
  • default
    • special case; present (by convention placed on the end of the switch statement. however, it isn’t required).
    • is executed when none of the cases matches the expression. The evaluation itself is made with a strict comparison operator (===) so not only must the value match, but also the type of case value and the expression.
  • loops
    • second form of flow control statement
    • are repeated a given number of times
    • continue until some condition is met
  • while loop syntax

    while(condition) {
    block of code
    }
  • while loop
    • one of the loops that we normally use when we don't know exactly how many times something should be repeated, but we do know when to stop
    • can turn into an infinite loop, a loop without end
    • condition is checked before each iteration
    • may never be executed at all if the initial condition is evaluated to false at the beginning of the loop
  •  dowhile loop
    • checked after each iteration
    • always executed at least once before the first condition check is done
  • do...while loop syntax 

    do {
    code block
    } while(condition);
  • for loop
    • part of the second type of loops
    • one which is better in situations where we know how many times the loop should be executed (although this is not necessary)
  • for loopsyntax

    for (initialization; condition; increment) {
    block of code
    }
  • for loop initialization statement
    • executed only once
    • Usually, it’s used to initialize (or declare and initialize) a variable that will be used as a loop counter
    • use any existing variable as a counter but good to declare a new one
    • optional and can be left empty, except for ending with a semicolon
  • for loop condition statement
    expression that is evaluated to a Boolean before each loop iteration
    true = execute code, false = terminate
    optional; but if left empty, it will be assumed as always true
    leading to an infinite loop if no other means of exiting the loop is used
  • for loop increment statement
    • always executed at the end of the current loop iteration, and most of the time it’s used to increment (or decrement, depending on the need) a loop counter that is used in a condition statement
    • can be any expression, not only incrementation/decrementation.
    • can also be left empty.
  •  for ... of

    • dedicated for use with arrays (and other iterative structures
    • we do not explicitly specify any conditions or number of iterations, as it is performed exactly as many times as there are elements in the indicated array
  • for...of syntax

    for (variable of array) {
    block of code
    }
  • forin
    • enables us to walk through object fields
    • iterates through all fields of the indicated object, placing the names of these fields (or keys) in the variable
  • break statement
    used to terminate the execution of a loop or a switch statement; exits the whole loop or switch statement, and jumps to the first statement after that loop or switch
  • continue
    • used in loops (but not in the switch statement)
    • when used, it applies to the closest surrounding loop
    • does not end the whole loop, but rather starts the next iteration of this loop
    • can think of it as jumping right to the end of the current iteration