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 ...
随机推荐
- linux下I2C驱动架构全面分析【转】
本文转载自:http://blog.csdn.net/wangpengqi/article/details/17711165 I2C 概述 I2C是philips提出的外设总线. I2C只有两条线,一 ...
- 8-13 canvas专题-阶段练习二(下)
8-13 canvas专题-阶段练习二(下) <!DOCTYPE html> <html lang="zh-cn"> <head> <me ...
- [翻译]NUnit---Exception && Utility Methods (六)
网址:http://www.cnblogs.com/kim01/archive/2013/04/01/2994378.html Exception Asserts (NUnit 2.5) Assert ...
- bzoj 1620: [Usaco2008 Nov]Time Management 时间管理【贪心】
按s从大到小排序,逆推时间模拟工作 #include<iostream> #include<cstdio> #include<algorithm> using na ...
- sql server 大数据处理
对SQL Server数据表进行分区的过程分为三个步骤: 1)建立分区函数 2)建立分区方案 3)对表格进行分区 第一个步骤:建立分区函数 分区函数定义[u]how[/u],即你想要SQL Serve ...
- OAuth2.0最简向导
无论你是否有技术背景,你都能看懂授权协议框架OAuth2.0 翻译来自:川崎高彦对自己投资人讲解自己的SaaS安全产品. Got it! https://medium.com/@darutk/the- ...
- 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries
题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...
- [转]微信开发.Net 接入示例
本文转自:http://my.oschina.net/lcak/blog/219618 微信公众平台接口开发官方仅提供了 PHP 接入示例代码, 网上找到的.Net代码多半需要积分下载, 所以自己写了 ...
- CSS布局整理
目录 常用居中方法 水平居中 垂直居中 单列布局 二列&三列布局 float+margin position+margin 圣杯布局(float+负margin) 双飞翼布局(float+负m ...
- Dojo - 操作Dom的函数
DOM Manipulation You might be seeing a trend here if you have gotten this far in the tutorial, in th ...