Introduction
In this chapter, we will explore the while
loop in TypeScript. The while
loop is a control flow statement that allows you to execute a block of code as long as a specified condition is true. Understanding how to use the while
loop is essential for managing loops that require condition-based repetition in TypeScript programs.
Table of Contents
- Definition
- While Loop Syntax
- Basic While Loop
- Do-While Loop
- Nested While Loop
- Complete Example with Output
- Conclusion
Definition
The while
loop repeats a block of code as long as a specified condition is true. It is useful when the number of iterations is not known before entering the loop.
While Loop Syntax
Syntax
while (condition) {
// code to be executed
}
Example
This example demonstrates a basic while
loop that prints numbers from 0 to 4.
let i: number = 0;
while (i < 5) {
console.log(i);
i++;
}
Output
0
1
2
3
4
Basic While Loop
The basic while
loop continues to execute the block of code as long as the specified condition remains true.
Example
This example prints the first five odd numbers using a while
loop.
let num: number = 1;
while (num < 10) {
console.log(num);
num += 2;
}
Output
1
3
5
7
9
Do-While Loop
The do-while
loop is similar to the while
loop, but it guarantees that the code block will be executed at least once before the condition is tested.
Syntax
do {
// code to be executed
} while (condition);
Example
This example demonstrates a do-while
loop that prints numbers from 0 to 4.
let i: number = 0;
do {
console.log(i);
i++;
} while (i < 5);
Output
0
1
2
3
4
Nested While Loop
A nested while
loop is a while
loop inside another while
loop. It allows for more complex iterations.
Example
This example prints a multiplication table for numbers 1 through 3 using nested while
loops.
let i: number = 1;
while (i <= 3) {
let j: number = 1;
while (j <= 3) {
console.log(`${i} * ${j} = ${i * j}`);
j++;
}
i++;
}
Output
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
Complete Example with Output
In this section, we will combine all 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:
// Basic While Loop
let i: number = 0;
while (i < 5) {
console.log(i); // Output: 0, 1, 2, 3, 4
i++;
}
// Do-While Loop
i = 0;
do {
console.log(i); // Output: 0, 1, 2, 3, 4
i++;
} while (i < 5);
// Nested While Loop
i = 1;
while (i <= 3) {
let j: number = 1;
while (j <= 3) {
console.log(`${i} * ${j} = ${i * j}`); // Output: 1 * 1 = 1, 1 * 2 = 2, ... , 3 * 3 = 9
j++;
}
i++;
}
Conclusion
In this chapter, we covered the while
loop in TypeScript, including the basic while
loop, do-while
loop, and nested while
loop. We provided a complete example with its output to illustrate how these loops work in TypeScript. Understanding the while
loop is essential for managing loops that require condition-based repetition in TypeScript programs.
Comments
Post a Comment
Leave Comment