当前位置:网站首页>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);![]()
边栏推荐
- Mysql common commands
- Mysql常用命令大全
- Block elements, inline elements (elements, span elements)
- 18 years of programmer career, read more than 200 programming books, pick out some essence to share with you
- Stress testing and performance analysis of node projects
- Linux CentOS8安装Redis6
- 浏览器的onload事件
- Matlab paper illustration drawing template No. 41 - bubble chart (bubblechart)
- C竞赛训练
- Use the browser's local storage to realize the function of remembering the user name
猜你喜欢

Alluxio为Presto赋能跨云的自助服务能力

Detailed explanation of mysql stored procedure

Install and use Google Chrome
[email protected](using passwordYES)"/>Navicat报错:1045-Access denied for user [email protected](using passwordYES)

ELK日志分析系统

【解决】RESP.app 连接不上redis

APP Bluetooth connection test of test technology

H5接入支付流程-微信支付&支付宝支付

eggjs controller层调用controller层解决方案

C language: Check for omissions and fill in vacancies (3)
随机推荐
H5如何实现唤起APP
51单片机外设篇:点阵式LCD
The Go language learning notes - dealing with timeout - use the language from scratch from Context
How H5 realizes evoking APP
Grid布局介绍
C语言中i++和++i在循环中的差异性
MySQL 8.0.29 set and modify the default password
Android studio connects to MySQL and completes simple login and registration functions
整合ssm(一)
12 reasons for MySQL slow query
The company does not pay attention to software testing, and the new Ali P8 has written a test case writing specification for us
一线大厂软件测试流程(思维导图)详解
How Navicat Connects to MySQL
Introduction to Grid Layout
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
[PSQL] window function, GROUPING operator
Redis-集群模式(主从复制模式,哨兵模式,集群化模式)
C语言小游戏——扫雷小游戏
kubernetes 亲和、反亲和、污点、容忍
C language: Check for omissions and fill in vacancies (3)