Home  >  Article  >  Web Front-end  >  Detailed explanation of for loop and if judgment statement in JavaScript

Detailed explanation of for loop and if judgment statement in JavaScript

黄舟
黄舟Original
2017-10-11 10:17:114222browse

The following editor will bring you a JavaScript for loop if judgment statement (study notes). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look

Today I learned the for loop and if judgment statement in JavaScript

for(initial value; loop condition; operation){

Code statement to be executed if the condition is met
}

Initial value: initialized variable before loop, Usually assignment expression: It is recommended to use var assignment to speed up the operation.

Loop condition: The condition to be calculated before each loop is a conditional operator in the operator category. The return value is true or false. When the return value is true, the loop is executed, and when it is false, the loop is exited. (Often i>=n; or logical operators)

Operation: The expression to be calculated after each loop is usually an assignment expression such as increment ++ or decrement --.

The three parameters in the for statement, the first is an initialization variable, used to set the initial value of the number of loops; the second is a conditional operator or logical operator, used to determine whether the loop continues; The third is the statement executed after each loop, which actually increments or decrements the initialized variable.

if(condition){
Code executed when the condition is true
}

if statement: only when the specified condition is When true, use this statement to execute code
if...else statement: Execute code when the condition is true, execute other code when the condition is false
if...else if....else statement : Use this statement to select one of multiple code blocks to execute
switch statement: Use this statement to select one of multiple code blocks to execute

switch and if..else execution efficiency issues

if...else is used the same as switch, but the difference between the two is the issue of processing efficiency.

Generally, switch can be used,

But for example, in the case of if (a > 1 && a < 100), you can only use if.

The above is the detailed content of Detailed explanation of for loop and if judgment statement in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn