First, JS in the loop structure
Steps to perform the loop structure
1, declare the cyclic variable;
2, judge the cycle conditions;
3, the implementation of the circulation body operation;
4, update the loop variable;
5, then loop execution 2-4, until the condition is not set, jump out of the loop.
the expression in the while loop () can be of various types, but will eventually be true and false, and the transformation rule is the same as the IF structure. The code is as follows:
var num = 1;
while (num<=10) {//2, judging the cycle condition;
document.write (num+ "<br/>");//3, perform cyclic body operation;
NUM++;//4, update the cyclic variables;
}
While loop characteristics: first judgment after execution;
Do-while Loop: Perform the re-judgment first, even if the initial condition is not established, the Do-while cycle executes at least once; code is as follows
var num = ten;
do{
document.write (num+ "<br/>");
num--;
}while (num>=0);
document.write (num);
forCycle
1. For has three expressions: ① declares the cyclic variable; ② determines the cyclic condition; ③ updates the cyclic variable;
Between three expressions, with or as a division,
For loop three expressions can be omitted, but two;
2, for loop execution characteristics: first judgment and execution, and while the same
3, for loop three expressions can have a multi-part, the second part of a number of criteria for judging && | | Connection, part 13th is separated by commas; The code is as follows:
for (var num =1; num<=10; num++) {
document.write (num+ "<br/>");
}
Loop control Statements
1, break: Jump out of this layer loop, continue to execute the statement after the loop.
If the loop has multiple layers, break can only jump out of one layer.
2, continue: Skip the remainder of this cycle of code, continue to perform the next cycle.
① to the statement executed after the For loop, continue, is the Loop variable UPDATE statement i++;
② for the while, Do-while Loop, continue executed after the statement, is the cyclic condition judgment;
Therefore, when using these two loops, continue must be placed after i++, otherwise continue will skip i++ into the dead loop. The code is as follows:
var I=1;
While (i<=20) {
document.write (i+ "<br/>");
i++;
}
document.write ("End of Cycle");
Two, the number of daffodils (three digits)
While loop notation, the code is as follows:
var i=100,a,b,c;
while (i<=999) {
A=parseint (i/100);
B=parseint (I%100/10);
C=parseint (I%10);
if (a*a*a+b*b*b+c*c*c==i) document.write (i+ "<br/>");
i++;
}
for loop notation, the code is as follows:
for (Var i=100,a,b,c;i<=999;i++) {
A=parseint (i/100);
B=parseint (I%100/10);
C=parseint (I%10);
if (a*a*a+b*b*b+c*c*c==i) document.write (i+ "<br/>");
}
Operation Result:
Third, Fibonacci sequence
while loop, the code is as follows:
var a = 1;
var b = 1;
var i = 1;
var C;
document.write (A + "," +b+ ",");
while (i<=10) {
C=a+b;
A=b;
B=c;
i++;
document.write (c+ ",");
}
for Loop, the code is as follows:
var a = 1;
var b = 1;
var C;
document.write (A + "," +b+ ",");
for (var i =1;i<=10;i++) {
C=a+b;
A=b;
B=c;
i++;
document.write (c+ ",");
}
Operation Result:
JavaScript's looping structure and classic topics