


Discussion on the execution order of the three parts separated by semicolons in the For loop_javascript skills
What triggered this question was the running result of a js program:
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.
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,
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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: #

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

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." ";}".

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 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

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:

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.

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.
