A useful program makes decisions and repeats work. Conditional statements choose between paths, and loops repeat a block of code.

Conditional Statements

The if statement runs a block when its condition is true. An else block runs when it is false, and else if tests further conditions in order. The first true condition wins, and the rest are skipped. The switch statement compares one value with a list of cases, and each case should end with break, or the code runs on into the next case.

Definition: Conditional statement. A statement that runs code only when a condition is true.

Loops

A loop repeats a block of code. The for loop is used when the number of repeats is known. It has three parts in its brackets: a starting value, a condition and a step. The while loop repeats as long as its condition is true. The do...while loop always runs its body once, and then checks the condition.

Definition: Loop. A statement that repeats a block of code while a condition holds.

Table 3.3: The three kinds of loop

Loop When the condition is checked Use
for Before each round Known number of repeats
while Before each round Repeat until something changes
do...while After each round Body must run at least once

Controlling Loops

The statement break leaves a loop immediately. The statement continue skips the rest of the current round and starts the next. A loop must change something that eventually makes its condition false, or it will run forever and freeze the page.

Example 3.3: Grades and a table of squares

let marks = 72, grade;
if (marks >= 90) grade = "A";
else if (marks >= 75) grade = "B";
else if (marks >= 60) grade = "C";
else grade = "D";
console.log("Grade:", grade);

for (let i = 1; i <= 5; i++) {
  console.log(i + " squared is " + i * i);
}
let day = 3;
switch (day) {
  case 1: console.log("Mon"); break;
  case 3: console.log("Wed"); break;
  default: console.log("Other");
}

Output:

Grade: C
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
Wed

Truthy and Falsy Values

A condition does not have to be a true or false value. JavaScript treats some values as false when they appear in a condition: false, 0, an empty string, null, undefined and NaN. Every other value counts as true. This allows short checks such as if (name), which is true only when name holds some text. The conditional operator gives a compact form of if and else: age >= 18 ? "adult" : "minor". Use it only for simple choices, because long ones are hard to read.

Common mistakes

  • Writing = instead of === inside a condition, which assigns a value instead of comparing.
  • Forgetting break in a switch, so that the following cases also run.
  • Writing a loop whose condition never becomes false.
  • Being off by one, for example using i < 5 when i <= 5 is needed.

Key takeaways

  • Use if, else if and else to choose, and switch to compare one value with many cases.
  • Use for when the number of repeats is known and while when it is not.
  • break leaves a loop and continue skips to the next round.
  • Always change the loop variable so that the loop can end.

Practice questions

  1. Which loop always runs its body at least once?
  2. What does break do inside a loop?
  3. What is the output of for (let i = 1; i <= 3; i++) console.log(i)?

Answers to the practice questions

  1. The do...while loop.
  2. It leaves the loop at once.
  3. The numbers 1, 2 and 3, each on its own line.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit III