Control Flow Statements in TypeScript
Control flow statements are essential constructs that allow you to dictate the order in which statements are executed in your TypeScript programs. These statements enable you to make decisions, repeat actions, and handle exceptions, thereby creating more dynamic and responsive applications.
Below, we explore the various control flow statements available in TypeScript, complete with descriptions and examples to illustrate their use.
You will explore various control flow statements such as if...else, switch, for, for...of, for...in, while, do...while, break, continue, try...catch...finally, and throw, and learn how to use them effectively in your TypeScript programs.
1. if...elseβ
The if...else statement is used to execute a block of code based on a condition.
Example:
let age: number = 25;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
2. switchβ
The switch statement is used to perform different actions based on different conditions.
Example:
typescript
let fruit: string = "apple";
switch (fruit) {
case "apple":
console.log("Apple");
break;
case "banana":
console.log("Banana");
break;
default:
console.log("Unknown fruit");
}
3. forβ
The for loop is used to execute a block of code a specified number of times.
Example:
for (let i = 0; i < 5; i++) {
console.log(i);
}
4. for...ofβ
The for...of loop is used to iterate over the values of an iterable object (like an array).
Example:
let numbers: number[] = [1, 2, 3, 4];
for (let num of numbers) {
console.log(num);
}
5. for...inβ
The for...in loop is used to iterate over the keys of an object.
Example:
let person: { name: string, age: number } = { name: "Alice", age: 25 };
for (let key in person) {
console.log(key + ": " + person[key]);
}
6. whileβ
The while loop is used to execute a block of code as long as a specified condition is true.
Example:
let count: number = 0;
while (count < 5) {
console.log(count);
count++;
}
7. do...whileβ
The do...while loop is similar to the while loop, but it will execute the block of code at least once before checking the condition.
Example:
let count: number = 0;
do {
console.log(count);
count++;
} while (count < 5);
8. breakβ
The break statement is used to exit a loop or a switch statement.
Example:
for (let i = 0; i < 5; i++) {
if (i === 3) {
break;
}
console.log(i);
}
9. continueβ
The continue statement is used to skip the current iteration of a loop and continue with the next one.
Example:
for (let i = 0; i < 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}
10. try...catch...finallyβ
The try...catch...finally statement is used to handle exceptions and execute code regardless of whether an exception was thrown.
Example:
try {
let result: number = 10 / 0;
console.log(result);
} catch (error) {
console.log("An error occurred: " + error.message);
} finally {
console.log("This will always execute");
}
11. throwβ
The throw statement is used to create a custom error.
Example:
function divide(a: number, b: number): number {
if (b === 0) {
throw new Error("Division by zero");
}
return a / b;
}
try {
console.log(divide(10, 0));
} catch (error) {
console.log("An error occurred: " + error.message);
}