3. loop structure

1. The concept and meaning of cycle
 
Loop: refers to movement or change things again and again.
 
There are many repeat regularity in practical problems, so you need to repeat some statements in the program.
 
feature:
1. Repeat regularity of
2. The code is repeatedly performed very similar
 
The: output 10 'hello world'
 
console.log('hello world 1');
console.log('hello world 2');
console.log('hello world 3');
......
console.log('hello world 10');
 
This process is very time-consuming and laborious, but also there are a lot of redundant code!
 
If I want to output 100 times 1000 'hello world' it? -> cycle!
 
 
2.for cycle
 
The general form is:
 
for (Expression 1; 2 Expression; Expression 3) {
    Loop body;
}
 
Expression 1: For a single expression is not participating in the loop for loop control variables to initial values
Expression 2: typically is a relational expression, as the loop condition (set end value)
Expression 3: Usually the loop variable is incremented or decremented (step)
Loop: you need to repeat code
 
for (var i = 0; i  <5; i ++) {// increment cycle
    console.log(i);
}
 
for (var. 5 = I; I> =. 1; i--) { // cycle reduction
    console.log(i);
}
 
Application Exercise: page includes five li, li all set the font color to red
 
 
note:
 
for () represented by the formula are in parentheses may be omitted, but can not be omitted semicolons.
 
2 is omitted expression ( loop condition ), if not do other processing becomes infinite loop.
 
Infinite loop: loop and has no termination condition is the execution of an infinite loop.
 
for nested loops, the relationship can be simply understood as rows and columns.
 
 
3.break and continue keywords
 
break keywords used in a loop, he represents the termination and out of the loop.
 
containue keywords used in a loop, he represented skip this cycle.
 
And if for use in combination to achieve a skipped cycles and meet a certain criteria to exit the loop.
 
 
4.while cycle
 
while loop as long as a specified condition is true, the cycle can always execute the code.
 
while (condition) {
  Code needs to be executed
}
 
Case: Xiao Ming, 68 yuan, eight yuan a bowl of rice noodles, rice noodles to eat with the number of times while the output of each How much money is left?
 
5.do/while cycle
 
do / while loop is a variant of the while loop.
 
Do {} perform a first code block, then perform  while () is determined.
 
Difference 6.for, while and do-while the
 
while loop is the first judge, then execute, it is possible to perform not once.
 
do / while loop is executed first, and then determining, performing at least one code block.
 
The for loop is generally used to determine the number of cycles in the scene.
 
while loop is generally used in cycles unknown scenarios.
 
Interview questions:
 
There are k = 0;
for(var i=0, v=0; i<6, v<9; i++, v++){
    k = i + v;
}
console.log(k);
 
There are k = 0;
for(var i=0, v=0; i<9, v<6; i+=2, v++){
k = i + v;
}
console.log(k);

Guess you like

Origin www.cnblogs.com/r-mp/p/11081337.html