Loop statement 1. While
Grammar:
while (exp) {
statements;
}
Description
while (variable <= end value) {code to be executed}
Example 1:
var i = 1;
while (I < 3) {
alert (i);
i++;
}
Note: If you forget to increase the value of the variable used in the condition, the loop never ends. This may cause the browser to crash.
(
Using the while output 1-1000 value
var i=1; while (i<=1000) { if(i%2) { document.write (i); // The output is 1-1000 odd, i%2==1 indicates true output, i%2==0 indicates false does not output. Else { document.write (i); } // an even number of output 1-1000 i++; }
)
2. Do...while
do {
statements;
}while (condition);
Do {code to be executed} while (variable <= end value)
Cases:
var i = 1;
do {
alert (i);
i++;
}while (I < 3);
Note: Here is basically the reverse version of the previous while example, with only one difference: the Do loop detects the condition at the end of the loop. Therefore, even if the condition is never true, the statement in the loop will run at least once.
3.for Loop statement
for (initial; expression; post-loop-expression) {
Statement
}
for (variable = start value (statement 1), variable <= end value (statement 2), variable = variable + Step value (statement 3)) {
Code to be executed
}
Execute starts before the start of statement 1 (code block).
Statement 2 defining conditions for running loops (code blocks)
Statement 3 executes after a loop (code block) has been executed
Cases:
for (var count = 1; count < one; count++) {
alert (count);
}
4.for/in Loop Statements
The JavaScript for/in statement loops through the properties of the object:
Cases:
var person={fname: "John", lname: "Doe", age:25};
for (x in person)
{
Txt=txt + person[x];
}
(- -! Less use, simply take notes)
Different types of loops
JavaScript supports different types of loops:
- for-loop code block for a certain number of times
- for/in -Looping through the properties of an object
- While-loops the specified block of code when the specified condition is true
- Do/while -also loops the specified block of code when the specified condition is true
5. Break statement and Continue statement
The break statement can exit the loop immediately;
The continue statement simply exits the current loop;
5.1break statements
The break statement can be used to jump out of a loop.
After the break statement jumps out of the loop, the code after the loop resumes (if any):
for (i=0;i<10;i++) {
if (i==3) {
Break
}
X=x + "The number is" + i + "<br>";
}
5.2continue Statements
The Continue statement interrupts the iteration in the loop, and if a specified condition occurs and then proceeds to the next iteration in the loop, the following example skips the value 3:
for (i=0;i<=10;i++) {
if (i==3) continue;
X=x + "The number is" + i + "<br>";
}
Function
A function is a set of statements that completes a function that is defined by a keyword function + function name + a set of parameters ;
Functions can be called repeatedly after they are defined, often written as a function, using functions that enable the organization of code
Structure more clearly.
Note: Function names are case-sensitive.
Basic syntax:
function Funname (arg0, arg1, ... ArgN) {
Statements
}
Example 1:
function sum (i, j) {
alert (I+J);
}
sum (2,3);
Example 2: Returning a value from a function using the return statement
function sum (NUM1, num2) {
return NUM1 + num2;
}
var s = SUM (1, 2);
alert (s);
document.write (s);
Console.log (s);
The function sum can be called multiple times here without repeating the write!
Attention:
1. Any code that is behind the return statement will never be executed!
2. In JavaScript, you don't have to specify a return value!
3. When defining the parameters of a function, if the function is called without passing arguments, it will not be an error, but can be found in the function
If there is a pass-through parameter, for example, it can be judged as follows:
if (variable = = ' undefined ' | | variable = = NULL) {
variable= ' 1 ';//can give him a default value
}
JavaScript looping statements and functions