当前位置:网站首页>C语言中i++和++i在循环中的差异性
C语言中i++和++i在循环中的差异性
2022-08-02 05:06:00 【摆烂的神】
前言
今天刷题时遇到的,浅浅记录一下(可能就我不知道)。C语言中i++和++i都表示自增,不同的是++i是先增加再赋值,而i++是先赋值在增加。我觉得有和我一样的初学者,在之前一直有疑问:它们两个都差不多,那到底什么时候用i++,什么时候用++i?今天才了解到原来i++和++i在循环中的判断机制也不一样。
FOR循环
for循环中i++和++i是一样的,都是先判断再相加。
for (int i = 0; i < 5; i++)
{
cout << i << " ";
}
for (int i = 0; i < 5; ++i)
{
cout << i << " ";
}输出结果是一样的。
![]()
while循环
在while循环中,i++和++i就不一样了:i++是先判断再增加再进入循环体:
int i = -5;
while (i++ < 0)
{
cout << i << " ";
}如上代码中,先判断i == -5满足小于零,再自增 i = i + 1,最后进循环;
而++i则是先增加再判断再进入循环体:
i = -5;
while (++i < 0)
{
cout << i << " ";
}如上代码中,先自增 i = i + 1,再判断 i == -4 满足小于零,最后进循环;
测试结果如下:
![]()
do-while循环
在do-while循环中和while循环中的i++和++i是一样的,只不过do-while先执行了一次循环体:
cout << "do-while循环i++:";
i = -5;
do
{
cout << i << " ";
} while (i++ < 0);
cout << "do-while循环++i:";
i = -5;
do
{
cout << i << " ";
} while (++i < 0);![]()
边栏推荐
- MySQL 5.7详细下载安装配置教程
- Google notes cut hidden plug-in installation impression
- 本周大新闻|苹果MR已进行Pre-EVT测试,Quest 2涨价100美元
- navicat新建数据库
- el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
- golang环境详细安装、配置
- AMQP协议详解
- MySQL如何创建用户
- nacos注册中心
- Review: image saturation calculation formula and image signal-to-noise (PSNR) ratio calculation formula
猜你喜欢

Detailed explanation of mysql stored procedure

腾讯注册中心演进及性能优化实践

【C语言】LeetCode26.删除有序数组中的重复项&&LeetCode88.合并两个有序数组

Grid布局介绍

MySQL 5.7 detailed download, installation and configuration tutorial

数学建模笔记:TOPSIS方法(优劣解距离法)和熵权法修正

The original question on the two sides of the automatic test of the byte beating (arranged according to the recording) is real and effective 26

apisix-入门使用篇

MYSQL unique constraint

navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
随机推荐
JDBC revisited
MySQL 游标
MySQL implements sorting according to custom (specified order)
复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式
JUC(一)- JUC学习概览 - 对JUC有一个整体的认识
goroutine (coroutine) in go language
navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
数学建模学习笔记:层次分析法(AHP)
MySQL 5.7 upgrade to 8.0 detailed process
自动化运维工具——ansible、概述、安装、模块介绍
21 Day Learning Challenge Schedule
navicat新建数据库
golang的time包:时间间隔格式化和秒、毫秒、纳秒等时间戳格式输出的方法
"Digital reconstruction of the system, getting the CEO is the first step"
C language: Check for omissions and fill in vacancies (3)
Android studio连接MySQL并完成简单的登录注册功能
MySql copies data from one table to another table
classSR论文阅读笔记
MySQL 的 limit 分页查询及性能问题
MySQL 字符串拼接 - 多种字符串拼接实战案例