当前位置:网站首页>【愚公系列】2022年7月 Go教学课程 015-运算符之赋值运算符和关系运算符
【愚公系列】2022年7月 Go教学课程 015-运算符之赋值运算符和关系运算符
2022-07-25 06:34:00 【愚公搬代码】
一、赋值运算符
赋值运算符的分类:
- 基本赋值运算符:基本的赋值运算符是“=”。一开始可能会以为它是“等于”,其实不是的。它实际上意味着把右边表达式的值赋给左边的运算数。
- 复合赋值运算符:复合的赋值运算符,又称为带有运算的赋值运算符,也叫赋值缩写。比如:+=、-=、*=、/=、%=。
1.赋值运算符的概念
| 运算符 | 说明 | 示例 |
|---|---|---|
| = | 普通赋值 | c = a + b 将 a + b 表达式结果赋值给 c |
| += | 相加后再赋值 | c += a 等价于 c = c + a |
| -= | 相减后再赋值 | c -= a 等价于 c = c - a |
| *= | 相乘后再赋值 | c *= a 等价于 c = c * a |
| /= | 相除后再赋值 | c /= a 等价于 c = c / a |
| %= | 求余后再赋值 | c %= a 等价于 c = c % a |
相关案例:
package main
import "fmt"
func main() {
var a = 10
fmt.Println("a=",a)
a += 2
fmt.Println("a += 2,a=",a)
a -= 2
fmt.Println("a -= 2,a=",a)
a *= 2
fmt.Println("a *= 2,a=",a)
a /= 2
fmt.Println("a /= 2,a=",a)
a %= 2
fmt.Println("a %= 2,a=",a)
}

二、关系运算符
关系运算符,有6种关系,分别为小于、大于、小于等于、大于等于、等于、不等于。
1.关系运算符的概念
| 运算符 | 说明 | 示例 |
|---|---|---|
| == | 检查两个值是否相等,如果相等返回 True 否则返回 False | (A == B) 为 False |
| != | 检查两个值是否不相等,如果不相等返回 True否则返回 False | (A != B) 为 True |
> | 检查左边值是否大于右边值,如果是返回 True否则返回 False | (A > B) 为 False |
| < | 检查左边值是否小于右边值,如果是返回 True 否则返回 False | (A < B) 为 True |
>= | 检查左边值是否大于等于右边值,如果是返回 True 否则返回 False | (A >= B) 为 False |
<= | 检查左边值是否小于等于右边值,如果是返回 True否则返回 False | (A <= B) 为 True |
相关案例:
package main
import "fmt"
func main() {
var a int = 21
var b int = 10
if a == b {
fmt.Printf("第一行 -a 等于 b\n")
} else {
fmt.Printf("第一行 -a 不等于 b\n")
}
if a < b {
fmt.Printf("第二行 -a 小于 b\n")
} else {
fmt.Printf("第二行 -a 不小于 b\n")
}
if a > b {
fmt.Printf("第三行 -a 大于 b\n")
} else {
fmt.Printf("第三行 -a 不大于 b\n")
}
a = 5
b = 20
if a <= b {
fmt.Printf("第四行 -a 小于等于 b\n")
}
if b >= a {
fmt.Printf("第五行 -a 大于等于 b\n")
}
}

边栏推荐
- Use of golang exec.command
- [unity3d] ugui callback function
- What determines the "personality" of AI robots?
- Detailed explanation of arm instruction CMP
- Baidu SEM bidding avoidance
- How to troubleshoot the problem of too many inodes
- Restrict Su command and sudo mechanism to promote nmap and console command netstat
- mysql数据库备份和恢复
- [C language] document processing and operation
- [cann training camp] play with the one-stop plan of cann target detection and recognition - learning notes 1 (initial experience)
猜你喜欢

Dry goods | training AI model can't find data? Collect 20 selected open source communities!

【transformer】DeiT

Do you know the same period last year in powerbi

【C】程序环境和预处理

【datawhale202207】强化学习:策略梯度和近端策略优化

Keil uvisin5 code auto completion or code Association

Labelme labels different objects, displays different colors and batch conversion

In container multicast

R strange grammar summary

What determines the "personality" of AI robots?
随机推荐
Scientific computing library numpy Foundation & Improvement (Understanding + explanation of important functions)
R strange grammar summary
[datawhale202207] reinforcement learning: the foundation of reinforcement learning
JZ7 重建二叉树
Shell script realizes the scheduled backup of MySQL database on two computers
Pic16f877xa instruction system (assembly language)
Detailed annotation and analysis of start.s of uboot
ARM裸板调试之JTAG调试源码级调试
C # read Beckhoff variable
Mysql database backup and recovery
Solve the problem of invalid modification of QT 5 interface. Solve the problem of invalid modification of qtdesigner
Brief tutorial of vbs script syntax (1)
A little consideration of strategic mode
VSCode 如何开启多个终端?如何横向显示?
Standard C language 89
Bug notes
[C language] in depth understanding of pointers and arrays (phase I)
[C language] document processing and operation
Analysis of the calling principle of Changan chain solid smart contract
四、MFC工具栏、运行时类信息机制、运行时创建机制