当前位置:网站首页>变量交换;复合赋值;增递减运算符
变量交换;复合赋值;增递减运算符
2022-08-01 16:48:00 【妍小妍】
变量交换:
#include <stdio.h>
int main()
{
int a=5;
int b=6;
int t;
t=a;
a=b;
b=t;
printf("a=%d,b=%d\n",a,b);
return 0;
}程序是按部执行的,而不是关系;
若想要a与b的值互换,可以在设另一个变量进行三者交换,按布执行程序;
t=a:将a的值赋予t; a=b:将b的值再赋予a; b=t:将t的值(也就是a的值)再赋予b,这样就完成了a与b的值互换,因为程序是按部执行的;而不是a=b,b=a这么简单;
复合赋值:
五个算术运算符"+,-,*,/,%"都可以和赋值运算符"="结合起来形成复合赋值运算符;
例如:
total+=5 → total=total+5;
total+=(sum+100)/2 → total=total+(sum+100)/2 ;
total*=sun+12 → total=total*(sun+12);
total/=12+6 → total=total/(12+6);注意:两个运算符中间不能有空格,且赋值运算符右边本来的数字要先算!
增递减运算符
"++"和 " -- " 是两个很特殊的运算符,它们是单目运算符,这个算子还必须是变量。这两个运算符分别叫做递增和递减运算符,他们的作用就是给这个变量+1或者-1.
例如:
count++ → count+=1 → count=count+1;前缀后缀形式:
++和-可以放在变量的前面,叫做前缀形式,也可以放在变量的后面,叫做后缀形;
a++的值是a加1以前的值,而++a的值是加了1以后的值,无论哪个,a自己的值都加了I了。
count++运算结果是给count加1,表达式的值是count原来的值;
++count 运算结果是给count加1 ,表达式的值是count+1以后的值;
count-- 运算结果是 给count减1, 表达式的值是count原来的值;
--count 运算结果是 给count减1, 表达式的值是count-1以后的值;
边栏推荐
- C#中关于DevExpress的常用操作和帮助类项目工程内容说明
- 【R语言】对图片进行裁剪 图片批量裁剪
- ROS2支持技术:DDS简述
- AntDB数据库亮相24届高速展,助力智慧高速创新应用
- 请问数据库中报错信息如下,mongoshake 有什么配置的方式解决这种大消息问题吗?
- [Dark Horse Morning Post] Hu Jun's endorsement of Wukong's financial management is suspected of fraud, which is suspected to involve 39 billion yuan; Fuling mustard responded that mustard ate toenails
- 阿里官方 Redis 开发规范
- 2022 Strong Net Cup CTF---Strong Net Pioneer ASR wp
- C语言:表达式求值详解
- intentservice使用(Intention)
猜你喜欢
随机推荐
二分练习题
Using Canvas to achieve web page mouse signature effect
首席工程师究竟是怎样的存在?
软件测试谈薪技巧:同为测试人员,为什么有人5K,有人 20K?
吴恩达机器学习课后习题——kmeans
04 flink 集群搭建
untiy Resorces目录动态加载资源
DOM series of touch screen events
金仓数据库 KDTS 迁移工具使用指南(3. 系统部署)
mysql源码分析——聚簇索引
Go unit tests
Ant discloses the open source layout of core basic software technology for the first time
【Unity,C#】哨兵点位循迹模板代码
Ali's official Redis development specification
素域和扩域
Path helper class for C#
MySQL's maximum recommended number of rows is 2000w, is it reliable?
Isometric graph neural networks shine in drug discovery
DOM系列之触屏事件
DevExpress的GridControl帮助类









