The basic statements of JavaScript are: 1. if selection statement; 2. switch multi-condition selection statement; 3. for loop statement; 4. while loop statement; 5. do-while loop statement; 6. break and continue jump statement.
## The operating environment of this article: windows7 system, javascript version 1.8.5 , Dell G3 computer.
1. if selection statementConditional statements are used to perform different actions based on different conditions.
·if() statement - Use this statement to execute code only when the specified condition is true
Standard expression:
if(){ }
·if()…else statement - when Execute code when the condition is true, execute other code when the condition is false
Standard expressions: if( ){ }else{ }
·if()…else if()…else statement - use this statement to Select one of multiple code blocks to execute
Standard expressions: if( ){ }else if( ){ } else{ };
·The expression in the if statement () will be automatically converted into a Boolean value.
2. Switch multi-condition selection statementUse the switch statement to select one of multiple code blocks to be executed. (Mostly used for fixed value analysis and judgment, easy to expand)
Standard syntax:
·The value of the expression is then compared with the value of each case in the structure.
·If there is a match, the code block associated with the case will be executed.
·Please use break to prevent the code from automatically running to the next case.
·default keyword to specify what to do when the match does not exist:
3. for loop statementIn programming, some instructions need to be executed many times, so loop statements are used.
The syntax of for loop:
·Statement 2 defines the conditions for running the loop (code block), if statement 2 If true is returned, the loop will start again, if false is returned, the loop will end.
·Statement 3 is executed after the loop (code block) has been executed
·Statement 1 is optional, which means that you can not use statement 1.
·If you omit statement 2, then a break must be provided within the loop. Otherwise the cycle cannot be stopped. This may crash the browser.
·Statement 3 is also optional.
Nesting of for statements:
4. While loopThe while loop tests a condition before execution. If Enter the loop when the condition is met.
The syntax of while loop:
The while loop tests a condition before execution, while the do-while loop first Execute the loop, and then test whether the condition is trueSyntax of do-while loop:
##6. Break and continue jump statements
break will jump out directly and end the current loop structure.continue is used to skip an iteration in the loop.The continue statement can only be used in a loop; break can only be used in a loop or switch.
The above is the detailed content of What are the basic statements of javascript. For more information, please follow other related articles on the PHP Chinese website!