TypeScript Conditionals Loops and Jumps
In TypeScript, program control structures are essentially the same as in JavaScript, including conditional statements, loop statements, and jump statements.
Conditional statements are used to execute different code blocks based on conditions. Common conditional statements in TypeScript include if statements and switch statements.
if Statement
The if statement executes different code blocks based on a condition. The syntax is as follows:
if (condition) {
// code block to be executed if the condition is true
} else {
// code block to be executed if the condition is false
}
For example:
let num: number = 42;
if (num > 50) {
console.log("The number is greater than 50.");
} else if (num < 50) {
console.log("The number is less than 50.");
} else {
console.log("The number is equal to 50.");
}
switch Statement
The switch statement executes different code blocks based on different values. The syntax is as follows:
switch (expression) {
case value1:
// code block to be executed if expression matches value1
break;
case value2:
// code block to be executed if expression matches value2
break;
...
default:
// code block to be executed if expression doesn't match any value
}
For example:
let num: number = 2;
switch (num) {
case 1:
console.log("The number is one.");
break;
case 2:
console.log("The number is two.");
break;
case 3:
console.log("The number is three.");
break;
default:
console.log("The number is not one, two, or three.");
}
Loop Statements
Loop statements are used to repeat the execution of a block of code. Common loop statements in TypeScript include for loops, while loops, and do-while loops.
for Loop
A for loop is used to repeat the execution of a block of code, allowing control over the number of iterations and the loop condition. The syntax is as follows:
for (initialization; condition; increment) {
// code block to be executed
}
For example:
for (let i: number = 0; i < 5; i++) {
console.log(i);
}
while Loop
A while loop is used to repeat the execution of a block of code as long as the condition remains true. The syntax is as follows:
while (condition) {
// code block to be executed
}
For example:
let i: number = 0;
while (i < 5) {
console.log(i);
i++;
}
do-while Loop
A do-while loop is used to repeat the execution of a block of code, executing the block once first and then continuing as long as the condition remains true. The syntax is as follows:
do {
// code block to be executed
} while (condition);
For example:
let i: number = 0;
do {
console.log(i);
i++;
} while (i < 5);
Jump Statements
Jump statements are used to jump to a specified location within a program. Common jump statements in TypeScript include break statements, continue statements, and return statements.
break Statement
The break statement is used to exit from a loop or a switch statement. For example:
for (let i: number = 0; i < 5; i++) {
if (i == 3) {
break;
}
console.log(i);
}
continue Statement
The continue statement is used to skip a specific iteration within a loop. For example:
for (let i: number = 0; i < 5; i++) {
if (i == 3) {
continue;
}
console.log(i);
}
return Statement
The return statement is used to return a value from a function and end the execution of that function. For example:
function add(a: number, b: number): number {
return a + b;
}
console.log(add(2, 3)); // 5