当前位置:网站首页>5 operators, expressions, and statements
5 operators, expressions, and statements
2022-07-28 09:18:00 【You came back to your home】
3.3 Increment operator :++
Increment operator (increment operator) Perform simple tasks , Increment its operand 1. This operator appears in two ways . The first 1 Ways of planting ,++ Appear in front of the variable it acts on , This is the prefix pattern ; The first 2 Ways of planting ,++ Appears after the variable it acts on , This is the suffix pattern . The difference between the two modes is that the time of incremental behavior is different . Let's explain their similarities first , Analyze their differences again . Program listing 5.10 The program example in demonstrates how the increment operator works .
Program 5.10
/* add_one.c -- incrementing: prefix and postfix */
#include <stdio.h>
int main(void){
int ultra = 0, super = 0;
while (super < 5){
super++;
++ultra;
printf("super = %d, ultra = %d \n", super, ultra);
}
return 0;
}
After running the program , Output is as follows :
super = 1, ultra = 1
super = 2, ultra = 2
super = 3, ultra = 3
super = 4, ultra = 4
super = 5, ultra = 5
Take advantage of the incremental operator , We can write such a program :
shoe = 2.0;
while (++shoe < 18.5){
foot = SCALE*shoe + ADJUST;
printf("%10.1f %20.2f inches\n", shoe, foot);
}
As shown in the above code , Put the incremental process of variables into while In the condition of a cycle . This structure is in C Language is very common , Let's analyze it carefully .
First , In this way while How loops work ? It's simple .shoe Increasing the value of the 1, And then 18.5 The comparison . If the incremented value is less than 18.5, Then execute the statement in curly brackets - Time . then , shoe Increase the value of 1, Repeat the steps , until shoe The value of is not less than 18.5 until . Be careful , We put shoe The initial value of is from 3.0 Change it to 2.0, Because it's right foot The first 1 Before the second evaluation ,shoe It has increased 1 ( See the picture 5.4).
secondly , What are the benefits of doing so ? It makes the program more concise . what's more , It concentrates the two processes of the control cycle – A place to . The main process of this cycle is to judge whether to continue the cycle ( In this case , Check whether the size of shoes is smaller than 18.5), The secondary process is to change the elements to be tested ( In this case, it is increasing the size of shoes ).
If you forget to change the size of shoes ,shoe The value of will always be less than 18.5, The cycle doesn't stop . The computer will fall into an infinite loop (infinite loop) in , Generate countless identical rows . Last , You can only forcibly close this program . Put the cycle test and update cycle together , You won't forget the update cycle .
however , Merge the two operations into - In an expression , Reduced the readability of the code , Make the code difficult to understand . and , It is also prone to counting errors .
Another advantage of the increment operator is , Usually the machine language code it generates is more efficient , Because it is very similar to the actual machine language instructions . For all that , With the introduction of C Compilers are getting smarter , This advantage may disappear .- An intelligent compiler can put x =x+1 As ++x treat .
Last , The increment operator also has a feature that is particularly useful in some situations . We passed the program list 5.11 To illustrate .
Program 5.11
/* post_pre.c -- postfix vs prefix */
#include <stdio.h>
int main(void){
int a = 1, b = 1;
int a_post, pre_b;
a_post = a++; // value of a++ during assignment phase
pre_b = ++b; // value of ++b during assignment phase
printf("a a_post b pre_b \n");
printf("%1d %5d %5d %5d\n", a, a_post, b, pre_b);
return 0;
}
The output of the compiled program should be :
a a_post b pre_b
2 1 2 2
a and b It's all increasing 1, however ,a_ post yes a Increment the previous value , and b_ pre yes b Value after increment . This is it. ++ The difference between prefix form and suffix form of ( See the picture 5.5).


边栏推荐
- TXT文本文件存储
- KEGG通路的从属/注释信息如何获取
- 关闭页面时向后台发送消息
- F - Jealous Two-二维逆序对
- Detailed explanation of the basic use of express, body parse and express art template modules (use, route, path matching, response method, managed static files, official website)
- 【多线程】long和double的非原子性协定
- oracle 创建用户且只有查询权限
- JSON 文件存储
- DIY system home page, your personalized needs PRO system to meet!
- 面经-手撕代码
猜你喜欢

mysql主从架构 ,主库挂掉重启后,从库怎么自动连接主库

2022年安全员-B证考试模拟100题及答案

IntelliJ IDEA 关联数据库

How to obtain the subordinate / annotation information of KEGG channel

Go synergy

From development to testing: I started from scratch and worked for six years of automated testing

看得清比走得快更重要,因为走得对才能走得远

推荐一个摆脱变量名纠结的神器和批量修改文件名方法

Bluetooth technology | it is reported that apple, meta and other manufacturers will promote new wearable devices, and Bluetooth will help the development of intelligent wearable devices
![[English postgraduate entrance examination vocabulary training camp] day 15 - analyze, general, avoid, surveillance, compared](/img/a8/2c2fab613035f5e50524056d5f51a3.png)
[English postgraduate entrance examination vocabulary training camp] day 15 - analyze, general, avoid, surveillance, compared
随机推荐
v-bind指令的详细介绍
C language array pointer and pointer array discrimination, analysis of memory leakage
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
[advanced drawing of single cell] 07. Display of KEGG enrichment results
Promise learning notes
Kubernetes data persistence scheme
Go interface advanced
MDM数据质量应用说明
使用 GBase C API 执行存储过程是怎样的?
【JVM】JVM表示浮点数
478-82(56、128、718、129)
opencv4.60版本安装和配置
Send a message to the background when closing the page
51单片机存储篇:EEPROM(I2C)
中国地图省>市>级>区>镇>村5级联动下载【2019和2021】
mysql5.7.38容器里启动keepalived
Sword finger offer
Bluetooth technology | it is reported that apple, meta and other manufacturers will promote new wearable devices, and Bluetooth will help the development of intelligent wearable devices
公众号简介
leetcode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(中等)