Js cycle usage in

Loop: for loop while loop do {} while () loop

What is the cycle?
  Repeat the same section of code
features:
  1, a predetermined number of cycle condition execution cycle
  2, the operation cycle, the same display or statement to be executed

Cycle three elements:
  1, variable loop
  2, loop condition, the condition execution cycle
  3, the loop variable changes, changes tend to be satisfying the condition is not satisfied, the cycle tends to end.

A, for loop
  syntax:
  for (Expression 1; 2 Expression; Expression 3) {
  loop body
  }

  Expression 1: Declare variables need to be declared before the start of the cycle is omitted good loop variable
  expression 2: judgment cycling conditions may be omitted add end of the cycle in the body of the loop will form a judgment or an infinite loop
  expression 3: Updating the loop variable add update statement can be omitted loop variable in the loop body

  Outputs 1 through 100
  for (var. 1 = I; I <= 100; I ++) {
  the console.log (I); // console output
  }

Two, the while loop
  syntax;
  the while (condition) {
  loop
  }
  1) Analyzing Cycling conditions
  2) the loop condition is true true iteration of the loop
  re-determination condition, the condition is true if the loop body is executed
  ....
  3) loop condition is false false operation cycle is ended
  outputs 1 through 100
  var = I. 1;
  the while (I <= 100) {
  the console.log (I);
  I ++;
  }

Three, do {} while ()
  and the difference is that the while loop, the first loop execution, then determination condition.
  Syntax:
  do {
  loop
  } while (condition)
  1) Analyzing Cycling conditions
  2) the loop condition is true true iteration of the loop
  re-determination condition, the condition is true if the loop body is executed
  ....
  3) loop condition is false false ends cycle operation

  Outputs 1 through 100
  var. 1 = I;
  do {
  the console.log (I)

  i++;
  }while(i<=100)

Guess you like

Origin www.cnblogs.com/Godfather-twq/p/11273878.html