TypeScript Conditional Statements
Conditional statements are used to perform different actions based on different conditions.
TypeScript conditional statements determine which code block to execute based on the result (True or False) of one or more statements.
The following diagram gives a simple view of how conditional statements execute:

Conditional statement
When writing code, you often need to perform different actions for different decisions. You can use conditional statements in your code to accomplish this.
In TypeScript, we can use the following conditional statements:
- if statement - Use this statement to execute code only when the specified condition is true
- if...else statement - Execute code when the condition is true, and other code when the condition is false
- if...else if....else Statement- Use this statement to select one of several blocks of code to execute
- switch statement - Use this statement to select one of several blocks of code to execute
if statement
A TypeScript if statement consists of a boolean expression followed by one or more statements.
Syntax
The syntax format is as follows:
if(boolean_expression){
# Execute if boolean_expression is true
}
If the boolean expression boolean_expression is true, the code block inside the if statement is executed. If the boolean expression is false, the first group of code after the if statement (after the closing brace) is executed.
Flowchart

Example
var
num
:
number
=
5
if
(
num
0
)
{
console
.
log
(
"
Number is positive
"
)
}
Compiling the above code yields the following JavaScript code:
var
num
=
5
;
if
(
num
0
)
{
console
.
log
(
"
Number is positive
"
)
;
}
Executing the above JavaScript code produces the following output:
数字是正数
if...else statement
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
Syntax
The syntax format is as follows:
if(boolean_expression){
# Execute if boolean_expression is true
}else{
# Execute when boolean_expression is false
}
If the boolean expression boolean_expression is true, the code inside the if block is executed. If the boolean expression is false, the code inside the else block is executed.
Flowchart

Example
TypeScript
var
num
:
number
=
12
;
if
(
num
%
2
==
0
)
{
console
.
log
(
"
Even
"
)
;
}
else
{
console
.
log
(
"
Odd
"
)
;
}
Compiling the above code yields the following JavaScript code:
JavaScript
var
num
=
12
;
if
(
num
%
2
==
0
)
{
console
.
log
(
"
Even
"
)
;
}
else
{
console
.
log
(
"
Odd
"
)
;
}
Executing the above JavaScript code produces the following output:
偶数
if...else if....else Statement
The if...else if....else statement is useful when executing multiple conditional checks.
Syntax
The syntax format is as follows:
if(boolean_expression 1) {
# Execute if boolean_expression 1 is true
} else if( boolean_expression 2) {
# Execute if boolean_expression 2 is true
} else if( boolean_expression 3) {
# Execute if boolean_expression 3 is true
} else {
# Executes when all conditions of the boolean expression are false
}
Note the following points:
-
A if A conditional statement can have 0 or 1 else statements, which must come after the else..if statement.
-
A if A conditional statement can have 0 or more else..ifstatements, which must appear after the else before.
-
Once the code inside else..if has been executed, the subsequent else..if or else will no longer be executed.
Example
TypeScript
var
num
:
number
=
2
if
(
num
0
)
{
console
.
log
(
num
"
is positive
"
)
}
else
if
(
num
<
0
)
{
console.
log
(
num
"
is negative
"
)
}
else
{
console
.
log
(
num
"
is neither positive nor negative
"
)
}
Compiling the above code yields the following JavaScript code:
JavaScript
var
num
=
2
;
if
(
num
0
)
{
console
.
log
(
num
"
is positive
"
)
;
}
else
if
(
num
<
0
)
{
console.
log
(
num
"
is negative
"
)
;
}
else
{
console
.
log
(
num
"
is neither positive nor negative
"
)
;
}
Executing the above JavaScript code produces the following output:
2 是正数
The switch…case statement
A switch The statement allows testing a variable against multiple values. Each value is called a case, and the tested variable is checked against each switch case .
switch statement syntax:
switch
(
expression
)
{
case
constant
expression
:
statement
(
s
)
;
break
;
/*
Optional
*/
case
constant
expression
:
statement
(
s
)
;
break
;
/*
Optional
*/
/*
You can have any number of case statements
*/
default
:
/*
Optional
*/
statement
(
s
)
;
}
switch statements must follow these rules:
- switch The expression is an expression to be compared and can be of any type, including primitive types (such as number, string, boolean), object types (such as object, Array, Map), and custom types (such as class, interface, enum).
- There can be any number of case statements in a switch. Each case is followed by a value to compare and a colon.
- The case constant-expression It must have the same or a compatible data type as the variable expression in switch.
- When the tested variable equals the constant in the case, the statements following the case are executed until break is encountered.
- When break is encountered, the switch terminates and control flow jumps to the line after the switch statement.
- Not every case needs to contain break. If a case statement does not contain break, control flow will continue to the following cases until a break is encountered.
- A switch statement can have an optional default case appears at the end of switch. The default keyword denotes the code block executed when the expression's value matches no case value. In the default case, break statement is not required.
Flowchart
Example
TypeScript
var
grade
:
string
=
"
A
"
;
switch
(
grade
)
{
case
"
A
"
:
{
console
.
log
(
"
Excellent
"
)
;
break
;
}
case
"
B
"
:
{
console
.
log
(
"
Good
"
)
;
break
;
}
case
"
C
"
:
{
console
.
log
(
"
Pass
"
)
;
break
;
}
case
"
D
"
:
{
console
.
log
(
"
Fail
"
)
;
break
;
}
default
:
{
console
.
log
(
"
Invalid input
"
)
;
break
;
}
}
Compiling the above code yields the following JavaScript code:
JavaScript
var
grade
=
"
A
"
;
switch
(
grade
)
{
case
"
A
"
:
{
console
.
log
(
"
Excellent
"
)
;
break
;
}
case
"
B
"
:
{
console
.
log
(
"
Good
"
)
;
break
;
}
case
"
C
"
:
{
console
.
log
(
"
Pass
"
)
;
break
;
}
case
"
D
"
:
{
console
.
log
(
"
Fail
"
)
;
break
;
}
default
:
{
console
.
log
(
"
Invalid input
"
)
;
break
;
}
}
Executing the above JavaScript code produces the following output:
优