当前位置:网站首页>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);![]()
边栏推荐
- 【解决】RESP.app 连接不上redis
- Shuttle + Alluxio 加速内存Shuffle起飞
- Brush LeetCode topic series - 10. Regular expression match
- 51单片机外设篇:点阵式LCD
- 18年程序员生涯,读了200多本编程书,挑出一些精华分享给大家
- 25K test old bird's 6-year experience in interviews, four types of companies, four types of questions...
- Detailed explanation of interface in Go language
- Block elements, inline elements (elements, span elements)
- [PSQL] 函数、谓词、CASE表达式、集合运算
- MySQL 8.0.29 decompressed version installation tutorial (valid for personal testing)
猜你喜欢

Detailed explanation of interface in Go language

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

goroutine (coroutine) in go language

腾讯大咖分享 | 腾讯Alluxio(DOP)在金融场景的落地与优化实践

coredns介绍

MySQL数据表的基本操作和基于 MySQL数据表的基本操作的综合实例项目

MySql copies data from one table to another table

Navicat如何连接MySQL

Detailed explanation of AMQP protocol

leetcode每天5题-Day04
随机推荐
利用浏览器本地存储 实现记住用户名的功能
Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
高防服务器防御的原理是什么
Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
165.比较版本号
5年在职经验之谈:2年功能测试、3年自动化测试,从入门到不可自拔...
Meta公司内部项目-RaptorX:将Presto性能提升10倍
[PSQL] 函数、谓词、CASE表达式、集合运算
nacos注册中心
C language: Check for omissions and fill in vacancies (3)
自动化运维工具——ansible、概述、安装、模块介绍
Brush LeetCode topic series - 10. Regular expression match
PSQL function, predicate, CASE expression and set operations
[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays
Redis database
Redis-----非关系数据库
apisix-Getting Started
Go语言之interface详解
apifox介绍及使用(1)。
Navicat new database