-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.For.go
51 lines (42 loc) · 1.17 KB
/
5.For.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Title: 5.For.go
Author: OpenSource
Date: 2017-05-20
Description: For Study
for is Go’s only looping construct. Here are three basic types of for loops.
*/
package main
import "fmt"
func main(){
fmt.Println("5.For.go--------Start-----------")
i := 1
fmt.Println("for i <= 3 {} ")
// The most basic type, with a single condition.
for i <= 3 {
fmt.Println(i)
i = i + 1
}
fmt.Println("for j:= 7; j <= 9; j++ {} ")
// A classic initial/condition/after for loop
for j := 7; j <= 9; j++{
fmt.Println(j)
}
fmt.Println("for {} ")
// for without a condition will loop repeatedly until you break out of the loop
// or return from the enclosing function.
for {
fmt.Println("loop")
break
}
fmt.Println("for n:= 0; n <= 5; n++ {} ")
// You can also continue to the next iteration of the loop.
for n := 0 ; n <= 5; n++ {
if n%2 ==0 {
continue
}
fmt.Println(n)
}
// We’ll see some other for forms later when we look at
// range statements, channels, and other data structures.
fmt.Println("5.For.go--------End-----------")
}