TypeScript Loops
Sometimes we may need to execute the same block of code multiple times. Normally, statements are executed sequentially: the first statement in a function executes first, followed by the second, and so on.
Programming languages provide various control structures for more complex execution paths.
Loop statements allow us to execute a statement or group of statements multiple times. The following is a flowchart of a loop statement in most programming languages:

for loop
The TypeScript for loop is used to execute a sequence of statements multiple times, simplifying the code that manages loop variables.
Syntax
The syntax format is as follows:
for ( init; condition; increment ){
statement(s);
}
The following is an analysis of the for loop's control flow:
- init is executed first and only once. This step allows you to declare and initialize any loop control variables. You may also write no statement here, as long as a semicolon appears.
- Next, the condition. If true, the loop body is executed. If false, the loop body is not executed, and control flows to the next statement immediately following the for loop.
- After executing the for loop body, control flow jumps back to the above increment statement. This statement allows you to update the loop control variable. This statement can be left empty, as long as a semicolon appears after the condition.
- The condition is evaluated again. If true, the loop executes, and this process repeats (loop body, then increment the step value, then re-evaluate the condition). When the condition becomes false, the for loop terminates.
Here, statement(s) can be a single statement or a block of several statements.
condition can be any expression; the loop executes when the condition is true and exits when the condition is false.
Flowchart

Example
The following example calculates the factorial of 5; the for loop generates the numbers from 5 down to 1 and computes the product at each iteration.
TypeScript
var
num
:
number
=
5
;
var
i
:
number
;
var
factorial
=
1
;
for
(
i
=
num
;
i
=
1
;
i
--
)
{
factorial
*=
i
;
}
console
.
log
(
factorial
)
Compiling the above code yields the following JavaScript code:
JavaScript
var
num
=
5
;
var
i
;
var
factorial
=
1
;
for
(
i
=
num
;
i
=
1
;
i
--
)
{
factorial
*=
i
;
}
console
.
log
(
factorial
)
;
Executing the above JavaScript code produces the following output:
120
for...in loop
The for...in statement is used to iterate over a collection or list of values.
Syntax
The syntax format is as follows:
for (var val in list) {
//语句
}
val must be of type string or any.
Example
TypeScript
var
j
:
any
;
var
n
:
any
=
"
a b c
"
for
(
j
in
n
)
{
console
.
log
(
n
[
j
]
)
}
Compiling the above code yields the following JavaScript code:
JavaScript
var
j
;
var
n
=
"
a b c
"
;
for
(
j
in
n
)
{
console
.
log
(
n
[
j
]
)
;
}
Executing the above JavaScript code produces the following output:
a
b
c
for…of, forEach, every, and some loops
In addition, TypeScript also supports for…of, forEach, every, and some loops.
The for...of statement creates a loop to iterate over iterable objects. Introduced in ES6, the for...of loop replaces for...in and forEach() and supports the new iteration protocol. for...of lets you traverse iterable data structures such as Arrays, Strings, Maps, and Sets.
TypeScript for...of Loop
let
someArray
=
[
1
,
"
string
"
,
false
]
;
for
(
let
entry
of
someArray
)
{
console
.
log
(
entry
)
;
//
1, "string", false
}
forEach, every, and some are JavaScript loop syntax; as a superset of JavaScript, TypeScript naturally supports them by default.
Because forEach cannot return a value during iteration, you can use every and some in place of forEach.
TypeScript forEach Loop
let
list
=
[
4
,
5
,
6
]
;
list
.
forEach
(
(
val
,
idx
,
array
)
=>
{
//
val: current value
//
idx: current index
//
array: Array
}
)
;
TypeScript every Loop
let
list
=
[
4
,
5
,
6
]
;
list
.
every
(
(
val
,
idx
,
array
)
=>
{
//
val: current value
//
idx: current index
//
array: Array
return
true
;
//
Continues
//
Return false will quit the iteration
}
)
;
while loop
The while statement repeats a statement or group of statements while a given condition is true. The condition is tested before the loop body executes.
Syntax
The syntax format is as follows:
while(condition)
{
statement(s);
}
Here, statement(s) can be a single statement or a block of several statements.
condition can be any expression; the loop executes when the condition is true. When the condition is false, program flow exits the loop.
Flowchart

In the diagram,while The key point of the loop is that it may not execute even once. When the condition is false, the loop body is skipped and execution proceeds directly to the statement immediately after the while loop.
Example
TypeScript
var
num
:
number
=
5
;
var
factorial
:
number
=
1
;
while
(
num
=
1
)
{
factorial
=
factorial
num
;
num
--;
}
console
.
log
(
"
The factorial of 5 is:
"
factorial
)
;
Compiling the above code yields the following JavaScript code:
JavaScript
var
num
=
5
;
var
factorial
=
1
;
while
(
num
=
1
)
{
factorial
=
factorial
num
;
num
--;
}
console
.
log
(
"
The factorial of 5 is:
"
factorial
)
;
Executing the above JavaScript code produces the following output:
5 的阶乘为:120
do...while loop
Unlike for and while loops, which test the loop condition at the top of the loop.do...while The do...while loop checks its condition at the bottom of the loop.
Syntax
The syntax format is as follows:
do
{
statement(s);
}while( condition );
Note that the condition expression appears at the bottom of the loop, so the statement(s) in the loop execute at least once before the condition is tested.
If the condition is true, control flows back to the do above and the statement(s) in the loop are executed again. This process repeats until the given condition becomes false.
Flowchart

Example
TypeScript
var
n
:
number
=
10
;
do
{
console
.
log
(
n
)
;
n
--;
}
while
(
n
=
0
)
;
Compiling the above code yields the following JavaScript code:
JavaScript
var
num
=
5
;
var
n
=
10
;
do
{
console
.
log
(
n
)
;
n
--;
}
while
(
n
=
0
)
;
Executing the above JavaScript code produces the following output:
10
9
8
7
6
5
4
3
2
1
0
break statement
break statement has the following two uses:
- When break When a statement appears inside a loop, the loop terminates immediately, and program flow continues with the next statement immediately following the loop.
- It can be used to terminate a switch case within a statement.
If you are using nested loops (i.e., one loop inside another), the break statement stops executing the innermost loop and then begins executing the next line of code after that block.
Syntax
The syntax format is as follows:
break;
Flowchart

Example
TypeScript
var
i
:
number
=
1
while
(
i
<=
10
)
{
if
(
i
%
5
==
0
)
{
console
.
log
(
"
The first number between 1 and 10 divisible by 5 is:
"
i
)
break
//
Exit the loop after finding one
}
i
++
}
//
Output 5, then the program finishes executing
Compiling the above code yields the following JavaScript code:
JavaScript
var
i
=
1
;
while
(
i
<=
10
)
{
if
(
i
%
5
==
0
)
{
console
.
log
(
"
The first number between 1 and 10 divisible by 5 is:
"
i
)
;
break
;
//
Exit the loop after finding one
}
i
++;
}
//
Output 5, then the program finishes executing
Executing the above JavaScript code produces the following output:
在 1~10 之间第一个被 5 整除的数为 : 5
continue statement
continue continue statement in C++ is somewhat like the break statement. But it is not a forced termination; continue skips the code in the current loop and forces the start of the next iteration.
For for a for loop, thecontinue After the statement executes, the increment statement still executes. For while and do...while a for loop, thecontinue statements, the condition statement is re-executed.
Syntax
The syntax format is as follows:
continue;
Flowchart

Example
TypeScript
var
num
:
number
=
0
var
count
:
number
=
0
;
for
(
num
=
0
;
num
<=
20
;
num
++
)
{
if
(
num
%
2
==
0
)
{
continue
}
count
++
}
console
.
log
(
"
The number of odd numbers between 0 and 20 is:
"
count
)
//
Output 10 even numbers
Compiling the above code yields the following JavaScript code:
JavaScript
var
num
=
0
;
var
count
=
0
;
for
(
num
=
0
;
num
<=
20
;
num
++
)
{
if
(
num
%
2
==
0
)
{
continue
;
}
count
++;
}
console
.
log
(
"
The number of odd numbers between 0 and 20 is:
"
count
)
;
//
Outputs 10
Executing the above JavaScript code produces the following output:
0 ~20 之间的奇数个数为: 10
Infinite loop
An infinite loop is a loop that keeps running without stopping. Both for and while loops can create infinite loops.
Syntax for creating an infinite loop with for:
for(;;) {
// 语句
}
Example
for(;;) {
console.log("这段代码会不停的执行")
}
Syntax for creating an infinite loop with while:
while(true) {
// 语句
}
Example
while(true) {
console.log("这段代码会不停的执行")
}