Control Flow

Control flow is how JavaScript decides the order in which code runs. It allows your program to make decisions, repeat actions, or stop when needed. This tutorial explains how to use conditional statements and loops to control the flow of your program.

Conditional Statements

Conditional statements allow your program to perform different actions based on conditions. Think of it as asking, "If this is true, do something."

if Statement

The if statement runs a block of code only if the condition is true.

Example:

Javascript
Copy
let age = 18;

if (age >= 18) {
  console.log("You are an adult.");
}

Here, if age is 18 or greater, it prints "You are an adult."

if...else Statement

Use if...else when you want to do one thing if the condition is true and something else if it's false.

Example:

Javascript
Copy
let age = 16;

if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are not an adult.");
}

If age is less than 18, it prints "You are not an adult."

if...else if...else Statement

Use else if to check multiple conditions.

Javascript
Copy
let score = 75;

if (score >= 90) {
  console.log("Excellent!");
} else if (score >= 50) {
  console.log("Good job!");
} else {
  console.log("Keep trying!");
}

Here:

  • If score is 90 or above, it prints "Excellent!"
  • If score is between 50 and 89, it prints "Good job!"
  • Otherwise, it prints "Keep trying!"

switch Statement

The switch statement is useful when you need to check multiple specific values.

Example:

Javascript
Copy
let day = 3;

switch (day) {
  case 1:
    console.log("Monday");
    break;
  case 2:
    console.log("Tuesday");
    break;
  case 3:
    console.log("Wednesday");
    break;
  default:
    console.log("Unknown day");
}
  • The case checks specific values (e.g., day === 1).
  • break stops the code from running the next cases.
  • default runs if no cases match.

Loops

Loops allow your program to repeat a block of code multiple times. They're great for tasks like processing lists or running code until a condition is met.

for Loop

The for loop is used when you know how many times you want to repeat something.

Syntax:
Javascript
Copy
for (initialization; condition; increment) {
  // Code to repeat
}

Example:

Javascript
Copy
for (let i = 1; i <= 5; i++) {
  console.log("Count:", i);
}
  • Initialization: let i = 1 (start at 1).
  • Condition: i <= 5 (keep looping while true).
  • Increment: i++ (increase i by 1 after each loop).

while Loop

The while loop repeats as long as the condition is true.

Example:

Javascript
Copy
let count = 1;

while (count <= 3) {
  console.log("Number:", count);
  count++;
}

Here, the loop runs while count is less than or equal to 3.

do...while Loop

The do...while loop is similar to while, but it always runs the code at least once, even if the condition is false.

Example:

Javascript
Copy
let count = 1;

do {
  console.log("Number:", count);
  count++;
} while (count <= 3);

Even if the condition is false initially, the code inside the loop will run once.

for...of Loop

The for...of loop is used to iterate over arrays or other iterable objects.

Example:

Javascript
Copy
let fruits = ["apple", "banana", "cherry"];

for (let fruit of fruits) {
  console.log(fruit);
}

Here, the loop goes through each item in the fruits array and prints it.

for...in Loop

The for...in loop is used to loop through the properties of an object.

Example:

Javascript
Copy
let person = { name: "Alice", age: 25 };

for (let key in person) {
  console.log(key + ":", person[key]);
}

Output:

name: Alice age: 25

Breaking Out of Loops

You can stop a loop early using the break statement.

Example:

Javascript
Copy
for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    break; // Stops the loop when i is 3
  }
  console.log(i);
}

Output:

1 2

Skipping Iterations

Use continue to skip the current iteration and move to the next.

Example:

Javascript
Copy
for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    continue; // Skips when i is 3
  }
  console.log(i);
}

Output:

1 2 4 5