Conditional statements are used to perform different actions based on different conditions. Determine the given condition, what code to execute if the condition is true, and what code to execute if the condition is false.

TypeScript in fact, conditional statements and JavaScript conditional statements, as if already familiar JavaScript conditional statements, you can skip this section.

if conditional statement

TypeScript conditional statement 061474975a3a72 in if consists of a Boolean expression and a code block.

Syntax format:

if(expression){
    // 当 expression 为 true 时执行代码块
}
Example:

Define a variable age , according to the given conditional expression, determine whether the user's age meets the requirements of playing the game:

var age:number = 22; 
// 给定一个条件
if(age >= 18){
    console.log("已满18岁, 可以玩游戏");
}

Compiled into JavaScript code:

var age = 22;
// 给定一个条件
if (age >= 18) {
    console.log("已满18岁, 可以玩游戏");
}

Output:

已满18岁, 可以玩游戏

if...else conditional statement

In if behind the conditional statement can take an optional else statement, when a given condition is true when executed if code block after the statement is false is executed else block following statements.

Syntax format:

if(expression){
    // 当 expression 为 true 时执行代码块
}else{
    // 当 expression 为 false 时执行
}
Example:

In the above example, we are given a condition, when the condition is met, if statement is executed, and the code will not be executed if the condition is not met. If we want to execute the specified code even when the conditions are not met, we can use the if...else conditional statement.

As shown below, when the conditions are met, the output is "18 years old, can play games", when the conditions are not met, the output is "Under adults, can not play games":

var age:number = 16; 
// 给定一个条件
if(age >= 18){
    console.log("已满18岁, 可以玩游戏");
}else{
    console.log("未成人, 不可以玩游戏");
}

Compile into JavaScript code:

var age = 16;
// 给定一个条件
if (age >= 18) {
    console.log("已满18岁, 可以玩游戏");
}
else {
    console.log("未成人, 不可以玩游戏");
}

Output:

未成人, 不可以玩游戏

else if conditional statement

if...else conditional statement mentioned above has another flaw, that is, it is not easy to judge when there are different conditions. For example, in the second-year exam, the teacher grades the students. Grades below 60 are graded as C, grades from 60 to 80 are graded as B, and grades above 80 are graded as A. In this case, we can use else if conditional statement.

else if statement if statement. There is a parenthesis () and the specified conditional expression in the parentheses, but the two are somewhat different in use:

  • There is generally only one if statement in a conditional statement, but any number of else if statements can be used.
  • else if statements generally if after the statement, else use before the statement.

Syntax format:

if(expression1){
    // 当 expression1 为 true 时执行代码块
}else if(expression2){
    // 当 expression2 为 true 时执行代码块
}else{
    // 当所有表达式都不满足时执行
}
Example:

Let's use else if achieve the rating mentioned above:

var score:number = 88; 

if(score < 60){
    console.log("成绩等级为C");
}else if( score >= 60 && score < 80){
    console.log("成绩等级为B");
}else{
    console.log("成绩等级为A");
}

Compiled into JavaScript code:

var score = 88;
if (score < 60) {
    console.log("成绩等级为C");
}
else if (score >= 60 && score < 80) {
    console.log("成绩等级为B");
}
else {
    console.log("成绩等级为A");
}

Output:

成绩等级为A

switch statement

switch statement to execute a statement from a plurality of conditions, the test value of an expression, and case a value of the matching, matching is performed successfully case block following statements.

Syntax format:

switch(expression){
    case constant-expression  :
       statement(s);
       break; 
    case constant-expression  :
       statement(s);
       break; 
    default : 
       statement(s);
}

A switch statement can have any number of case statement, when expression value with one of the constant-expression match, executes the corresponding code block.

break statement is used to terminate switch . If the case statement does not contain break case statement will continue to be executed until the code in break or switch

default is optional and is used to execute the code block when case And the default in break is not necessary.

Example:

When the value of the variable is 1, Monday is output, when it is 2, Tuesday is output, ... and so on:

var week:number = 5; 

switch(week) {
    case 1:{
        console.log("星期一");
        break;
    }   
    case 2:{
        console.log("星期二");
        break;
    }  
    case 3:{
        console.log("星期三");
        break;
    }  
    case 4:{
        console.log("星期四");
        break;
    }  
    case 5:{
        console.log("星期五");
        break;
    }  
    case 6:{
        console.log("星期六");
        break;
    }  
    case 7:{
        console.log("星期七");
        break;
    }  
    default :{
        console.log("错误");
        break;
    }
}

Compiled into JavaScript code:

var week = 5;
switch (week) {
    case 1: {
        console.log("星期一");
        break;
    }
    case 2: {
        console.log("星期二");
        break;
    }
    case 3: {
        console.log("星期三");
        break;
    }
    case 4: {
        console.log("星期四");
        break;
    }
    case 5: {
        console.log("星期五");
        break;
    }
    case 6: {
        console.log("星期六");
        break;
    }
    case 7: {
        console.log("星期七");
        break;
    }
    default: {
        console.log("错误");
        break;
    }
}

Output:

星期五

If we do not add break case statement, then after executing the case statement that meets the conditions, the following statement will continue to be executed.

Let's take a look:

var week = 5;
switch (week) {
    case 1: {
        console.log("星期一");
    }
    case 2: {
        console.log("星期二");
    }
    case 3: {
        console.log("星期三");
    }
    case 4: {
        console.log("星期四");
    }
    case 5: {
        console.log("星期五");
    }
    case 6: {
        console.log("星期六");
    }
    case 7: {
        console.log("星期七");
    }
    default: {
        console.log("错误");
    }
}

Output:

星期五
星期六
星期七
错误

Summarize

It should be relatively simple in the above several conditional statements. We need to note that both the if...else if...else statement and the switch statement are used to select one of multiple code blocks to execute. How to choose which statement to use in different situations?

  • switch statement is usually more efficient and logically clearer than a series of nested if It is recommended to use when judging a fixed value.
  • if statement is switch statement. Any Boolean expression can be used. It is recommended to use it when judging the interval or range.
  • In short, switch can do, if can do, but not vice versa, so if you are entangled in which sentence to use, you can choose if sentence, no matter what.

知否
221 声望177 粉丝

Skrike while the iron is hot.