Master TypeScript From Scratch

Learn TypeScript step by step with practical examples, interactive coding lessons, and real-world projects.

TypeScript Control Statements

Learn how to control the flow of a TypeScript program using if/else statements, switch statements, for loops, while loops, do-while loops, break, and continue.

Simple Explanation: Control statements determine which code should execute and how many times a block of code should repeat. They help programs make decisions and perform repetitive tasks.

What Will We Learn?

Control statements are important for implementing application rules, validating inputs, processing records, and repeating operations.

  • Make decisions using if, else if, and else.
  • Select an execution path using switch.
  • Repeat instructions using for loops.
  • Use while and do-while loops.
  • Stop a loop using break.
  • Skip an iteration using continue.

Control Statements Workflow

Follow these steps to understand how control statements direct program execution.

📥
Step 1
Start Program

Execute the statements.

→
🧠
Step 2
Evaluate

Check the condition or expression.

→
🔀
Step 3
Control Flow

Choose a path or repeat a block.

→
✅
Step 4
Complete

Continue or finish execution.

1. If-Else Statement

The if-else statement executes one block when a condition is true and another block when the condition is false.

Example

let marks: number = 75;

if (marks >= 35) {
    console.log("Pass");
} else {
    console.log("Fail");
}

Expected Output

Pass

Because the marks are at least 35, the condition is true and the first block executes.

2. Switch Statement

A switch statement selects a matching case based on the value of an expression. It is useful when one value has several possible cases.

Example

let day: number = 2;

switch (day) {
    case 1:
        console.log("Monday");
        break;

    case 2:
        console.log("Tuesday");
        break;

    case 3:
        console.log("Wednesday");
        break;

    default:
        console.log("Invalid Day");
}

Expected Output

Tuesday

The value of day matches case 2, so the Tuesday message is displayed. The break statement prevents execution from falling through to the following cases.

3. For Loop

A for loop repeats a block of code while its condition remains true. It is commonly used when you know how many iterations are required.

Example

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

Expected Output

1
2
3
4
5

The loop starts at 1, runs while i <= 5, and increments i after each iteration.

4. While Loop

A while loop checks its condition before each iteration. If the condition is false initially, the loop body does not execute.

Example

let count: number = 1;

while (count <= 3) {
    console.log(count);
    count++;
}

Expected Output

1
2
3

The loop continues while count is less than or equal to 3. Incrementing count eventually makes the condition false.

5. Do-While Loop

A do-while loop executes its body first and checks its condition afterward. Therefore, the body executes at least once, even if the condition is initially false.

Example

let count: number = 1;

do {
    console.log(count);
    count++;
} while (count <= 3);

Expected Output

1
2
3

The body executes, then the condition is checked. This continues until the condition becomes false.

6. Break and Continue

The break statement terminates the nearest enclosing loop or switch statement. The continue statement skips the rest of the current loop iteration and proceeds to the next one.

Example: Break

for (let i: number = 1; i <= 5; i++) {
    if (i === 4) {
        break;
    }

    console.log(i);
}

Expected Output

1
2
3

Example: Continue

for (let i: number = 1; i <= 5; i++) {
    if (i === 3) {
        continue;
    }

    console.log(i);
}

Expected Output

1
2
4
5

The first loop stops at 4. The second loop skips printing 3 but continues with the remaining values.

7. Control Statements Comparison

Statement Purpose Condition Checked Typical Use
if/else Choose between branches. Before the selected block. Validation and decisions.
switch Select a matching case. Against case values. Menus and fixed options.
for Repeat a block. Before each iteration. Counted repetitions.
while Repeat while true. Before each iteration. Condition-based repetition.
do-while Run at least once. After each iteration. First-run-required tasks.
break Exit the nearest loop or switch. When executed. Stop processing.
continue Skip the current iteration. When executed. Skip selected items.

Practical Example: CIIT Student Results

Let's calculate a student's result and display the subjects using control statements.

Example

let studentName: string = "Priya";
let marks: number = 75;

if (marks >= 35) {
    console.log(studentName + ": Pass");
} else {
    console.log(studentName + ": Fail");
}

console.log("Subject Marks:");

for (let subject: number = 1; subject <= 3; subject++) {
    console.log("Subject " + subject);
}

Expected Output

Priya: Pass
Subject Marks:
Subject 1
Subject 2
Subject 3

The if/else statement checks the result. The for loop repeats the subject display three times.

Common Mistakes

  • Forgetting braces when a block contains multiple statements.
  • Forgetting to update the counter inside a while loop, which may create an infinite loop.
  • Forgetting break in a switch when fall-through is not intended.
  • Assuming a while loop always executes at least once.
  • Confusing the effect of break and continue.
  • Writing conditions that never become false in a loop.
Tip: Always check loop conditions and counter updates to avoid infinite loops.

Practice Task

Create a TypeScript program that performs these tasks.

  • Store student marks as 80.
  • Display Pass if marks are at least 35; otherwise display Fail.
  • Use a for loop to print numbers from 1 to 5.
  • Use continue to skip the number 3.

Hint

let marks = 80;

if (marks >= 35) {
    console.log("Pass");
} else {
    console.log("Fail");
}

for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        continue;
    }

    console.log(i);
}

Expected Output

Pass
1
2
4
5

Summary

In this module, we introduced if/else statements, switch statements, for loops, while loops, do-while loops, break, and continue.

Key Point: Conditional statements make decisions, loops repeat instructions, and break/continue control how a loop proceeds.