Javascript based learning (four) js statement

Because the program is executed from the top-down order, the process control statements may be changed by the program execution sequence, a certain period or repeatedly executed program.

Category statement

  1. Conditional statement

  2. Conditional branch statement

  3. loop statement

Conditional statement

Also known as conditional statement if statement

A grammar:

if(条件表达式){
    语句...
}       

Execution flow: if statement executed, first the condition expression is evaluated to determine if the value is true, if the statement is executed, if the value is false, is not performed.

Grammar II:

if(条件表达式){
    语句...
}else{
    语句...
}

Execution flow: if ... else statement when executed, will determination condition expression is evaluated, if the value is true, if the statement is executed, if the value is false, the statement is executed else

Grammar III:

if(条件表达式){
    语句...
}else if(条件表达式){
    语句...
}else if(条件表达式){
    语句...
}else if(条件表达式){
    语句...
}else{
    语句...
}

Flow of execution: if ... else if ... else statement when executed, will automatically turn on conditional expressions are evaluated to determine top to bottom,

     如果判断结果为true,则执行当前if后的语句,执行完成后语句结束。

 如果判断结果为false,则继续向下判断,直到找到为true的为止。

 如果所有的条件表达式都是false,则执行else后的语句
    

Conditional branch statement

Using a switch statement

grammar:

switch(条件表达式){
    case 表达式:
        语句...
        break;
    case 表达式:
    语句...
    break;
    case 表达式:
    语句...
    break;
    default:
    语句...
    break;
}
    

Flow of execution: switch ... case ... statement is executed, will be followed by the value of the expression of the value of the switch expression and the case be congruent comparison,

 如果比较结果为false,则继续向下比较。如果比较结果为true,则从当前case处开始向下执行代码。

 如果所有的case判断结果都为false,则从default处开始执行代码。

    

loop statement

Some statements may execute multiple times by repeatedly loop

while loop syntax:

while(条件表达式){
    语句...
}
        

Flow of execution: while the statement is executed, will first conditional expressions are evaluated to determine,

 如果判断结果为false,则终止循环

 如果判断结果为true,则执行循环体

     循环体执行完毕,继续对条件表达式进行求值判断,依此类推
            

do ... while loop syntax:

do{
    语句...
}while(条件表达式)
            

Flow of execution: do ... while at the time of execution, will first iteration of the loop after do, and then to judge the conditional expression,

 如果判断判断结果为false,则终止循环。

 如果判断结果为true,则继续执行循环体,依此类推
                

And while the difference: while: After the first judgment execution

      do...while: 先执行后判断

          do...while可以确保循环体至少执行一次。
            
            

for loop syntax:

for(①初始化表达式 ; ②条件表达式 ; ④更新表达式){
    ③语句...
}

Implementation process: ① initialization expression is executed first, initialize a variable,

 然后对②条件表达式进行求值判断,如果为false则终止循环

 如果判断结果为true,则执行③循环体

 循环体执行完毕,执行④更新表达式,对变量进行更新。

 更新表达式执行完毕重复②
            

Endless loop

while(true){
        
}
        
for(;;){
        
}

Guess you like

Origin www.cnblogs.com/Yee-Q/p/12318254.html