JS looping structures have? Loop structure Overview

The so-called loop, is to repeat the implementation of a code, judgment and personal computer is far worse compared to computers better at one thing - constantly repeated. And we call this loop in JavaScript. Let us learn to understand JavaScript in circulation.

js loop structure which

There are three loop structure js

  1. The for loop ==> multiple passes to block
  2. while loop ==> when the specified condition is true, the code block cyclic
  3. do while ==> when the specified condition pseudo true, the code block cyclic

1, for loop

for it is composed of two parts, and condition control loop
syntax:

for(初始化表达式;循环条件表达式;循环后的操作表达式){
	需要重复的代码块;
}

for sentence structure as shown:
for loop
for sequential execution cycle

1. initialization expression
2. The cycling conditions expression
3. Repeat code blocks
after cyclic operation expression 4

Simple for loop, a loop is executed a variable value change
for example: output value from 1 to 100

for(var i=1; i <= 100; i++){
//在循环开始时设置一个变量i;//定义运行循环的条件i<=100;//每个循环执行后,变量增加1
console.log(i);
}

2, while circulation

The while loop repeats execute a piece of code until a certain condition is no longer met.
grammar:

while(条件表达式语句){
	执行的代码块;
}

FIG while loop structure:
while loop structure
while execution sequence
when we use the conditions of the condition return value is true, will execute blocks of code inside the braces, the braces statement been performed, repeats the braces statement, until it is determined the condition returns is false, the cycle will end.

Case:

var i = 0;
while (i < 10){
	console.log(i);
	i++;
}
//while循环会先判定条件,再根据条件是否成立达成决定是否进入循环
//如果条件一开始就是false ,则不会进入循环

Disadvantages:

  1. Use the while statement, be sure to write braces
  2. If there are no conditions, it will be unlimited run down, resulting in an endless loop.

3, do while loop structure

while the basic principles and structures do while the structure is substantially the same, but it ensures that the loop is executed at least once. Because it is the first implementation of the code, after the judgment condition
syntax:

do {
	执行语句块;
}
while(条件表达式语句);

do while execution order:
first implementation of a code, a final decision. And while circulating different, do no matter what the conditions will be executed once the code while
the case:

var i = 0;
do{
	console.log(i);
	i++;
}while(i<10);

While and do while the different

  1. while: first determine the condition is not satisfied then execute the loop body is not executed again
  2. do ... while: first determination condition and then execute the loop body is executed at least once
Released two original articles · won praise 14 · views 189

Guess you like

Origin blog.csdn.net/qq_44279352/article/details/104578428