把循环写完了。感觉和Python的差别不是很大。只是在GO中单独有switch, select。循环语句和python一毛一样。中断关键字,break.continue 也是一样的用法
package main
import ("fmt")
func main() {
//运算符 第一类 加减乘除,取余数等
a, b := 5, 10
var c int
c = a + b
c = a - b
d := 6
d *= a //与Python类似。 d= d*a
fmt.Println(c, d)
// 运算符 第二类:关系运算符 ==, !=, >,<,>=,<=,返回的是bool类型的false,true
i :=4
// 条件:if else
if i%2 ==0 {
fmt.Println("this is even")
} else {
fmt.Println("this is odd")
}
//条件:if...else if...
j := 9
if j %2 ==0 {
fmt.Println("j能被2整除")
} else if j%3 ==0 {
fmt.Println("j能被3整除")
} else if j%4 ==0 {
fmt.Println("j能被4整除")
}
//条件:switch
//条件语句:switch,case,按照顺序向下进行匹配,匹配成功后break,不会再进行匹配。
//switch默认情况下case 最后自带break语句,匹配成功后不会执行其他case,如果我们需要执行后面的case,可以使用fallthrough。
//fallthrough:强制执行后面的case语句
//switch var1 {
// case val1: 执行的命令
// ...
// case val2:
// ...
// default:
// ...
//}
//switch x.(type){
// case type:
// statement(s);
// case type:
// statement(s);
// default: // 可选
// statement(s);
//}
//条件写在外面,switch当作if else
score :=90
switch {
case score >90:fmt.Printf("优秀\n")
fallthrough
case score<=90 || score >80:fmt.Printf("加油吧\n")
default:fmt.Printf("你还有啥吧\n")
}
//条件写在switch中
switch scores :=91;{
case scores >90:fmt.Printf("优秀\n")
fallthrough
case scores<=90 || scores >80:fmt.Printf("加油吧\n")
default:fmt.Printf("你还有啥吧\n")
}
grade := "B"
marks := 90
switch marks {
case 90:
grade = "A"
case 80:
grade = "B"
case 70, 60, 50:
grade = "C"
}
switch{
case grade =="A": fmt.Printf("优秀A")
case grade =="B": fmt.Printf("良好B")
case grade =="C": fmt.Printf("加油吧您内C")
}
fmt.Printf("你的等级是 %s\n", grade )
//条件:select:
//select {
// case communication clause :
// statement(s);
// case communication clause :
// statement(s);
// default : // 可选
// statement(s);
//}
//循环:for
//for init; condition; post { } //for
//for condition { } //while
//for {}
//init: 一般为赋值表达式,给控制变量赋初值;
//condition: 关系表达式或逻辑表达式,循环控制条件;
//post: 一般为赋值表达式,给控制变量增量或减量。
m :=10
for i:=0;i<m;i+=1 {
fmt.Println(i)
}
p :=0
for p <10 {
fmt.Println(p)
p+=1
}
//循环语句的控制关键字:
//break语句:
//用于循环语句中跳出循环,并开始执行循环之后的语句。
//break 在 switch(开关语句)中在执行一条 case 后跳出语句的作用。
//在多重循环中,可以用标号 label 标出想 break 的循环。
//continue语句:跳过当前循环的剩余语句,然后继续进行下一轮循环。
//goto:无条件转移到过程中指定行,与条件语句配合,实现条件转移、构成循环、跳出循环体等(不建议用,造成混乱)
}
执行结果
GOROOT=C:\Program Files\JetBrains\GoLand 2020.1.3\go1.15.6\go #gosetup
-5 30
this is even
j能被3整除
加油吧
优秀
加油吧
优秀A你的等级是 A
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
Process finished with exit code 0