当前位置:网站首页>【愚公系列】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")
}
}

边栏推荐
- JTAG debugging source level debugging of arm bare board debugging
- How to troubleshoot the problem of too many inodes
- [cann training camp] play with the one-stop plan of cann target detection and recognition - learning notes 1 (initial experience)
- How does vscode enable multiple terminals? How to display horizontally?
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 2 link with game glitch (SPFA finds positive and negative links)
- Ant design input search box listens for allowclear event separately
- C # --metroframework framework calls the metromodernui library and uses it in the toolbar
- Application of first-order complementary filtering to STM32 ADC acquisition
- JS array = number assignment changes by one, causing the problem of changing the original array
- Shell script realizes the scheduled backup of MySQL database on two computers
猜你喜欢

LeetCode46全排列(回溯入门)

Restrict Su command and sudo mechanism to promote nmap and console command netstat

Do you know the same period last year in powerbi

Keilc51 usage details (III)

C # --metroframework framework calls the metromodernui library and uses it in the toolbar

R奇怪语法总结

DOM event type

labelme标注不同物体显示不同颜色以及批量转换

【Unity3D】UGUI回调函数

C control open source library: download of metroframework
随机推荐
The most comprehensive multi-threaded application tutorial - summary in detail
Application of first-order complementary filtering to STM32 ADC acquisition
长安链Solidity智能合约调用原理分析
Data too long for column 'data' at row 1 and the garbled code caused by setting to longblob are solved. node-mysql
The LAF protocol elephant of defi 2.0 may be one of the few profit-making means in your bear market
JS 获取鼠标选中的文字并处于选中状态
Restrict Su command and sudo mechanism to promote nmap and console command netstat
GF Securities online account opening? Is it safe?
C # read Beckhoff variable
JZ7 rebuild binary tree
How does vscode enable multiple terminals? How to display horizontally?
[Luogu p6629] string (runs) (tree array)
target_compile_features specified unknown feature “cxx_std_14“ for target
Do you know the same period last year in powerbi
2022 "strong country Cup" preliminary WP (with script and detailed process)
C language -c51 compilation warning "* * * warning l1: unresolved external symbol" and extern
Qt 5界面修改无效的问题解决QtDesigner修改之后无效的解决办法
[C language] document processing and operation
【luogu P6629】字符串(Runs)(树状数组)
VBS common built-in functions (2)