方法1
/*普通用法*/
func main(){
var count int = 5;
var i int
for i = 1;i <= count; i++{
fmt.Println("This is ",i)
}
}
方法2
/*缺省初始化*/
func main(){
var count int = 5;
var i int = 1
for ;i <= count; i++{
fmt.Println("This is ",i)
}
}
方法3
/*缺省执行动作*/
func main(){
var count int = 5;
var i int = 1
for i <= count{
fmt.Println("This is ",i)
i += 1
}
}
方法4
/*条件恒true,相当于while(1)*/
func main(){
var count int = 5
var i int = 1
for true{
fmt.Println("This is ",i)
i += 1
if i == count +1 {
break
}
}
}
方法5
/*什么都没有类型for(;;)*/
func main(){
var count int = 5;
var i int = 1
for {
fmt.Println("This is ",i)
i++
if i== count +1 {
break
}
}
}
以上的运行结果均为:
|
请发表评论