"Execution steps for the loop structure"
1. Declaring the loop variable
2. Judging the cycle conditions
3. Perform cyclic gymnastics
4. Update the loop variable
then, the loop executes 2-4 until the condition is not set, jumping out of the loop.
the expression in the while loop () can result in various types, but will eventually be converted to true or false .
The conversion rule is the same as the if structure.
while Loop features: First judge, then execute.
Do-while cycle characteristics: First execution, then judgment; even if the initial conditions are not true, the Do-while cycle is executed at least once.
//var NUM=1;//1, declaring a loop variable
//while (num<=10) {//2, judging cycle conditions
//document.write (num+ "<br/>");//3, performing cyclic gymnastics
//NUM++;//4, updating loop variables
//}
"For Loop"
1. The For loop has three expressions: ① declares a cyclic variable; ② determines the loop condition; ③ updates the loop variable.
between three expressions, separated;
for loop three expressions can be omitted, but two;
2, for loop execution characteristics: First judge, then execute. is the same as while.
3, for loop three expressions, can have a multi-part composition. The second part of a number of criteria for judging && | | Connection,
part 13th, with, separating.
[loop control statement]
1, break: Jump out of this layer loop, continue to execute the statement after the loop.
If a loop has multiple layers of nesting, then break can only jump out of one layer.
2, continue: Skip the remainder of this cycle of code, continue to perform the next cycle.
① for A For loop, the statement executed after continue is the Loop variable UPDATE statement i++
② for the while Do-while Loop, the statement executed after the continue is a cyclic condition judgment.
Therefore, when you use these two loops, you must put continue in the i++ after you use them. Otherwise, continue will* Skipping i++ causes a dead loop.
Examples:
Enter n number to achieve 1 plus to n
var count = prompt ("Please enter an integer number:"), var i = 0;var sum = 0;while (i<count) {var num = parseint (Prompt ("enter" + (i+1) + "integer:")); i==count-1) document.write (num+ "="); else document.write (num+ "+"); sum + = num;i++;} document.write (sum);
To find the Fibonacci sequence.
varA=1; varB=1; varc=a+b; document.write ("1" +1+ ""); for(vari=1;i<18;i++) {a=b; b=C; C=a+b; if((i+2)%5==0&& (i+2)!=0) {document.write (c+ "<br/>"); }Else{document.write (c+" "); } }
"Declaration and invocation of functions "
1, the function of the declaration format:
function Name (parameter 1, parameter 2,...) {
//Function Body Code
return value;
}
Call to function
① Call directly: function name (value of parameter 1, value of parameter 2, ...);
② Event Invocation method: directly in the HTML tag, using the event name = "function ()"
<button onmouseout= "saysth (' Hello ', ' red ') ' > click the button to print the content </button>
2. Considerations for Functions
the ① function name must conform to the small hump rule, and the first letter of each word is capitalized;
The parentheses after the ② function name can have parameters, or no parameters, respectively called the parameter function and the parameterless function;
③ The argument list when declaring a function is called a formal parameter list. (The name of the variable);
function Saysth (str,color) {}
The argument list, called the arguments list, is the actual parameter when the function is called. (Assignment of variables);
saysth ("Hello", "Red");
There is no actual association between the number of parameter lists of the ④ function and the number of argument lists.
the number of function arguments, depending on the argument list
if the number of arguments list < parameter list, the parameter that is not assigned is undefind.
The ⑤ function can have a return value, return the result using return, and use a variable to receive the return result of the function when the function is called.
If the function does not return a value, the received result is undefined;
function func () {
return= "haha";
}
var num=func ();//num= "haha";
Scope of variables in the ⑥ function
in a function, a variable declared with Var, which is a local variable. can only be accessed inside the function;
variables declared without VAR, as global variables, can also be accessed outside the function;
The formal parameter list of the function, which defaults to the local variable of the function and can only be used inside the function.
The declaration of the ⑦ function has no precedence over the invocation of the function, that is, the function can be called before declaring the function
func ();//Declaration of Func before call is also possible
function func () {}
[use of anonymous functions]
1. Declare an anonymous function that assigns a value directly to an event.
window.onload=function () {} document-ready function
2. An anonymous function expression. Assign the anonymous function to a variable.
Disclaimer: Var func=function () {}
Call: Func ();
Note: When using an anonymous function expression, the function's invocation statement must be placed after the function declaration statement (as distinct from the normal function).
3. Self-executing function:
①!function () {} ();//You can start with a variety of operators but generally use exclamation marks.
! function (formal parameter list) {} (argument list);
② (function () {} ());//use () wrap the parentheses around functions and functions
③ (function () {}) (); use () to wrap only the function part;
three ways of writing:
① use! The beginning, the structure is clear, not easy to confuse, recommended to use.
② can indicate that anonymous functions and calls () are a whole, and are officially recommended for use.
③ cannot indicate the entirety of parentheses and subsequent (), and is not recommended for use;
//single-line comment
/* Multiline comment, start with a * */
/**
[JS code Execution order problem]
JS code is divided into two parts when it is run. Check loading and execution stages
Check the load stage: The syntax error of the code is detected first, and the declaration of the variable and function
Execution Phase: Assignment of variables, invocation of functions, etc., all belong to the execution phase
take the following code as an example:
console.log (num);//undefined
var num=10;
func1 ();//function can execute normally
function Func1 () {}
Func2 ();//function cannot execute, when printing func2, display undefined
var func2=function () {}
the order of execution of the above code is
------Check the load stage-------
var num;
function Func1 () {}
var Func2;
------Code Execution phase-------
console.log (num); num=10;
func1 ();
Func2 (); Func2=function () {}
[Internal properties of a function]
1. Arguments Object
① function: Used to store all arguments when a function is called. When we call a function and assign a value with an argument, the argument list is actually
saved to the arguments array, which can be called in the function, using arguments[n]. N starting from 0.
the number of ②arguments arrays, depending on the argument list, is independent of the parameter.
However, once the nth position of the formal parameters, arguments, arguments are present, the shape arguments and arguments bound, synchronous change. (That is, in
Modify the value of the parameter in the function, the arguments will also change, and vice versa, is also established)
③arguments.callee is an important attribute of arguents, which represents the reference address of the function arguments, in which the function can be
calling the function itself using Arguments.callee
inside the function, the invocation of the function itself is called recursion.
recursion is divided into two parts: recursion and attribution. The function can be divided into the upper and lower parts by using recursive call statements as bounds.
Pass: When the function executes the upper part and encounters its own invocation statement, it proceeds to the inner function and executes the upper part.
until the most inner function is executed.
return: When the inner function is finished, the bottom half of the function is gradually executed starting from the inner function.
when the outermost function executes, it encounters its own invocation statement, which goes into the inner function execution, while the second part of the outer function is temporarily not executed. Until
when the inner function is finished, it is executed gradually.
Use recursion to calculate 1-10 and
var sum=0; var i=1; function sum1 () { + = i; I++ ; if (i<=10) { sum1 (); } return sum; } Sum1 (); alert (sum);
Html-js Loop function recursion