JavaScript, if conditional statement

 Conditional statements

When writing code, often require different judgments based perform different actions;

You can do this using conditional statements in the code.

In JavaScript, we can use the following conditional statements:
1. if a predetermined block to be executed, if the specified condition is to true;
2. else to use a predetermined code block to be executed, if the same condition is to false;
. 3. else if a new use for a predetermined condition to be tested, if the first condition is to false;
4. using a switch to a predetermined plurality of candidate code blocks are executed.

 The if statement
single branch statement, composed by one if, if the condition is satisfied, then enter the code begins to execute the statement. grammar

if (condition) { 
    code is executed if the condition is true 
}

 Example:

Age = 20 is the let; 
IF (Age> = 18 is) { 
the console.log ( "Older"); 
}

  

else double branch statement
as the name suggests, there are two branch lines, if if does not hold, it will jump into the else statement inside.

if (condition) { 
    code block execution condition is true 
} else { 
    code block is executed when the condition is false 
}

Example:

Age = 16 the let; 
IF (Age> = 18 is) { 
the console.log ( "Older"); 
} {the else 
the console.log ( "underage"); 
}

  

else if multiple branching statements
plurality if ... else statement may be combined together to form the logical tree. grammar

{(Condition 1) IF 
    block of code is executed when Condition 1 is to true 
} {(Condition 2) else if 
    block is executed when condition 1 is false and condition 2 is to true 
 } the else { 
    conditions 1 and 2 are simultaneously performed is false code block 
}

 Note that in the multi-branch statement which, if entered into any of a block of statements, the latter condition will no longer be determined, but will directly jump.

 E.g:

let readline = require("readline-sync");
console.log("输入考试成绩:");
let score = readline.question("");
if(score > 100 || score < 0){
console.log("成绩输入有误?");
}else if(score >= 90){
console.log("优秀");
}else if(score >= 70){
console.log("良好");
}else if(score >= 60){
console.log("合格");
}else{
console.log("不合格");
}

  

Guess you like

Origin www.cnblogs.com/youwei716/p/11111027.html