当前位置:网站首页>Self study golang [3.3go language loop statement] for loop syntax structure, omit initial conditions, omit incremental conditions, omit the application of end conditions
Self study golang [3.3go language loop statement] for loop syntax structure, omit initial conditions, omit incremental conditions, omit the application of end conditions
2022-07-22 08:35:00 【Aspire to be a doctor】
Catalog
1, A simple for Circular case :1-100 The sum of the
2, Omit the application of initial conditions :10 Turn into the system 2 Base number
4,for Application of loop omitting end condition : Dead cycle
End of this chapter , Run code summary :
1, A simple for Circular case :1-100 The sum of the
Run code :
package main
import "fmt"
// Loop statement for
//for We don't need brackets in the condition
//for The initial condition can be omitted from the condition of , The end condition , Incremental expression
func main() {
sum := 0// Definition sum The initial value is 0
for i := 1; i <= 100; i++ {//i from 1 To 100, In steps of 1
sum += i// Add once per cycle
}
fmt.Println(sum)
}
Output :5050
Run a screenshot :
2, Omit the application of initial conditions :10 Turn into the system 2 Base number
Run code :
func tobin(n int) string { // Integer to binary function , The value of the function is an integer , The return value of the function is a string
result := ""
for ; n > 0; n /= 2 {
lsb := n % 2
result = strconv.Itoa(lsb) + result //itoa The function is to convert the integer into characters
}
return result
}
Output :101 1101
Run a screenshot :
3, Omit the application of incremental conditions : Read abc.txt Output the contents in the file ,go No, while, It can be used for replace
Run code :
// Omit the application of incremental conditions ,go No, while, It can be used for replace
func printfile(filename string) {
file, err := os.Open(filename) // Use os.open Open a file , Two parameters are returned
//file It's a document ,err Is the error message
if err != nil { // If err It's not equal to nil Description error
panic(err) // Use panic Function to terminate the program and report an error
}
scanner := bufio.NewScanner(file) // Definition scanner For reading content
for scanner.Scan() { //for The loop omits the increment condition and has only one end condition
fmt.Println(scanner.Text()) // Output the contents of the file line by line
}
}
Run a screenshot :
4,for Application of loop omitting end condition : Dead cycle
Run code :
//for Application of loop omitting end condition
func forever() {
for {
fmt.Println("abc")
}
}
Run a screenshot :
End of this chapter , Run code summary :
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
// Omit the application of initial conditions :10 Turn into the system 2 Base number
func tobin(n int) string { // Integer to binary function , The value of the function is an integer , The return value of the function is a string
result := ""
for ; n > 0; n /= 2 {
lsb := n % 2
result = strconv.Itoa(lsb) + result //itoa The function is to convert the integer into characters
}
return result
}
// Omit the application of incremental conditions ,go No, while, It can be used for replace
func printfile(filename string) {
file, err := os.Open(filename) // Use os.open Open a file , Two parameters are returned
//file It's a document ,err Is the error message
if err != nil { // If err It's not equal to nil Description error
panic(err) // Use panic Function to terminate the program and report an error
}
scanner := bufio.NewScanner(file) // Definition scanner For reading content
for scanner.Scan() { //for The loop omits the increment condition and has only one end condition
fmt.Println(scanner.Text()) // Output the contents of the file line by line
}
}
//for Application of loop omitting end condition
func forever() {
for {
fmt.Println("abc")
}
}
func main() {
fmt.Println(
tobin(5), //101
tobin(13), //1101
)
printfile("abc.txt")
forever()
}
边栏推荐
- 软考中级【数据库系统工程师】第0章:如何自学备考,考试介绍考什么,备考教材,上午和下午的体型分数分布,备考课程链接,个人备考感谢
- How excel automatically matches gender code according to ID number
- 十年交易员重磅推荐:简单易操作的突破回调策略
- 机器学习之时间序列
- 阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践
- SPI调试不成功很有可能是你线接错了!!
- 不断提升认知,从而达到交易的最高级别——稳定盈利(终)
- excel 如何根据身份证号自动匹配性别代码
- How to cache with blob object in browser
- 数据分析与挖掘2
猜你喜欢
The only way to advance transactions: from Xiaobai to stable profits (III)
软考中级【数据库系统工程师】第0章:如何自学备考,考试介绍考什么,备考教材,上午和下午的体型分数分布,备考课程链接,个人备考感谢
七/四/五层网络模型
服务器切换不同的conda环境以及查看各个用户进程
Verilog——74LVC161计数器
Error: L6218E: Undefined symbol SX1276FskGetPacketCrcOn (referred from sx1276-fsk.o)
Personal summary of the difference between on and where in MySQL -- make clear where the conditions should be written
Seven / four / five layer network model
8位补码booth一位乘法器
小波变换中的多贝西小波(DB小波函数)概述
随机推荐
excel 如何删除有颜色的行
MySQL中MyISAM和InnoDB的区别与实现
MySQL存储过程
C语言指针和数组a与&a,&a[0]等的区别
How to cache with blob object in browser
Verilog - Basketball 24s timer
How to open stdio in C h ? How to find the definition of printf?
Hide and unhide Excel
Verilog -- Serial four bit adder and leading four bit adder 74hc283
CDH 6.1 environment construction graphic tutorial
Percona xtradb cluster installation
自学golang【第三章:第一个go语言程序】使用goland创建第一个go程序,main函数与init函数,使用go实现运行windows命令,实现cmd的数据输出
深度学习之Caffe框架及制作数据源
不用编程也能做应用开发?能!
量化交易日记-回撤分析-2021年02月6日
NOR FLASH 和 NAND FLASH异同
Detailed construction process of airflow (personal test + summary)
pytorch学习(一):线性回归
科技创造价值 | 云扩科技荣登真榜·中国科技创新品牌榜TOP100
How excel automatically matches gender code according to ID number