"Go Language" "9" go language of the loop statement

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

First, the Loop statement

Listen to the loop statement as a for statement, you might say: "No, it's not just for statements, and while or do-while", really sorry, the go language is really only for, it thought to provide a kind of, provide more afraid to bring trouble to the programmer:)

Here we use the For loop to calculate the 1+2+3+4+......+100 and, as in other languages, we use the Loop 100 times:

Define a variable so that it loops 100 times from 1, each time judging whether it is less than 101, and if the variable adds 1

The code is as follows:

Declares the variable sum and initializes it to 0

Sum: = 0

Define the variable i, loop 100 times, each time to determine whether it is less than 101

for i:= 1; i< 101; i++{

Listen//Accumulate sum

Listen to sum + = I

}

Print results

Fmt. Println ("sum=", sum)

Results such as:


1, habits of the formation

If you have been developing with C/c++/java, you may say, "For loop is like if, there is no parenthesis, I'm not used to it," Yes, it's not customary at first, so you get used to it after you write it 30 times.


2, the above example of deformation

Some languages do not allow variables to be defined after for, so the above example can be deformed to

Sum, I: = 0, 1

For I < 101 {

Listen to sum + = I

Listen to i++

}

Fmt. Println ("sum=", sum)

The variable i definition for the back is moved to the for, and I is added to the inside of the For loop body, and for the back is only used for conditional judgment


3. Infinite loop

Since the go language removes the While keyword, how do you express an infinite loop? The secret is that for the back, no expression is taken.

Sum, I: = 0, 1

for {

Listen if I > 100 {

Listen and hear the break

Listen}

Listen to sum + = I

Listen to i++

}

Fmt. Println ("sum=", sum)

Since there are no conditional expressions for the for, it is not always possible for the CPU to run down to form a dead loop, so the break keyword is used, and break can jump out of the current for loop when the condition is met


Two, RANGE traversal

For a blog post, it has an ID (blogId), a title (Blogtitle), a content (Blogcontent), a publication date (blogdate), and so on, if the information is placed in a string array:

Blog: = []string{"BlogId", "Blogtitle", "Blogcontent", "Blogdate"}

So how do we traverse this array? Using loop statements is the most common and first thought:

For I: = 0; I < len (blog); i++ {

Listen to the FMT. Printf ("%s\t", Blog[i])

}


If the reader is familiar with JavaScript scripts, you should know that there is a each traversal usage:

$.each (blog, function (i, item) {

Listen */* Here I is the index of the blog array, item is the value corresponding to the blog array index, for example, when I value is 1 o'clock, the value of item is Blogtitle * /

Listen alert ("i =" + i + ", value =" + Item ")

});

The client script programmer is very fond of this traversal usage, fortunately the Go language also provides a similar usage: range, which is used with the FOR keyword block for array, slice, string, and map traversal. Now use range to rewrite the above traversal:

For I, Value: = Range Blog {

Listen to the FMT. Println ("Index", I, "corresponding value is", value)

}

Operation Result:


Glimpse, it can be seen that the go language has many of the advantages of language built into itself, which is why in the "3" Go language constants, said: "Just start to touch go, feel it is a hodgepodge, there are many language shadow" reason:)


Three, jump statement

Listen to the use of loops often accompanied by the break, continue, goto keyword, yes, you do not read wrong, goto keyword in the go language has been reused, early in the first contact programming, experienced programmers always inculcate: "Can not goto do not use Goto", So many languages are always deliberately avoid goto, but in the key words such as gold in the go language, goto and shiny up.


The following example uses the GOTO keyword to rewrite the 1+2+3+4+......+100 loop:

First define an index variable i and sum variable sum, and then define a label Mybowl (Come to my bowl ), and then determine whether the index variable i to 101, if not to the sum value add I, then i++, and then jump to Mybowl The sum of 1 to 100 is output when the condition is not satisfied (i.e. I to 101)


"Remarks":

1, go language no ++i, only i++

Or that sentence, the go language thinks that one way to solve a problem is not to provide a second method.


2, the scope of the label

In the example above, Mybowl is a label whose scope includes

If I < 101 {

Listen to sum + = I

Listen to i++

Listen to Goto Mybowl

}

But does not include FMT. Println ("sum=", sum), otherwise it will output 100 times "sum="


Iv. Exercises

1. What is the output of the program?

for {

Listen for I: = 0; I < 5; i++ {

Listen Listen if I > 3 {

Listen and listen and listen to the FMT. Println ("i=", i)

Listen, hear, hear, break.

Listen and Hear}

Listen}

}

Answer:

The dead loop. Since break is just a loop out of the current layer, break can cause the program to jump out for i:=0 when i=4. i<5;i++{} This layer loops, but does not jump out of the first layer for{} this infinite loop.


2. What is the output of the program?

Mybowl:

Listen for {

Listen listen for I: = 0; I < 5; i++ {

Listen and listen to hear if I > 3 {

Listen, listen, hear, hear, and listen to the FMT. Println ("i=", i)

Break

Listen, listen, hear, listen, listen.

Listen and Hear}

Listen}

The program is similar to the above program 1, just add a label mybowl, then what about this program?

Answer:

We think of the mybowl tag and the for{} Infinite loop as the first loop, for i:=0;i<5;i++{} as the second loop, when I is 4 o'clock conditional statement, execution break jumps out of the second loop into the first loop, because the first loop is an infinite loop, So here is the dead loop, with no relation to the mybowl tag.

3. What is the output of the program?

Mybowl:

Listen for {

Listen listen for I: = 0; I < 5; i++ {

Listen and listen to hear if I > 3 {

Listen, listen, hear, hear, and listen to the FMT. Println ("i=", i)

Break Mybowl

Listen, listen, hear, listen, listen.

Listen and Hear}

Listen}

This program differs from program 2 only in the break statement

Answer:

Output i=4

The reason is that when the conditional statement is satisfied when the i=4, the output i=4 executes the break statement, here the break after the addition of the label Mybowl, which also means to jump out of the mybowl tag, and the Mybowl tag is the first loop, so the program can end.


Conclusion:

To be familiar with a language, the most important thing is to use the language to write applications, write more can experience, practice practice:)

This article is from the "Green Guest" blog, please be sure to keep this source http://qingkechina.blog.51cto.com/5552198/1618407

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.