5.Go by Example: For

Go by Example: For


for is Go’s only looping construct. Here are three basic types of for loops.

The most basic type, with a single condition.

A classic initial/condition/after for loop.

for without a condition will loop repeatedly until you break out of the loop or return from the enclosing function.


package main
import "fmt"
func main() {
 
    i := 1
    for i <= 3 {
        fmt.Println(i)
        i = i + 1
    }
 
    for j := 7; j <= 9; j++ {
        fmt.Println(j)
    }

    for {
        fmt.Println("loop")
        break
    }
}


译:
for 关键字是在go语言中唯一一个循环结构,下面的就是for循环的三个基本类型结构

最基本的是一个单条件

一个是 初始值/条件/然后的自身处理条件对于 for循环

for 循环对于没有条件的将会一直循环直到break出循环,或者在外围函数返回


package main

import "fmt"

func main() {

	i := 1
	for i <= 3 { //相当于while循环
		fmt.Println(i)
		i = i + 1
	}

	for j := 7; j <= 10; j++ { //传统的for循环
		fmt.Println(j)
	}

	for { //类似于 do。。。while循环
		fmt.Println("loop")
		break
	}

}

运行结果
go run for.go
1
2
3
7
8
9
10

loop


原文地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值