First, the Loop statement
The loop statement is a for statement, and you might say, "No, it's not just for statements, there's a while or a do-while", and it's really a pity that the go language is really only for, and it thinks that providing one is OK, providing more fear of causing trouble to programmers:)
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++{
Cumulative sum
sum + = i
}
Print results
Fmt. Println ("sum=", sum)
Results such as:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5A/5D/wKioL1T7EDigDGZsAAA_GdFb3Wk125.jpg "title=" Sum value. png "alt=" wkiol1t7edigdgzsaaa_gdfb3wk125.jpg "/>
1, habits of the formation
If you have been using C/c++/java development, you may say: "For loop and if, there is no parentheses, really not used to", yes, at first really not used to, and so you write 30 times after the habit of 650) this.width=650; "src=" http// Img.baidu.com/hi/jx2/j_0059.gif "alt=" J_0059.gif "/>
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 {
sum + = i
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 {
If i > 100 {
Break
}
sum + = i
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++ {
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) {
/ * Here I is the index of the blog array, item is the value corresponding to the blog array index, for example, when the I value is 1 o'clock, the value of item is Blogtitle * /
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 {
Fmt. Println ("Index", I, "corresponding value is", value)
}
Operation Result:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/5A/5F/wKioL1T8J8OivANYAACOUEymqdQ071.jpg "title=" Range.png "alt=" Wkiol1t8j8oivanyaacoueymqdq071.jpg "/>
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
Using loops is often accompanied by break, continue, goto keyword, yes, you are not mistaken, 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:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5A/63/wKiom1T8KvvQYpvcAAJKEzzrYF4904.jpg "title=" jump. png "alt=" Wkiom1t8kvvqypvcaajkezzryf4904.jpg "/>
First define an index variable i and sum variable sum, and then define a label Mybowl (Come to my bowl to 650) this.width=650; "Src=" http://img.baidu.com/hi/jx2/j_0069. GIF "alt=" j_0069.gif "/>), and then determine whether the index variable i to 101, if not to the sum value of add I, then i++, and then jump to Mybowl, when the condition is not satisfied (that is, I to 101) output 1 to 100 of the sum
"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 {
sum + = i
i++
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 {
For I: = 0; I < 5; i++ {
If I > 3 {
Fmt. Println ("i=", i)
Break
}
}
}
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:
for {
For I: = 0; I < 5; i++ {
If I > 3 {
Fmt. Println ("i=", i)
Break
}
}
}
The program is similar to the above program 1, just add a label mybowl, then what about this program?
Answer:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5A/63/wKiom1T8OGyz-LRxAABqtys9jlg502.jpg "title=" Loop hierarchy. png "alt=" wkiom1t8ogyz-lrxaabqtys9jlg502.jpg "/>
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:
for {
For I: = 0; I < 5; i++ {
If I > 3 {
Fmt. Println ("i=", i)
Break Mybowl
}
}
}
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
"Go Language" "9" go language of the loop statement