JavaScript daily must learn the cycle _javascript skills

Source: Internet
Author: User
Tags ticket

Hello, friends, today, we go on to the previous content, we have already talked about the conditional branch, today we talk about the cycle, as the name implies, repeat the same operation, the normal cycle is controlled, abnormal situation, there will be a dead loop, that is our code in the bug, that, We have to learn to adjust the bug, after we finish the basic knowledge, I will devote space to explain the debugging in the browser, that program will be in our control, this is what we want results.

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

Start from for

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

(Here's a new operator, which I've been missing in the first place, but, as you can see, the operator returns the result is a Boolean value (True,false))

It was successfully printed out in 1 to 9, see the 4 steps above, the first step (declaring the variable), only once, and then the second step to determine if the condition is set (the same type as the condition after the IF), and if it is set up, follow the contents of the loop body, This is considered the third step , after the third step is executed, is to perform the fourth step to let the variable change; then the second step is taken to determine whether it is tenable. It's already been picked up, and then the cycle is alternating.

Note: explain the fourth step i++ we can understand that i = i + 1; In a flash it was clear that I = 0 was stated earlier, then i = i + 1 was to let I reassign, that is, to make it change, i = 0 + 1; So I became 1, and when I finished a lap, Waiting to print out the 0,i is turned into 1, the second lap after the execution, print is 1, I will become 2, ..., the 10th lap after the completion of the printing is 9,i changed 10, when the second step to perform the judgment, I < 10 is ten < 10, return is false, so does not set up, This will not continue after that.

The control we have mentioned above is executed 10 times in full according to our intention, if it is the same condition, can we jump out of the loop in the middle? The answer is yes, this is going to use a keyword we've already learned. Break under break in We'll look at the sample code

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, we want to jump out of the loop, it is OK, this forced to interrupt 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 definitely OK, we are 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 achieved the effect we want, when I is less than 5, we have a strong cycle, the subsequent printing has not been performed, the next cycle to continue, when I is greater than or equal to 5, we will print out the value of I.

Here we go on to say for...in, before that, we have to understand a data type, is an array, before I said, because in front of even said, we still can not understand, so now let's take a look at the array, we are here to say only one-dimensional array, There's no need to talk about two-dimensional and multidimensional arrays, look at the sample code

Declare an array, enclosed in brackets, in which the contents of the group are separated by commas, and the array can contain various types of values
var arr = [1,2, ABC, Mrdream, True,false,null];

Array value is to use subscript to get, in the program, the first value of the subscript is 0, the second is 1, and so
on//here in the ARR array, we put 7 values in, so the largest subscript is 6

Next, let's take a look at how to get a single value

ARR[0]//1
arr[1]//2
arr[2]//"ABC"
ARR[3]//"Mrdream" arr[4
]//True
Arr[5]/False
arr[6 ]//NULL

Let's do it, see if it does.

It's the same as we expected.

From the example above, we can get the value in the array and print it out, but is it cumbersome to write it every time? Yes, we can use the loop.

var arr = [1,2, "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 = [1,2, "abc", "Mrdream", True,false,null];

for (var i in arr) {
  console.log (arr[i]);
}

For...in in JavaScript is used to loop the subscript and object properties of an array, the properties of objects, and objects, and we'll talk about the array now, and I'm going to explain the steps in the execution of the for...in. Var i is used to declare a variable subscript (for an array) , in which to specify in which set, in order to get subscript, if there is nothing in the array, the loop will terminate directly. This understanding is more abstract than the previous one, and you will understand it when you write a few more times.

Now let's verify that in the array, the variable we get is not subscript

See the example, obviously is the subscript obtained.

We'll explain the new loop while

while (condition) {
  //execute
}

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

var tickets = 10;//Ticket Total quantity

//condition, when the ticket quantity is greater than 0 o'clock, executes the ticketing behavior while
(Tickets > 0) {
  console.log ("There is also" + Tickets + " Tickets can be sold, next ");//Here we use string stitching
  tickets-;////////////


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

We're just doing 10 sales moves, do we feel like this is a simple way to write a loop? In this case, we will give it a thorough understanding, the first is to determine whether the conditions are established, if established, the implementation of the loop inside the behavior, until the condition is not established. Here, we do not feel very puzzled, this cycle, only the conditions, when the establishment, only the implementation of the loop inside, and the previous for loop, almost, only to meet the conditions before the implementation of the content. So is there a cycle, is the first execution of 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 circulation body, that is do...while; let's look at the grammar first.

do{
  //execution
}while (condition)

Here is the first execution of the contents of the loop body, and then to determine whether the condition is set up, if the condition is set up, then cycle the previous content execution

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

var money = 100000; Passers-by A has 100,000 yuan deposit

do{
  console.log ("Hard work 1 years, save a 100,000"); 
  Money + + 100000; After the work, the money, on the modified variable
}while (Cash < 500000); The condition is that the deposit is less than 500,000 and continues to perform the work Act

See it, no money, go first to make money, passers-by a classmate 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.

This is not to say that we understand the do...while cycle of the way.

To sum up, we explained the cycle today, includes 4 kinds of circulation way for, for...in, while, do...while, we have not learned it, see again not enjoyable, we can also see a few times, today's talk of things, understand that there are some small difficulties, but more to write more practice, natural can be used skillfully.

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.