Control structure: If...Else, switch..case; While, Do...While, For loop; break, continue

Control structures decide the path through code: if/else and switch choose a branch, while / do-while / for repeat a block, and break stops a loop early while continue skips to its next turn, with switch cases needing break to avoid falling through.

11 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The page has to make decisions

FestConnect must decide: is this student eligible (year 2 or above)? It must repeat: print each of the 30 events. It must choose among options: cash, card, or UPI. And sometimes it must stop early: found a full event, quit scanning.

So far code ran straight down, top to bottom. Control structures bend that flow: they let code branch (choose a path) and loop (repeat a path). Two kinds of branch, three kinds of loop, and two ways to interrupt one.

Theory

Forks and roundabouts

Reading code as driving: an if/else is a fork, you take one road based on a sign. A switch is a multi-exit roundabout: you leave at the exit that matches your destination. A loop is a roundabout you keep circling until a condition lets you exit. break is an emergency exit that leaves the roundabout at once; continue skips straight to the next lap without finishing this one.

Theory

Branch, then loop

Branching:

  • if (cond) { } else if (cond2) { } else { }: run the first block whose condition is true.
  • switch (value) { case x: ...; break; default: ... }: match one value against many. Each case needs break.

Looping:

  • while (cond) { }: check first, then run (can run zero times).
  • do { } while (cond): run once, then check (at least one pass).
  • for (init; cond; update) { }: the counting loop.

Interrupt: break leaves the loop; continue skips to its next turn.

Practical

FestConnect decisions and loops

let year = 3;
if (year >= 2) {
  console.log("Eligible");
} else {
  console.log("First years cannot register");
}

// switch: each case needs break
let pay = "upi";
switch (pay) {
  case "cash": console.log("Pay at desk"); break;
  case "upi":  console.log("Scan QR");     break;
  default:     console.log("Unknown");
}

// for loop over 3 events, skip the full one, stop at 'gala'
let events = ["robotics", "full", "dance", "gala"];
for (let i = 0; i < events.length; i++) {
  if (events[i] === "full") continue;   // skip this turn
  if (events[i] === "gala") break;      // stop entirely
  console.log(events[i]);               // robotics, dance
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

while vs do-while

seatsLeft is 0. Compare while (seatsLeft > 0) { book(); } with do { book(); } while (seatsLeft > 0);. How many times does book() run in each, and why does it matter for FestConnect?

Show the answer

The while version runs book() zero times: it checks 0 > 0 first, which is false, so it never enters. The do-while version runs book() once before checking, booking a seat that does not exist, then stops. do-while always runs at least once, which is a bug here. Rule: use do-while only when the body genuinely must run once regardless (like showing a menu before asking to repeat).

Quiz

A switch has `case "cash": console.log("Desk"); case "upi": console.log("QR");` with NO break. The value is "cash". What prints?

  1. Both 'Desk' and 'QR', because execution falls through without break
  2. Only 'Desk'
  3. Only 'QR'
  4. Nothing, because a break is required to enter a case
Show the answer

Both 'Desk' and 'QR', because execution falls through without break

Without break, once a case matches, execution 'falls through' and keeps running the following cases too, so 'cash' prints both 'Desk' and 'QR' (A). It does not stop at the first (B), and it does enter the matching case fine (C, D are wrong). This fall-through is the classic switch bug: put break at the end of every case unless you deliberately want to share code between them.

Watch out

Control-flow traps

1. Missing break in switch: execution falls through to later cases. End each case with break.

2. Infinite loop: a while/for whose condition never becomes false (you forgot to change the counter) freezes the page. Always move toward the exit.

3. do-while runs once even when it should not: check-first with while unless one guaranteed pass is intended.

4. = in a condition: if (year = 2) assigns, always truthy. Use ===.

Theory

Loops power validation

In Unit 5, form validation loops over every required field checking it is filled, using exactly this for plus an if, and breaks out to report the first empty one. The eligibility if here is the seed of that validation logic. Branching and looping are the backbone of every interactive feature you will build.

Summary

Key takeaways

  • Branch with if / else if / else (choose a path) and switch/case (match one value against many).
  • Loop with while (check first), do-while (run once first), for (counting loop).
  • break exits a loop immediately; continue skips to the loop's next turn.
  • Every switch case needs break or execution falls through to the next case.
  • Avoid infinite loops (always move the condition toward false) and never use = in a condition.
  • Memory hook: forks (if/switch) and roundabouts (loops), with break as the emergency exit.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Overview of JavaScript

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Control structure: If...Else, switch..case; While, Do...While, For loop; break, continue · Web Designing-1 (option A) · Gri-Learn