Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

TypeScript Conditional Statements

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

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

if Statement in Perl

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:

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:

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:

优