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 ...
随机推荐
- dpdpdpdp~~~!!!
dpdpdpdpdpdp D你妹个P! 妈的劳资就不信征服不了你!!哼!!
- UVA - 12345 带修改的莫队
题意显然:给出初始序列,单点修改,区间查询元素的种类. 由于时限过宽,暴力可过. 比较优秀的解法应该是莫队. 带修改的莫队题解可以看https://www.luogu.org/blog/user126 ...
- 关于Flask的默认session
Flask的默认session利用了Werkzeug的SecureCookie,把信息做序列化(pickle)后编码(base64),放到cookie里了. 过期时间是通过cookie的过期时间实现的 ...
- codeforces round 416 div2 补题 CF 811 A B C D E
A. Vladik and Courtesy 水题略过 #include<cstdio> #include<cstdlib> #include<cmath> usi ...
- 学生表、课程表、 成绩表 、教师表sql练习
转自:http://yuncode.net/code/c_58df7a8ca687e58 1.查询“1”课程比“2”课程成绩高的所有学生的学号: SELECT t1.student_id FROM ...
- 大神给你分析HTTPS和HTTP的区别(转)
http://www.php100.com/html/it/biancheng/2015/0209/8582.html 今天在做雅虎的时候,发现用第三方工具截取不到客户端与服务端的通讯,以前重来没碰到 ...
- Linux 下安装配置 JDK7(转载)
转自:http://dawndiy.com/archives/155/ 自从从Oracle收购Sun近三年来,已经有很多变化.早在8月,甲骨文将“Operating System Distributo ...
- bzoj 1620: [Usaco2008 Nov]Time Management 时间管理【贪心】
按s从大到小排序,逆推时间模拟工作 #include<iostream> #include<cstdio> #include<algorithm> using na ...
- codevs1183 泥泞的道路(01分数规划)
1183 泥泞的道路 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description CS有n个小区,并且任意小区之间都有两 ...
- [Swift]Array数组的swapAt函数
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...