Control Structures in JavaScript

Control structures let you control the flow of execution in a JavaScript program. They allow your code to make decisions, repeat tasks, and handle multiple conditions dynamically. Mastering control structures is essential for writing logical, efficient, and maintainable programs.

The main control structures in JavaScript are:

  • Conditional statements (if, else, else if, switch)
  • Loops (for, while, do...while)

1. Conditional Statements

Conditional statements execute code only when certain conditions are met.


1.1 if Statement

Executes a block of code if a condition evaluates to true.

Syntax

if (condition) {
  // code runs if condition is true
}

Example

let age = 20;

if (age >= 18) {
  console.log("You are eligible to vote.");
}

1.2 if...else Statement

Executes one block if the condition is true, otherwise executes another.

let temperature = 25;

if (temperature > 30) {
  console.log("It's a hot day!");
} else {
  console.log("The weather is nice.");
}

1.3 else if Ladder

Used when multiple conditions need to be checked in order.

let score = 85;

if (score >= 90) {
  console.log("Grade: A");
} else if (score >= 80) {
  console.log("Grade: B");
} else if (score >= 70) {
  console.log("Grade: C");
} else {
  console.log("Grade: F");
}

2. switch Statement

The switch statement evaluates an expression and matches it against multiple values. It’s cleaner than long if...else chains when checking one variable against many values.

Syntax

switch (expression) {
  case value1:
    // code
    break;
  case value2:
    // code
    break;
  default:
    // fallback code
}

Example

let day = 3;
let dayName;

switch (day) {
  case 1:
    dayName = "Monday";
    break;
  case 2:
    dayName = "Tuesday";
    break;
  case 3:
    dayName = "Wednesday";
    break;
  case 4:
    dayName = "Thursday";
    break;
  case 5:
    dayName = "Friday";
    break;
  case 6:
    dayName = "Saturday";
    break;
  case 7:
    dayName = "Sunday";
    break;
  default:
    dayName = "Invalid day";
}

console.log(dayName); // Wednesday

Important Notes

  • break prevents fall-through to the next case
  • default runs if no case matches (optional but recommended)

3. Loops

Loops allow you to repeat a block of code while a condition is true.


3.1 for Loop

Best used when you know the number of iterations in advance.

Syntax

for (initialization; condition; increment) {
  // repeated code
}

Example

for (let i = 0; i < 5; i++) {
  console.log("Number:", i);
}

3.2 while Loop

Runs as long as the condition is true. Use it when iterations are uncertain.

let count = 0;

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

3.3 do...while Loop

Executes the block at least once, even if the condition is false initially.

let num = 5;

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

πŸ“Œ Output:

Number is: 5

Comparison Summary

StructureBest Use Case
ifSimple decision making
else ifMultiple conditions
switchMany values for one variable
forKnown number of iterations
whileCondition-based repetition
do...whileMust run at least once

Summary

Control structures are the backbone of JavaScript logic:

  • if, else, else if β†’ Decision making
  • switch β†’ Cleaner multi-value branching
  • Loops (for, while, do...while) β†’ Repetitive execution

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *