JavaScript Control Flows

Sept 13, 2022

Control Flows

Control Flow

  • Conditional Statements - provide branching logic so that only certain code executes

  • Iteration Statements - allow for repeated code execution

CONDITIONAL STATEMENTS

CONDITIONAL STATEMENTS

Conditionals provide decision points in your code to branch in different sections. In making the decision, an expression is evaluated and if a condition is met then specified code will execute.

COMPARISON OPERATORS

COMPARISON OPERATORS

  • > Greater than
  • < Less than
  • >= Greater than or equal
  • <= Less than or equal

COMPARISON OPERATORS

  • == Equality
    • checks for equality after type conversion
  • === Strict Equality
    • checks for equality before type conversion
  • != Inequality
    • checks for inequality aer type conversion
  • !== Strict inequality
    • checks for inequality before type conversion

LOGICAL OPERATORS

LOGICAL OPERATORS

  • && Logical AND
    • both operands must be true to return true
  • || Logical OR
    • only one operand must be true to return true
  • ! Logical NOT
    • inverts its operand; true becomes false and false becomes true

IF STATEMENT

IF Statement

if (score >= 50) {
    congratulate();
}

IF/ELSE STATEMENT

if (score >= 50) {
    congratulate();
} else {
    encourage();
}

IF/ELSE IF STATEMENT

if (score >= 500) {
    claimHacks();
} else if (score >= 50) {
    congratulate();
} else {
    encourage();
}

Loops

Loops

Loops will run their code as long as their check condition remains true.

For Loop

for (let count = 0; count < 10; count++) {
    console.log(count);
}

While loop

Checks condition first

let count = 10;
while (count > 0) {
    console.log(count);
    count--;
}

do/while loop

let count = 10;
do {
    console.log(count);
    count--;
} while (count > 0);