Home >Web Front-end >JS Tutorial >Detailed explanation of JavaScript for loop if judgment statement

Detailed explanation of JavaScript for loop if judgment statement

小云云
小云云Original
2018-01-25 11:32:482476browse

This article mainly brings 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, I hope it can help everyone.

for (initial value; loop condition; operation) {

Code statement to be executed if the condition is met
}

Initial value: initialized variable before loop, usually For assignment expressions: It is recommended to use var for assignment, which can 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: Use this statement to execute the code only when the specified condition is true
if...else statement: Execute code when the condition is true, and execute other code when the condition is false
if...else if...else statement: Use this statement to select one of multiple blocks of code To execute the
switch statement: Use this statement to select one of multiple code blocks to execute

Efficiency issues in the execution of switch and if..else

if...else follows The usage of switch is the same, but the difference between the two is the issue of processing efficiency.

Under normal circumstances, you can use switch,

But for example, if (a > 1 && a < 100) In this case, you can only use if.

Related recommendations:

Four kinds of for loops in js

##JavaScript for loop understanding of if judgment statements

Advanced for loop writing method

The above is the detailed content of Detailed explanation of JavaScript for loop if judgment statement. 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