Home Web Front-end JS Tutorial Discussion on the execution order of the three parts separated by semicolons in the For loop_javascript skills

Discussion on the execution order of the three parts separated by semicolons in the For loop_javascript skills

May 16, 2016 pm 04:46 PM
for loop semicolon Execution order

What triggered this question was the running result of a js program:

Copy code The code is as follows:

var i = 0;
function a(){
for(i=0;i<20;i ){
}
}
function b(){
for (i=0;i<3;i ){
a();
}
return i;
}
var Result = b();

The running result of this program is Result = 21;

From this program we can see that the value of i is 20 when function a returns, which is no problem.
When the b function returns, whether the value of i is 20 or 21 is worthy of discussion.
The essence of the problem is: should i be judged first, i<3, or i first, and then judge whether i<3.

According to the execution results, it can be seen that i was executed first.
Copy code The code is as follows:

function a(){
for(i= 0;i<20;i ){
// There is no var i
//The i here is a global variable that everyone can access
}
}
function b(){
for(i=0;i<3;i ){
//alert(i);//Similarly, i here is also a global variable, returning 0 and only once
a();// The return value of this function is i=20
//When i=20 passes through i and then i=21, then the condition of i<3 is not met and exits directly. So return i=21 which is normal!
}
return i;
}
var Result = b();

Here we complete the execution sequence of the for loop:
With the following program For example,
Copy code The code is as follows:

for(int i=0;i<10 ;i )
{
}

First execute i=0;i<10; and then execute the first round of the loop
and then execute: i ,i<10; then Execute the second round of loop body
until i >= 10 after the last i, at which point the loop ends.

That is,

statement 1 is executed before the loop (code block) starts.

statement 2 defines the conditions for running the loop (code block).

statement 3 is in Executed after the loop (block of code) has been executed
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C++ syntax error: The statement is missing a semicolon, how to correct it? C++ syntax error: The statement is missing a semicolon, how to correct it? Aug 22, 2023 am 09:57 AM

C++ is a very powerful programming language, but when writing code, you will inevitably encounter syntax errors. Among them, missing semicolons in statements is one of the common errors. In this article, we will discuss the situation when a statement is missing a semicolon and provide solutions. What is a statement missing a semicolon? In C++ programs, each statement usually ends with a semicolon (;). The semicolon tells the compiler that the current statement has reached the end. If you forget to add a semicolon at the end of a statement, the compiler will report a syntax error. For example, the following code will cause a syntax error: #

PHP Practical Tip: Remove the last semicolon in your code PHP Practical Tip: Remove the last semicolon in your code Mar 27, 2024 pm 02:24 PM

Practical PHP Tips: Delete the Last Semicolon in the Code When writing PHP code, you often encounter situations where you need to delete the last semicolon in the code. This may be because copy-pasting introduces extra semicolons, or to optimize code style and structure. In this article, we will introduce some methods to remove the last semicolon in PHP code and provide specific code examples. Method 1: Use the substr function The substr function can return a substring of a specified length from a string. we can

How to use php to find odd numbers within 100 How to use php to find odd numbers within 100 Dec 23, 2022 pm 06:54 PM

Implementation steps: 1. Use the for statement control range to traverse the numbers from 1 to 100, the syntax is "for ($i = 1; $i <= 100; $i++) {loop body code}"; 2. In the loop body, Just use the if statement and the "%" operator to obtain and output odd numbers. The syntax is "if($i % 2 != 0){echo $i." ";}".

What is the execution order of for loop in PHP What is the execution order of for loop in PHP Sep 22, 2021 pm 06:24 PM

Execution sequence: 1. Execute the "initialization expression"; 2. Execute the "conditional judgment expression". If the value of the expression is true, execute the "loop body", otherwise the loop ends; 3. After executing the loop body, execute "Variable update expression"; 4. After the variable is updated, enter the next loop until the condition judgment value is false, ending the loop.

PHP programming tips: How to deal with the last semicolon situation PHP programming tips: How to deal with the last semicolon situation Mar 26, 2024 pm 12:45 PM

PHP Programming Tips: How to Handle the Last Semicolon In PHP programming, you often encounter situations where you need to handle the last semicolon. Especially in loops and conditional statements, it is easy to cause program errors by writing one less or one more semicolon. In order to avoid this situation, we can adopt some programming techniques to handle the last semicolon situation. The following are some common techniques and code examples for handling the last semicolon: 1. Use if statements to determine the last semicolon

PHP code: How to remove the last semicolon PHP code: How to remove the last semicolon Mar 26, 2024 pm 02:54 PM

Title: PHP code: How to remove the last semicolon When writing PHP code, sometimes an extra semicolon will be added accidentally, resulting in an error. But what if you want to remove the last semicolon? Several methods and specific code examples will be introduced below. Method 1: Use the substr function. You can use PHP's substr function to remove the last semicolon. Specific code examples are as follows:

What is the relationship between loading order and execution order of PHP functions? What is the relationship between loading order and execution order of PHP functions? Apr 18, 2024 am 08:45 AM

The loading order of PHP functions is determined by the declaration order, including functions defined in the script, included files, and functions imported into the namespace; the execution order is determined by the order of function calls, and the function calls in the script are executed in sequence.

Does mysql have a for loop? Does mysql have a for loop? Mar 30, 2023 pm 08:26 PM

MySQL does not have a for loop. MySQL does not support for loop statements. It only supports three loop statements: WHILE, REPEAT and LOOP. MySQL provides loop statements, allowing you to repeatedly execute a SQL code block based on conditions.

See all articles