JavaScript great God cultivation (4)--cycle

Source: Internet
Author: User

Hello readers, today, we continue to the previous content, before we have said the conditions of the branch, today we talk about the loop, as the name implies, repeat the same operation, the normal cycle is controlled by the program, abnormal situation, there will be a dead loop, that is, we have a bug in the code, so, We also have to learn the mode of the bug, when we finish the basic knowledge, I will also dedicate to the space to explain the browser debugging, that program will be in our control, this is what we want results.

The structure of the loop consists of a for, while, Do--while,for Loop has two forms of existence, one is the number of variables caused by the change of the loop, the other is the for...in form, is the property or subscript caused by the cyclic changes, but for...in is not the key, The key or for, as in the C # language, is called foreach, well, it's just a name, no different, I'll talk about it when I say it.

Starting from for

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

(A new operator is used here, which I have previously omitted, but, as you can see, it is understood that the less operator, the result of the operation is a Boolean (True,false))

Here the successful print out of 1 to 9, see the above 4 execution steps, the first step (declare variable), execute only once, and then the second step to determine whether the condition is set (the same type as the condition after the IF), if set up, followed by the execution of the contents of the loop body, This is considered the third step , after the third step is done, the fourth step is to make the variable change; then, second step , determine whether it is true. This has been connected to the top, and then the cycle is so alternating.

Note: Explain the fourth step i++ we can understand it this way i = i + 1; I was immediately clear, I = 0 was declared, then i = i + 1 is let I re-assignment, is to make it change, i = 0 + 1, so I becomes 1, when the execution of a lap of time to print out the 0,i It becomes 1, when the second lap finishes, the print is 1, I becomes 2, ..., when the 10th lap finishes, the printed 9,i is changed 10, when the second step to perform the judgment, I < 10 is the < 10, the return is false, so it is not established, so that the latter will not continue.

The control we've talked about above is done 10 times exactly as we intended, and if it's the same condition, can we jump out of the loop when we're halfway there? The answer is yes, it's going to use one of the keywords we've already learned. Break down in the example code we'll look at

 for (var i = 0;i<10;i++) {    console.log (i);     // when I equals 5, we jump out of the loop    . if (i = = 5)        {break;    }}

See, as long as we meet our conditions, when we want to jump out of the loop, is OK, which forces the interruption of the subsequent steps to execute.

Since there is a forced interruption, then, I would like to ask, there is no forced to continue the cycle? The answer is yes, we're going to use a new keyword continue

 for (var i = 0;i<10;i++) {    // when I is less than 5, we force the loop    if(i < 5) {         Continue ;    }    Console.log (i);}

is not to achieve the effect we want, when I is less than 5, we are strong cycle, the subsequent printing has not been executed, continue the next cycle, when I is greater than or equal to 5, we print out the value of I.

Let's go on to the next for...in, before this, we have to understand a data type, is an array, the previous reason I did not say, because the front even said, we also do not understand, so now let's look at the array, we only say a one-dimensional array, Now it is not necessary to speak two-dimensional and multidimensional arrays, see the sample code

// declares an array, enclosed in brackets, where the contents of the group are separated by commas, and the array can contain values of various types var arr = [N, "abc", "Mrdream",true,false,null]; // array value is used subscript to get, in the program, the first value of the subscript is 0, the second is 1, and so on after // here in arr array, we put 7 values in, so the biggest subscript is 6 .

Next, let's take a look at how to get the individual values

// 1 // 2 // "ABC" // "Mrdream" // true // false // NULL

Let's do it, see if that's the case.

Just like we expected.

From the above example, we can get the values in the array and print them out, but is it a lot of trouble to write like this every time? By the right, we can use the loop.

var arr = [N, "abc", "Mrdream",true,false,null];  for (var i = 0;i<7;i++) {    console.log (arr[i]);}

Haha, see the magic of the loop, it is so convenient, but here, we are using a variable to simulate the subscript, below I use for...in to cycle

var arr = [N, "abc", "Mrdream",true,false,null];  for (var in arr) {    console.log (arr[i]);}

For...in in JavaScript is used to iterate over the array's subscript and Object Properties , object properties, and objects, and now we're just talking about arrays, and now I'm going to explain the steps of for...in. var i is used to declare a variable subscript (for an array), in to specify in which set, to get the subscript, and if there is nothing in the array, the loop will be terminated directly. This understanding is more abstract than the previous one, and you will understand it several times.

Now let's verify that the variables we get in the array are not subscript

See the example, obviously is the subscript obtained.

Next we'll explain the new loop mode while

 while (condition) {    // execute }

Now you see the terms of two words is no longer unfamiliar, or if the conditions behind the use of the same type, we still use the ticket to give an example

var tickets = 10; // Total number of tickets // conditions, when the number of tickets is greater than 0 o'clock, the execution of the Ticketing Act  while (Tickets > 0) {    Console.log ("Currently there are" + Tickets + "Tickets available for sale, the next one");  Here we use string concatenation     --//Sell a ticket, we reduce one }

Note: Here, we are exposed to an operator that is not mentioned earlier--that is, the variable is reduced by 1, as in the above + + operation.

Here we happen to perform 10 sales movements, is it not feel this kind of writing cycle is very simple? In this case, we will give it a thorough understanding, the first is to determine whether the condition is set up, if it is set up, the implementation of the loop body inside the behavior, until the conditions are not established. Speaking of here, we are not very confused, this cycle, only the conditions, the establishment of the loop, and the front for the loop, almost, only the first to meet the conditions, and then execute the content inside. Then there is a loop, is the first to execute the loop body content, and then to determine whether the condition is set up? Well asked, we just need this kind of research spirit, then, JavaScript language did not let us down, he really has such a loop body, that is do...while; let's look at the Grammar first.

 Do {    // execute }while (condition)

Here is the first to execute the contents of the loop body, and then to determine whether the condition is established, if the condition is established, then the loop before the content of the execution

Below we take the life inspirational money as an example to explain passers-by want to take a daughter-in-law, but only 100,000 yuan deposit, but take daughter-in-law needs 500,000, then how to do, only hard work, when there is enough money, you can happily take the daughter-in-law

var // passers-by has 100,000 yuan deposit  Do {    Console.log ("1 years of hard work, save 100,000");      // after work, rich, modify the variable once }  while // The condition is that the deposit is less than 500,000, and continue to perform the work behavior

See, no money, first to try to make money, passers-by after 4 years of unremitting efforts, finally saved enough 500,000 (because he had already had 100,000 yuan), finally took the daughter-in-law, and lived a happy life. We have to work hard, too.

In this way, do you understand the way of do...while circulation in a moment?

Summing up, we explained today the cycle, including 4 kinds of cycle for, for...in, while, Do...while, we have learned it, do not enjoy it, we can also see a few times, what we said today, understanding is a little difficult, but more write more practice, natural can be used skillfully.

JavaScript great God cultivation (4)--cycle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.