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 ...
随机推荐
- 在vs2017中创建Node.js项目
https://github.com/Microsoft/nodejstools/wiki/Projects 1. 安装vs2017的时候,需要勾选Node.js 2.新建项目的时候,选择其他语言,然 ...
- 8-15 globalCompositeOperation阶段练习二
8-15 globalCompositeOperation阶段练习二 <!DOCTYPE html> <html lang="zh-cn"> <hea ...
- luogu 3375 【模板】KMP字符串匹配
我太菜了 今天才学会kmp #include<iostream> #include<cstdio> #include<algorithm> #include< ...
- c# 生成 xml 文件
方法一: using System; using System.Xml; using System.IO; using System.Text; public class ReadWriteXml { ...
- vue用户登录状态判断
之前项目中用来判断是否登录我写了多种方案,但是最终只有一个方案是比较好的,这篇博客就是分享该方案; 先说基本要求: 项目中的登录状态是依据服务器里的状态来作为判断依据; 每一个需要登录后才能操作的接口 ...
- [LOJ#10064]黑暗城堡
Description 在顺利攻破 Lord lsp 的防线之后,lqr 一行人来到了 Lord lsp 的城堡下方.Lord lsp 黑化之后虽然拥有了强大的超能力,能够用意念力制造建筑物,但是智商 ...
- 构造 Codeforces Round #107 (Div. 2) B. Phone Numbers
题目传送门 /* 构造:结构体排个序,写的有些啰嗦,主要想用用流,少些了判断条件WA好几次:( */ #include <cstdio> #include <algorithm> ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)
题目描述 We consider a positive integer perfect, if and only if it is equal to the sum of its positive d ...
- [ Luogu 1273 ] 有线电视网
\(\\\) \(Description\) 一棵\(N\)个节点的树,编号在\([N-M+1,N]\)内的点必定为叶子节点,且这些点都有一个收益值\(Val_i\),同时每一条树边都有一个代价. 访 ...
- 图标文件ico制作以及使用说明
今天说一个图标文件——ico.我们在pc端浏览网页的时候网页栏那块都会显示一个本网站特有的图片,就是我们说的ico了.示例:<link href="image/favicon.ico& ...