深入解析Go語言中for循環(huán)的寫法
for循環(huán)是一個循環(huán)控制結(jié)構(gòu),可以有效地編寫需要執(zhí)行的特定次數(shù)的循環(huán)。
語法
for循環(huán)在Go編程語言中的語法是:
for [condition | ( init; condition; increment ) | Range]
{
statement(s);
}
下面是控制在一個流程的for循環(huán):
如果condition是可用的,那么對于循環(huán)只要條件為真時執(zhí)行。
如果for子句是( init; condition; increment ) 存在則
初始化(init)步驟首先被執(zhí)行,并且只有一次。這一步可以聲明和初始化任何循環(huán)控制變量。不需要把一個聲明在這里,只要有一個分號出現(xiàn)。
接著,條件(condition)進(jìn)行了評估計(jì)算。如果為true,則執(zhí)行循環(huán)體。如果是假的,循環(huán)體不執(zhí)行,只是之后的for循環(huán)流量控制跳轉(zhuǎn)到下一條語句。
for循環(huán)執(zhí)行主體之后,控制流跳轉(zhuǎn)回到增量(increment)語句。此語句可以讓你更新任何循環(huán)控制變量。這個語句可以留空,只要一個分號出現(xiàn)條件后。
condition現(xiàn)在重新評估計(jì)算。如果為true,循環(huán)執(zhí)行的過程中重復(fù)(循環(huán)體,然后增加步,然后再次條件)。之后如果條件為假,則循環(huán)終止。
如果range可用,然后循環(huán)執(zhí)行的范圍內(nèi)的每個項(xiàng)目。
流程圖:
例子:
package main
import "fmt"
func main() {
var b int = 15
var a int
numbers := [6]int{1, 2, 3, 5}
/* for loop execution */
for a := 0; a < 10; a++ {
fmt.Printf("value of a: %d\n", a)
}
for a < b {
a++
fmt.Printf("value of a: %d\n", a)
}
for i,x:= range numbers {
fmt.Printf("value of x = %d at %d\n", x,i)
}
}
讓我們編譯和運(yùn)行上面的程序,這將產(chǎn)生以下結(jié)果:
value of a: 0 value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6 value of a: 7 value of a: 8 value of a: 9 value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6 value of a: 7 value of a: 8 value of a: 9 value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of x = 1 at 0 value of x = 2 at 1 value of x = 3 at 2 value of x = 5 at 3 value of x = 0 at 4 value of x = 0 at 5
Go語言嵌套for循環(huán)
Go編程語言允許使用一個循環(huán)內(nèi)嵌套另一個循環(huán)。下面的內(nèi)容展示幾個例子來說明這個概念。
語法
在Go語言中嵌套for循環(huán)語句的語法如下:
for [condition | ( init; condition; increment ) | Range]
{
for [condition | ( init; condition; increment ) | Range]
{
statement(s);
}
statement(s);
}
例子:
下面的程序使用嵌套for循環(huán)從2至100找出的素數(shù):
package main
import "fmt"
func main() {
/* local variable definition */
var i, j int
for i=2; i < 100; i++ {
for j=2; j <= (i/j); j++ {
if(i%j==0) {
break; // if factor found, not prime
}
}
if(j > (i/j)) {
fmt.Printf("%d is prime\n", i);
}
}
}
當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:
2 is prime 3 is prime 5 is prime 7 is prime 11 is prime 13 is prime 17 is prime 19 is prime 23 is prime 29 is prime 31 is prime 37 is prime 41 is prime 43 is prime 47 is prime 53 is prime 59 is prime 61 is prime 67 is prime 71 is prime 73 is prime 79 is prime 83 is prime 89 is prime 97 is prime
相關(guān)文章
Go框架三件套Gorm?Kitex?Hertz基本用法與常見API講解
這篇文章主要為大家介紹了Go框架三件套Gorm?Kitex?Hertz的基本用法與常見API講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-02-02Golang實(shí)現(xiàn)Json分級解析及數(shù)字解析實(shí)踐詳解
你是否遇到過在無法準(zhǔn)確確定json層級關(guān)系的情況下對json進(jìn)行解析的需求呢?本文就來和大家介紹一次解析不確定的json對象的經(jīng)歷,以及遇到的問題和解決方法2023-02-02golang 接口嵌套實(shí)現(xiàn)復(fù)用的操作
這篇文章主要介紹了golang 接口嵌套實(shí)現(xiàn)復(fù)用的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04Mac下Vs code配置Go語言環(huán)境的詳細(xì)過程
這篇文章給大家介紹Mac下Vs code配置Go語言環(huán)境的詳細(xì)過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-07-07