当前位置:网站首页>Differences between i++ and ++i in loops in C language
Differences between i++ and ++i in loops in C language
2022-08-02 06:20:00 【rotten god】
Foreword
I encountered it when I was brushing the questions today, and I recorded it in a shallow way (maybe I don't know).In the C language, i++ and ++i both represent self-increment, the difference is that ++i is incremented first and then assigned, while i++ is incremented first.I think there are beginners like me who have been wondering before: they are both similar, so when to use i++ and when to use ++i?I just learned today that the judgment mechanism of i++ and ++i in the loop is different.
FOR loop
i++ and ++i are the same in the for loop, they are both judged first and then added.
for (int i = 0; i < 5; i++){cout << i << " ";}for (int i = 0; i < 5; ++i){cout << i << " ";}
The output is the same.
while loop
In the while loop, i++ and ++i are different: i++ is judged first, then incremented, and then entered into the loop body:
int i = -5;while (i++ < 0){cout << i << " ";}
In the above code, first judge that i == -5 is less than zero, then increment i = i + 1, and finally enter the loop;
And ++i is to increase first, then judge and then enter the loop body:
i = -5;while (++i < 0){cout << i << " ";}
In the above code, first increment i = i + 1, then judge that i == -4 is less than zero, and finally enter the loop;
The test results are as follows:
do-while loop
The i++ and ++i in the do-while loop are the same as in the while loop, except that the do-while executes the loop body first:
cout << "do-while loop i++:";i = -5;do{cout << i << " ";} while (i++ < 0);cout << "do-while loop++i:";i = -5;do{cout << i << " ";} while (++i < 0);
边栏推荐
- 6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
- Mysql common commands
- 51 MCU Peripherals: Infrared Communication
- ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
- 服务器的单机防御与集群防御
- Introduction and use of apifox (1).
- 自动化运维工具——ansible、概述、安装、模块介绍
- 配合蓝牙打印的encoding-indexes.js文件内容:
- 复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式
- The company does not pay attention to software testing, and the new Ali P8 has written a test case writing specification for us
猜你喜欢
Introduction to Grid Layout
How much does a test environment cost? Start with cost and efficiency
Google notes cut hidden plug-in installation impression
51单片机外设篇:DS18B20
25K测试老鸟6年经验的面试心得,四种公司、四种问题…
Google Chrome(谷歌浏览器)安装使用
Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
100 latest software testing interview questions in 2022, summary of common interview questions and answers
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
整合ssm(一)
随机推荐
12 reasons for MySQL slow query
apifox介绍及使用(1)。
Brush LeetCode topic series - 10. Regular expression match
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范
[PSQL] 窗口函数、GROUPING运算符
[PSQL] 函数、谓词、CASE表达式、集合运算
Mysql implements optimistic locking
Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
在腾讯做外包测试的那些日子.....
Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
goroutine (coroutine) in go language
LeetCode刷题系列 -- 10. 正则表达式匹配
mysql实现按照自定义(指定顺序)排序
复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式
MySQL 8.0.29 set and modify the default password
C竞赛训练
浏览器的onload事件
腾讯大咖分享 | 腾讯Alluxio(DOP)在金融场景的落地与优化实践