Lecture 14: Conditional Statements & Looping

BMC201 - Web Technology

Mr. Prashant Kumar Nag

2026-02-17

Lecture 14

Conditional Statements & Looping

Week 4 | Unit II: JavaScript Functions & Events
BMC201 - Web Technology
Mr. Prashant Kumar Nag, Assistant Professor

Learning Objectives


  • Make decisions with if and switch
  • Repeat tasks using loops
  • Compare for, while, and do...while
  • Use break and continue correctly
  • Avoid common infinite-loop mistakes

If / Else Logic


const marks = 72;

if (marks >= 90) {
  console.log("Grade A");
} else if (marks >= 60) {
  console.log("Grade B");
} else {
  console.log("Grade C");
}

Comparison and Logical Operators


age >= 18
score === 100
userRole === "admin" && isActive
isGuest || hasAccess
  • Use === for strict checks
  • Combine conditions with && and ||

Switch Statement


const day = "Friday";

switch (day) {
  case "Monday":
    console.log("Start of week");
    break;
  case "Friday":
    console.log("Almost weekend");
    break;
  default:
    console.log("Regular day");
}

For Loop


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

Best when iteration count is known.

While and Do…While


let count = 0;
while (count < 3) {
  console.log(count);
  count++;
}
let n = 0;
do {
  console.log(n);
  n++;
} while (n < 3);
  • while: condition first
  • do...while: runs at least once

for…of vs for…in


const nums = [10, 20, 30];
for (const value of nums) {
  console.log(value);
}

const user = { name: "Aman", age: 21 };
for (const key in user) {
  console.log(key, user[key]);
}

break and continue


for (let i = 1; i <= 10; i++) {
  if (i === 4) continue;
  if (i === 8) break;
  console.log(i);
}
  • continue: skip current iteration
  • break: exit loop completely

Common Loop Pitfalls


  • Forgetting to update loop variable
  • Wrong boundary (<= vs <)
  • Modifying array while iterating carelessly
  • Nesting too many loops unnecessarily

Questions?

Next: Lecture 15 - Objects & Scope