The Go language has only one loop body, which is a go loop.
The basic for loop looks like C or Java. But it does not have parentheses () and must not have them. braces {} are mandatory.
1 PackageMain
2
3Import "FMT"
4
5Func main (){
6 Sum: = 0
7 For I: = 0; I <10; I ++ {
8 Sum + = I
9 }
10FMT. println (SUM)
11}
45
Just like C or Java, You can omit the preceding and subsequent statements.
1 PackageMain
2
3Import "FMT"
4
5Func main (){
6 Sum: = 1
7 For; sum <1000 ;{
8 Sum + = sum
9 }
10FMT. println (SUM)
11}
1024
You can delete the semicolon in that position. The while in C language is replaced with for in the go language.
1 PackageMain
2
3Import "FMT"
4
5Func main (){
6 Sum: = 1
7 For sum <1000 {
8 Sum + = sum
9 }
10FMT. println (SUM)
11}
1024
If you delete the loop condition statement, it is a wireless loop.
PackageMain
Func main (){
For ;;{
}
}
[Process took too long]
There are no statements and the semicolon is omitted. It is a compact infinite loop.
1 PackageMain
2
3Func main (){
4 For {
5 }
6}