Current Affairs updated daily
KalviGavaniKalviGavani

Control Structure in Javascript Exercise Solutions | KalviGavani

TN Class 11 Computer Applications Control Structure in Javascript exercise solutions — if-else, switch, for, while, do-while, break, continue, MCQ and long answers.

Updated 16 June 202615 min read

Exercise Solutions Available

Step-by-step answers for all textbook questions in this chapter

View Solutions →

Chapter 15 — Control Structure in JavaScript: Exercise Solutions

How to Use This Page

This guide provides detailed solutions for every exercise question from the Samacheer Kalvi Class 11 Computer Applications textbook, Chapter 15: Control Structure in JavaScript. Work through each exercise on your own first, then compare your approach with the solutions below. Pay close attention to the code examples and explanations — they illustrate patterns that frequently appear in board examinations.


Key Concepts (Quick Recap)

TermDefinition
Control StructureA block of code that determines the flow of program execution based on conditions or repetition.
Sequential StructureDefault execution order — statements execute one after another from top to bottom.
Selection StructureAllows the program to choose between alternative paths based on a condition (if, if-else, if-else-if, switch).
if StatementExecutes a block of code only when the specified condition evaluates to true.
if-else StatementProvides two paths — one for true condition, one for false.
if-else-if LadderA chain of if-else statements used to test multiple conditions sequentially.
Switch StatementA multi-way branch construct that matches an expression against constant case values.
Iteration StructureRepeats a block of code multiple times; also called a loop (for, while, do-while).
for LoopEntry-controlled loop with initialisation, condition, and update in one line.
while LoopEntry-controlled loop that repeats as long as the condition is true; condition checked before each iteration.
do-while LoopExit-controlled loop where the body executes at least once; condition checked after each iteration.
break StatementImmediately exits the innermost loop or switch and transfers control to the next statement after it.
continue StatementSkips the remaining code in the current iteration and jumps to the next iteration of the loop.
Infinite LoopA loop whose condition never becomes false, causing it to run indefinitely.
NestingPlacing one control structure inside another; e.g., an if inside a for loop.
Ternary OperatorShorthand for if-else: condition ? valueIfTrue : valueIfFalse.
Default CaseThe fallback case in a switch statement that executes when no other case matches.
Loop Counter (Iterator)A variable that tracks the number of iterations, typically initialised, tested, and updated in a for loop.

I. Choose the Correct Answer (MCQ)

1. Which of the following is a selection structure in JavaScript?
Answer
  • (a) for loop
  • (b) while loop
  • (c) if-else statement ✓
  • (d) do-while loop
Explanationif-else is a selection (decision-making) structure. The for, while, and do-while are iteration (looping) structures.

2. The while loop is called an entry-controlled loop because:
Answer
  • (a) The condition is checked after the loop body executes
  • (b) The condition is checked before the loop body executes ✓
  • (c) The condition is never checked
  • (d) The loop always runs at least once
ExplanationIn a while loop, the condition is evaluated first. If it is true, the body executes; otherwise, the loop is skipped entirely. Hence, it is called entry-controlled.

3. Which loop guarantees at least one execution of the loop body?
Answer
  • (a) for loop
  • (b) while loop
  • (c) do-while loop ✓
  • (d) switch statement
ExplanationThe do-while loop executes the body first and then checks the condition, so it always runs at least once.

4. What does the break statement do inside a loop?
Answer
  • (a) Skips the current iteration
  • (b) Terminates the loop and transfers control to the statement after the loop ✓
  • (c) Restarts the loop from the beginning
  • (d) Pauses the loop execution
Explanationbreak immediately exits the innermost loop or switch, transferring control to the next statement after the loop.

5. The correct syntax of a switch statement in JavaScript is:
Answer
  • (a) switch (expression) { case value: statements; } ✓
  • (b) switch { expression: case value; }
  • (c) switch (case) { value: expression; }
  • (d) switch expression { value: case; }
ExplanationA switch evaluates an expression and matches it against constant case values. The syntax is switch (expression) { case value: statements; break; }.

6. In a for loop, which part is executed only once?
Answer
  • (a) Condition
  • (b) Update expression
  • (c) Initialisation ✓
  • (d) Loop body
ExplanationThe initialisation expression (e.g., let i = 0) is executed only once at the beginning. The condition is checked before every iteration, and the update runs after every iteration.

7. The ternary operator ? : is a shorthand for:
Answer
  • (a) switch statement
  • (b) for loop
  • (c) if-else statement ✓
  • (d) while loop
ExplanationThe ternary operator condition ? expr1 : expr2 is a concise way to write an if-else that returns a value.

8. What is the output of the following code?
let i = 1;
while (i <= 3) {
  console.log(i);
  i++;
}
Answer
  • (a) 0 1 2
  • (b) 1 2 3 ✓
  • (c) 1 2 3 4
  • (d) 0 1 2 3
ExplanationThe loop starts with i = 1. It prints 1, 2, and 3. When i becomes 4, the condition i <= 3 is false and the loop terminates.

II. Very Short Answer Questions (2 Marks)

1. What is a control structure?
Answer

A control structure is a programming construct that determines the order in which statements are executed in a program. JavaScript provides three types of control structures: sequential (default top-to-bottom flow), selection (if, if-else, switch), and iteration (for, while, do-while). Control structures allow programs to make decisions and repeat actions.


2. What are the three types of control structures in JavaScript?
Answer

1. Sequential Structure — Statements execute one after another in order. 2. Selection Structure — The program chooses a path based on a condition (if, if-else, if-else-if ladder, switch). 3. Iteration Structure — A block of code repeats as long as a condition is true (for, while, do-while).


3. What is an if-else statement?
Answer

The if-else statement is a selection control structure that provides two alternative paths. If the condition following if evaluates to true, the if block executes; otherwise, the else block executes. It is used when a program needs to choose between two mutually exclusive actions.


4. What is a switch statement?
Answer

The switch statement is a multi-way branch selection structure. It evaluates an expression and matches its value against one or more case labels. When a match is found, the statements under that case execute. A default case handles situations where no case matches. Each case typically ends with a break to prevent fall-through.


5. What is the difference between while and do-while loops?
Answer
Featurewhile Loopdo-while Loop
Condition checkBefore the loop body (entry-controlled)After the loop body (exit-controlled)
Minimum executions0 (may never execute)1 (always executes at least once)
Syntaxwhile (condition) { body }do { body } while (condition);
Use caseWhen iteration count is unknownWhen body must run at least once

6. What is the break statement?
Answer

The break statement is a control flow statement that immediately terminates the innermost loop or switch statement and transfers control to the statement following the terminated loop or switch. It is commonly used inside switch cases to prevent fall-through and inside loops to exit when a specific condition is met.


III. Short Answer Questions (3 Marks)

1. Explain the if-else-if ladder with an example.
Answer

The if-else-if ladder allows a program to test multiple conditions sequentially. Each condition is evaluated in order; the block associated with the first true condition executes, and the remaining conditions are skipped.

let marks = 75;

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

Here, marks >= 90 is false, so it moves to the next condition. marks >= 75 is true, so "Grade B" is printed and the remaining else-if and else blocks are skipped.


2. Explain the for loop with an example.
Answer

The for loop is an entry-controlled iteration structure where initialisation, condition, and update are specified in a single line. It is used when the number of iterations is known in advance.

Syntax:

for (initialisation; condition; update) {
  // loop body
}

Example — Print numbers 1 to 5:

for (let i = 1; i <= 5; i++) {
  console.log(i);
}
// Output: 1 2 3 4 5
  • let i = 1 — initialisation (runs once)
  • i <= 5 — condition (checked before each iteration)
  • i++ — update (runs after each iteration)

3. Explain the switch statement with an example.
Answer

The switch statement evaluates an expression and matches its value against case labels. When a match is found, the corresponding block executes. The break prevents fall-through to the next case, and default handles unmatched values.

let day = 3;

switch (day) {
  case 1:
    console.log("Monday");
    break;
  case 2:
    console.log("Tuesday");
    break;
  case 3:
    console.log("Wednesday");
    break;
  case 4:
    console.log("Thursday");
    break;
  case 5:
    console.log("Friday");
    break;
  default:
    console.log("Weekend");
}
// Output: Wednesday

4. Explain nested if with an example.
Answer

A nested if is an if statement placed inside another if or else block. It is used when a second condition must be checked only after the first condition is true.

let age = 20;
let hasID = true;

if (age >= 18) {
  if (hasID) {
    console.log("Eligible to vote");
  } else {
    console.log("Bring your ID");
  }
} else {
  console.log("Not eligible to vote");
}
// Output: Eligible to vote

The outer if checks age, and only if age is 18+ does the inner if check for ID.


IV. Long Answer Questions (5 Marks)

1. Explain all types of control structures in JavaScript with examples.
Answer

JavaScript supports three categories of control structures:

a) Sequential Structure: Statements execute in order, one after another, from top to bottom. This is the default flow of any program.

let a = 10;
let b = 20;
let sum = a + b;
console.log(sum); // 30

b) Selection Structure: Allows the program to choose between alternative paths based on conditions.

Simple if:

let num = 10;
if (num > 0) {
  console.log("Positive number");
}

if-else:

let age = 16;
if (age >= 18) {
  console.log("Adult");
} else {
  console.log("Minor");
}
// Output: Minor

if-else-if ladder:

let score = 82;
if (score >= 90) {
  console.log("A+");
} else if (score >= 80) {
  console.log("A");
} else if (score >= 70) {
  console.log("B");
} else {
  console.log("C");
}
// Output: A

switch:

let colour = "red";
switch (colour) {
  case "red":
    console.log("Stop");
    break;
  case "yellow":
    console.log("Ready");
    break;
  case "green":
    console.log("Go");
    break;
  default:
    console.log("Invalid colour");
}
// Output: Stop

c) Iteration Structure: Repeats a block of code as long as a condition is true.

for loop:

for (let i = 1; i <= 5; i++) {
  console.log(i);
}
// Output: 1 2 3 4 5

while loop:

let i = 1;
while (i <= 5) {
  console.log(i);
  i++;
}
// Output: 1 2 3 4 5

do-while loop:

let j = 1;
do {
  console.log(j);
  j++;
} while (j <= 5);
// Output: 1 2 3 4 5

2. Write a JavaScript program to find the factorial of a number using different loops.
Answer

The factorial of n (written as n!) is the product of all positive integers from 1 to n. Example: 5! = 5 x 4 x 3 x 2 x 1 = 120.

a) Using for loop:

let n = 5;
let fact = 1;
for (let i = 1; i <= n; i++) {
  fact = fact * i;
}
console.log(n + "! = " + fact);
// Output: 5! = 120

b) Using while loop:

let n = 5;
let fact = 1;
let i = 1;
while (i <= n) {
  fact = fact * i;
  i++;
}
console.log(n + "! = " + fact);
// Output: 5! = 120

c) Using do-while loop:

let n = 5;
let fact = 1;
let i = 1;
do {
  fact = fact * i;
  i++;
} while (i <= n);
console.log(n + "! = " + fact);
// Output: 5! = 120

In all three approaches, the variable fact is initialised to 1 (since 0! = 1 and multiplying by 1 is the identity). The loop multiplies fact by each integer from 1 to n. Note: the do-while loop will produce 1 even if n = 0, which is correct since 0! = 1.


3. Explain the break and continue statements with examples.
Answer

break Statement: The break statement immediately terminates the innermost loop or switch and transfers control to the statement after it.

Example — break in a loop:

for (let i = 1; i <= 10; i++) {
  if (i === 5) {
    break;
  }
  console.log(i);
}
// Output: 1 2 3 4

When i equals 5, break exits the loop entirely. Numbers 5 through 10 are never printed.

Example — break in switch:

let day = "Tuesday";
switch (day) {
  case "Monday":
    console.log("Start of week");
    break;
  case "Tuesday":
    console.log("Second day");
    break;
  default:
    console.log("Other day");
}
// Output: Second day

Without break, execution would "fall through" to subsequent cases, which is usually unintended.

continue Statement: The continue statement skips the remaining code in the current iteration and jumps to the next iteration of the loop.

Example — continue in a loop:

for (let i = 1; i <= 10; i++) {
  if (i % 2 !== 0) {
    continue;
  }
  console.log(i);
}
// Output: 2 4 6 8 10

When i is odd, continue skips the console.log and moves to the next iteration. Only even numbers are printed.

Key Differences:

Featurebreakcontinue
EffectExits the loop entirelySkips only the current iteration
Next actionControl goes to statement after the loopControl goes to the loop's condition check
Used inLoops and switchLoops only
TerminatesThe entire loopOnly the current iteration

Common Mistakes to Avoid

1. Using = instead of == or === for comparison. The = operator assigns a value; == compares with type coercion; === compares without coercion. Writing if (x = 5) assigns 5 to x and evaluates as truthy — this is almost always a bug.

2. Missing curly braces around multi-line if/else blocks. Without braces, only the next statement belongs to the block. Always use {} even for single-statement blocks to avoid bugs.

3. Forgetting break in switch cases. Omitting break causes fall-through — execution continues into the next case, producing unexpected results.

4. Creating infinite loops. Forgetting to update the loop counter (e.g., missing i++ in a while loop) means the condition never becomes false, causing the loop to run forever.

5. Placing a semicolon after the while condition in a do-while loop. The correct syntax is do { ... } while (condition); — the semicolon is required after the while condition. Omitting it or placing it incorrectly causes syntax errors.

6. Comparing floating-point numbers with ==. Due to precision issues, 0.1 + 0.2 === 0.3 evaluates to false. Use a tolerance check instead: Math.abs(a - b) < 0.0001.

7. Using var instead of let or const in for loops. var has function scope, which can cause unexpected behaviour in closures. Use let for loop counters to get block scope.

8. Off-by-one errors in loop bounds. Confusing < with <= or starting the counter at 0 vs 1 causes the loop to run one time too many or too few.


Exam Checklist

  • [ ] Can define control structure and name all three types (sequential, selection, iteration)
  • [ ] Can write correct syntax for if, if-else, and if-else-if ladder
  • [ ] Can write correct syntax for switch with break and default
  • [ ] Can distinguish between entry-controlled (for, while) and exit-controlled (do-while) loops
  • [ ] Can write for, while, and do-while loops with correct syntax
  • [ ] Can trace the output of a given loop or selection structure
  • [ ] Understand the ternary operator (? :) as shorthand for if-else
  • [ ] Can explain the difference between break and continue with examples
  • [ ] Can identify and fix common errors: = vs ==, missing braces, infinite loops, missing break in switch
  • [ ] Can write programs using nested control structures (e.g., nested if, if inside a loop)
  • [ ] Can write a program to find factorial, sum, or product using loops
  • [ ] Know that do-while always executes at least once; while may execute zero times
  • [ ] Can explain the flow of a switch statement including fall-through behaviour
  • [ ] Understand loop counter initialisation, condition, and update expressions in a for loop

Frequently Asked Questions

JavaScript supports three categories: sequential (default top-to-bottom execution), selection (if, if-else, if-else-if ladder, switch), and iteration (for, while, do-while). It also provides break and continue for loop control.