While loop
while (condition) {code to execute if condition is set}
* The general condition variable needs to increment, otherwise it will enter the dead loop (infinite loop), the browser crashes or even the computer freezes.
For example, a row-by-line output of 1-100 digits
var i = 1;
while (i<=100) {
document.write (i+ "<br/>");
i++;
}
Do...while Cycle
do{Execute code}while (conditions, if the condition is true, continue the loop, otherwise abort the loop)
For example, a row-by-line output of 1-100 digits
var i = 1;
bo=
document.write (i+ "<br/>");
i++;
}
while (i<=100)
* Unlike while: Do...while is the first to execute code before judging the condition, while the while opposite
For loop
For (initial value of parameter; parameter condition; parameter increment) {Condition for execution code}
Where the parameter is a local variable
For example, a row-by-line output of 1-100 digits
for (Var i=1;i<=100;i++) {
document.write (i+ "<br/>");
}
* It is recommended to use a for loop to facilitate program reading
Terminate loop: break,continue
Break: Exit the entire loop now
Continue: Skipping execution code for the current loop to continue looping
For example:
for (Var i=1;i<10;i++) {
if (i==5) {break}
}
alert (i); Output is 5
for (Var i=1;i<10;i++) {
if (i==5) {continue}
alert (i);
}
Output of 1,2,3,4,6,7,8,9 will find alert (5) skipped
Function
JS has a number of functions, including built-in functions and custom functions.
Built-in functions: in fact, we often use the alert (parameter) can also be seen as a function, it represents the way to pop a window, and the window prompt is the function of the parameter
Custom functions: We can create a function with a specific function method, for example, we create a function area (s,h)//where S is the bottom, H is high
To create a custom function:
Functions Area (s,h) {}//area is a custom function name, S,h is a custom parameter
To add functionality to a function:
function Area (s,h) {
var area=s*h*0.5;
return area;
}
In this way, the area of a triangle with a height of 5 is 10, as long as the function is directly called to write the functions can be: areas (5,10);
JS Summary loop exit loop function