Go:条件语句、循环语句
一、条件语句
package main import (
"fmt"
"io/ioutil"
) // if的条件不需要括号 func xx(i int) int {
if i == 1 {
return 1
} else if i == 2{
return 2
} else {
return 0
}
} func eval(a, b int, operator string) int {
var result int
switch operator {
case "+":
result = a + b
case "-":
result = a - b
case "*":
result = a * b
case "/":
result = a / b
default:
// panic相当于报错
panic("unsupported operator:" + operator)
}
return result
} func getGrade(score int) string {
grade := ""
// switch后面可以没有表达式,在case中实现即可
switch {
case score == 100:
grade = "A+"
case score >= 90 && score< 100:
grade = "A"
case score >= 80 && score< 90:
grade = "B"
}
return grade
} func main() {
fmt.Println(xx(10)) // 0 const filename = "test.txt"
if contents, err := ioutil.ReadFile(filename); err != nil {
fmt.Println(err)
} else {
fmt.Printf("%s\n", contents)
/*
千山鸟飞绝,万径人踪灭。
孤舟蓑笠翁,独钓寒江雪。
*/
} fmt.Println(eval(10, 20, "+")) // 30 fmt.Println(getGrade(95)) // A
}
二、循环语句
package main import (
"bufio"
"fmt"
"os"
"strconv"
) // for的条件不需要括号
// for的条件可以省略初始条件、结束条件、递增表达式 func convertToBinary(i int) string {
result := ""
for ; i > 0; i /= 2 {
tmp := i % 2
result = result + strconv.Itoa(tmp)
}
return result
} func printFileContent(filename string) {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(file)
// 只有结束条件
for scanner.Scan() {
fmt.Println(scanner.Text())
}
} func foreverCircle() {
// 死循环
for {
fmt.Println("1")
}
} func printStr1(s string) {
for index, value := range s {
fmt.Printf("index:%d value:%c\n", index, value)
/*
index:0 value:H
index:1 value:e
index:2 value:l
index:3 value:l
index:4 value:o
index:5 value:
index:6 value:W
index:7 value:o
index:8 value:r
index:9 value:l
index:10 value:d
*/
}
} func printStr2(s string) {
for index, value := range s {
if index < 5 {
fmt.Printf("index:%d value:%c\n", index, value)
/*
index:0 value:H
index:1 value:e
index:2 value:l
index:3 value:l
index:4 value:o
*/
continue
} else if index == 5 {
break
}
}
} func main() {
fmt.Println("1")
fmt.Println(
convertToBinary(1), // 1
convertToBinary(2), // 01
convertToBinary(5), // 101
convertToBinary(10), // 0101
) printFileContent("test.txt")
/*
千山鸟飞绝,万径人踪灭。
孤舟蓑笠翁,独钓寒江雪。
*/ //foreverCircle() printStr1("Hello World") printStr2("Hello World")
}
三、goto语句(不推荐使用)
package main
import "fmt"
func func1() {
LABEL:
for i := 0; i <= 5; i++ {
for j := 0; j <= 5; j++ {
if j == 4 {
continue LABEL // LABEL相当于标志,程序跳到此处执行
}
fmt.Printf("i is: %d, and j is: %d\n", i, j)
}
}
}
func func2() { // 相当于for循环
i := 0
LABEL:
fmt.Println(i)
i++
if i == 5 {
return
}
goto LABEL
}
func main() {
func1()
func2()
}
Go:条件语句、循环语句的更多相关文章
- Python 2.7 学习笔记 条件与循环语句
本文介绍下python条件和循环语句的语法 一.if条件语句 语法格式如下: if 表达式: .... elif 表达式: .... elif 表达式: .... else: ..... 说明:与其它 ...
- Python中的条件和循环语句
条件和循环语句 1. 条件语句 if单用 格式:if 条件表达式 例如:if 5 > 3: print('True') >>> 'True' #当条件满足时才会执行上述操作. ...
- #7 Python顺序、条件、循环语句
前言 上一节讲解了Python的数据类型和运算,本节将继续深入,涉及Python的语句结构,相当于Python的语法,是以后编写程序的重要基础! 一.顺序语句 顺序语句很好理解,就是按程序的顺序逻辑编 ...
- 【Python】-NO.99.Note.4.Python -【Python3 条件语句 循环语句】
1.0.0 Summary Tittle:[Python]-NO.99.Note.4.Python -[Python3 条件语句 循环语句] Style:Python Series:Python Si ...
- python系列八:Python3条件控制&循环语句
#!/usr/bin/python #-*-coding:gbk-*-#Python3 条件控制&循环语句import randomage = int(input("请输入你的年龄: ...
- python实例 条件和循环语句
#! /usr/bin/python #条件和循环语句 x=int(input("Please enter an integer:")) if x<0: x=0 ...
- python学习第四讲,python基础语法之判断语句,循环语句
目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...
- python之最强王者(3)——变量,条件、循环语句
1.Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的 ...
- PL/SQL学习(二)条件和循环语句
原文参考:http://plsql-tutorial.com/ PLSQL条件语句 IF THEN ELSE STATEMENT 1) IF condition THEN statement ...
- r语言之条件、循环语句
if条件语句:if (conditon) {expr1} else {expr2} > x<-1> if(x==1)+ {x<-"x=1"}else+ {x ...
随机推荐
- 下载、编译、运行android 7.1系统(ubuntu 16.0.4)【转】
本文转载自:http://blog.csdn.net/andywuchuanlong/article/details/53977710
- SQL Server: Difference between PARTITION BY and GROUP BY
https://stackoverflow.com/questions/2404565/sql-server-difference-between-partition-by-and-group-by ...
- xubuntu 17.04 和 iphone 6互传文件方法——使用libimobiledevice就可以像u盘一样操作文件了
I need to preface this by saying I'm also new to Linux, but I've got it working I think. The instruc ...
- P2610 [ZJOI2012]旅游 树的直径
这个题就是建图不太好建,但是我们一想,三角形貌似只能两两挨着,最后会变成一个二叉树,所以问题就变成求树的直径.建图用pair套map超级简单. 题干: 到了难得的暑假,为了庆祝小白在数学考试中取得的优 ...
- bzoj4195 [Noi2015]程序自动分析——并查集
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4195 突然在这道大水题上WA了半天... 思路很简单,离线处理询问,先把 = 的都加到并查集 ...
- argis地图
- jQuery——表单应用(1)
实现结果:聚焦表单的input部分时,input格式变更为CSS样式(获取和失去焦点改变样式) HTML: <!DOCTYPE html> <html> <head> ...
- WEB前端学习
第一日:软件的安装和下载 1.百度搜索推荐使用webStorm前端神器进行开发,傻瓜式安装不必多说! 激活 前提:修改本地的hosts配置文件(/etc/hosts) 最后一行新增这句话:0.0.0. ...
- [转]c 语言中 %d,%lu等区别
转载至:http://blog.sina.com.cn/s/blog_7d94c35c01019f96.html %d 有符号10进制整数 %ld 长整型 %hd短整型 %hu 无符号短整形 %u无符 ...
- GIt学习之路 第二天 创建版本库
本文参考廖雪峰老师的博客进行总结,完整学习请转廖雪峰博客 创建版本库 阅读: 1859216 什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文 ...