Introduction
In this chapter, we will explore the continue
statement in TypeScript. The continue
statement is used to skip the rest of the code inside a loop for the current iteration and move to the next iteration. Understanding how to use the continue
statement is essential for managing loop iterations in TypeScript programs.
Table of Contents
- Definition
- Continue Statement Syntax
- Using Continue in For Loops
- Using Continue in While Loops
- Nested Loops with Continue
- Complete Example with Output
- Conclusion
Definition
The continue
statement skips the rest of the code inside a loop for the current iteration and proceeds with the next iteration of the loop.
Continue Statement Syntax
Syntax
continue;
Example
This example demonstrates using the continue
statement to skip printing the number 3 in a loop.
for (let i = 0; i < 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}
Output
0
1
2
4
Using continue Statement in for Loop
The continue
statement can be used in a for
loop to skip the current iteration when a specified condition is met.
Example
This example uses the continue
statement to skip printing even numbers in a for
loop.
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) {
continue; // Skip even numbers
}
console.log(i);
}
Output
1
3
5
7
9
Using continue Statement in while Loop
The continue
statement can also be used in a while
loop to skip the rest of the code inside the loop for the current iteration.
Example
This example uses the continue
statement to skip printing the number 3 in a while
loop.
let num: number = 0;
while (num < 5) {
num++;
if (num === 3) {
continue; // Skip the rest of the loop when num equals 3
}
console.log(num);
}
Output
1
2
4
5
Nested Loops with continue Statement
The continue
statement can be used in nested loops to control the flow of iterations in both the inner and outer loops.
Example
This example demonstrates using the continue
statement in nested loops to skip printing the product of numbers when the inner loop index is 2.
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (j === 2) {
continue; // Skip the inner loop iteration when j equals 2
}
console.log(`${i} * ${j} = ${i * j}`);
}
}
Output
1 * 1 = 1
1 * 3 = 3
2 * 1 = 2
2 * 3 = 6
3 * 1 = 3
3 * 3 = 9
Complete Example with Output
In this section, we will combine the examples into a single TypeScript file, compile it to JavaScript, and run it to see the output.
TypeScript Code
You can test the following code in the TypeScript Playground:
// Using Continue in For Loops
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) {
continue; // Skip even numbers
}
console.log(i); // Output: 1, 3, 5, 7, 9
}
// Using Continue in While Loops
let num: number = 0;
while (num < 5) {
num++;
if (num === 3) {
continue; // Skip the rest of the loop when num equals 3
}
console.log(num); // Output: 1, 2, 4, 5
}
// Nested Loops with Continue
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (j === 2) {
continue; // Skip the inner loop iteration when j equals 2
}
console.log(`${i} * ${j} = ${i * j}`); // Output: 1 * 1 = 1, 1 * 3 = 3, ... , 3 * 3 = 9
}
}
Conclusion
In this chapter, we covered the continue
statement in TypeScript, including how to use it in for
loops, while
loops, and nested loops. We provided a complete example with its output to illustrate how the continue
statement works in TypeScript. Understanding the continue
statement is essential for managing loop iterations in TypeScript programs.
Comments
Post a Comment
Leave Comment