Control structures are the foundation of any programming language. They allow developers to dictate the flow of execution based on specific conditions, making programs dynamic and responsive. TypeScript, a superset of JavaScript, retains all the control structures available in JavaScript, but with the added benefit of static typing. Among the most fundamental control structures are decision-making statements like if
, else
, else if
, and switch
. In this tutorial, we’ll explore how these decision-making structures work in TypeScript through practical examples and use cases.
1. If Statement
The if
statement evaluates a Boolean expression and executes a block of code if the condition is true.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
}
Example:
let temperature: number = 35;
if (temperature > 30) {
console.log("It's a hot day!");
}
Output:
It's a hot day!
Explanation:
In the above example, the condition temperature > 30
is true, so the message is printed.
2. If…Else Statement
The if...else
structure provides an alternative set of instructions if the condition is false.
Syntax:
if (condition) {
// block executed if the condition is true
} else {
// block executed if the condition is false
}
Example:
let a: number = 10;
let b: number = 30;
if (a > b) {
console.log("A is greater than B");
} else {
console.log("B is greater than A");
}
Output:
B is greater than A
Real-World Use Case:
let age: number = 17;
if (age >= 18) {
console.log("You are eligible to vote.");
} else {
console.log("You are not eligible to vote.");
}
3. Else If and Nested If Statements
When multiple conditions need to be tested, else if
and nested if
statements are used.
Syntax:
if (condition1) {
// executed if condition1 is true
} else if (condition2) {
// executed if condition2 is true
} else {
// executed if all previous conditions are false
}
Example:
let marks: number = 85;
if (marks >= 90) {
console.log("Grade: A+");
} else if (marks >= 75) {
console.log("Grade: A");
} else if (marks >= 60) {
console.log("Grade: B");
} else {
console.log("Grade: C");
}
Output:
Grade: A
Nested If Example:
let username: string = "admin";
let password: string = "1234";
if (username === "admin") {
if (password === "1234") {
console.log("Login successful");
} else {
console.log("Incorrect password");
}
} else {
console.log("Invalid username");
}
4. Switch Statement
The switch
statement is a cleaner way to compare a variable with multiple values.
Syntax:
switch(expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// default code block
}
Example:
let day: number = 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;
case 6:
console.log("Saturday");
break;
case 7:
console.log("Sunday");
break;
default:
console.log("Invalid day");
break;
}
Output:
Wednesday
Real-World Use Case:
let status: string = "shipped";
switch(status) {
case "pending":
console.log("Your order is pending.");
break;
case "shipped":
console.log("Your order has been shipped.");
break;
case "delivered":
console.log("Your order has been delivered.");
break;
default:
console.log("Unknown status");
break;
}
Summary
Decision-making statements in TypeScript help determine the path of program execution. From the simple if
statement to the more structured switch
case, these control structures are essential for writing robust and dynamic code. Here’s a recap:
if evaluates a condition and executes code if true.
if…else provides two paths depending on the condition.
else if and nested if statements allow multiple conditions to be evaluated.
switch is ideal when comparing a single variable to multiple values.
💻 8 TypeScript Programming Assignments on Control Structures
🧩 Assignment 1: Positive, Negative or Zero
Write a TypeScript program that takes a number and prints whether it is:
- Positive
- Negative
- Zero
✅ Use if-else if-else
structure.
🧩 Assignment 2: Simple Voting Eligibility Checker
Take a person’s age as input. If the person is 18 or older, print “Eligible to vote”, otherwise print “Not eligible”.
✅ Use if-else
.
🧩 Assignment 3: Largest of Three Numbers
Write a program that takes three numbers and prints the largest.
✅ Use nested if-else
or chained conditions.
🧩 Assignment 4: Simple Calculator (switch-case)
Input two numbers and an operator (+, –, *, /). Perform the operation using switch
.
✅ Use switch-case
, and include a default
for invalid operators.
🧩 Assignment 5: Check Leap Year
Write a program to check if a year is a leap year:
- Divisible by 4 and not by 100, or divisible by 400
✅ Use if-else
with logical operators.
🧩 Assignment 6: Grade Generator
Input marks (0–100) and print:
- A: 90–100
- B: 80–89
- C: 70–79
- D: 60–69
- F: below 60
✅ Use chained if-else if
.
🧩 Assignment 7: Login Authentication
Take username and password as input.
- If both are correct, show success
- If username correct, password wrong → “Incorrect password”
- Else → “Invalid credentials”
✅ Use nested if-else
.
🧩 Assignment 8: Month Days Calculator
Input a month (1 to 12) and print how many days it has.
- Use
switch-case
- Include leap year check for February (assume year 2024)
✅ Use switch
for month and if
for leap year logic inside case 2 (Feb).