網(wǎng)站首頁 編程語言 正文
Go語言和其他語言不一樣,它只有一種循環(huán)方式,就是for語句
可以參考如下公式:
for initialisation; condition; post{ //Do Something }
執(zhí)行順序
- a.執(zhí)行一次initialisation,初始化
- b.判斷condition
- c.條件為true,執(zhí)行{}內的語句
- d.語句執(zhí)行之后執(zhí)行post
使用方式舉例:
1.基本使用類似其他語言的for
func ForTest1(){ for i:=1;i<=10;i++{ fmt.Printf("i=%d\t",i) } fmt.Println() }
2.替代while語句
func ForTest2(){ i:=1 for ;i<=10; { i=i+2 fmt.Printf("i=%d\t",i) } fmt.Println() //等價于 for i<=10 { i=i+2 fmt.Printf("i=%d\t",i) fmt.Println() } }
3.多條件(多重賦值)
func ForTest3(){ for x,y:=1,10; x<10 && y>1; x,y = x+1,y-1{ fmt.Printf("x=%d\t",x) fmt.Printf("y=%d\t",y) fmt.Println() } fmt.Println() }
4.無限循環(huán)
func ForTest4(){ count:=1 for { fmt.Printf("Hello\t") if(count == 3){ break } count++ } }
運行結果如下:
-----ForTest1-------
i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10 ??
-----ForTest2-------
i=3 i=5 i=7 i=9 i=11 ??
-----ForTest3-------
x=1 y=10 ??
x=2 y=9
x=3 y=8
x=4 y=7
x=5 y=6
x=6 y=5
x=7 y=4
x=8 y=3
x=9 y=2
-----ForTest4-------
Hello ? Hello ? Hello
原文鏈接:https://www.cnblogs.com/dcz2015/p/10443055.html
相關推薦
- 2022-09-04 python開發(fā)任意表達式求值全功能示例_python
- 2024-03-18 JDK版本對應其bytecode version (major version)
- 2022-09-18 ASP.NET?Core實現(xiàn)文件上傳和下載_實用技巧
- 2022-09-25 aspnet core中間件 短路 Response.WriteAsync正確用法
- 2023-03-29 Python本地搭建靜態(tài)Web服務器的實現(xiàn)_python
- 2024-01-29 在springboot多數(shù)據(jù)源項目中創(chuàng)建多個事務(解決@Transactional影響切換數(shù)據(jù)源問題
- 2022-06-06 flutter 導航組件 AppBar (含頂部選項卡TabBar,抽屜菜單 drawer ,自定義
- 2022-06-12 使用c++實現(xiàn)異或加密的代碼示例_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支