当前位置:网站首页>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).


边栏推荐
- Deconstruction assignment of ES6 variables
- 19c SYSAUX表空间SQLOBJ$PLAN表过大,如何清理
- AMQ streams (1) of openshift 4 - multiple consumers receive data from partition
- 从开发转测试:我从零开始,一干就是6年的自动化测试历程
- TXT文本文件存储
- DAPP safety summary and typical safety incident analysis
- 台大林轩田《机器学习基石》习题解答和代码实现 | 【你值得拥有】
- [advanced drawing of single cell] 07. Display of KEGG enrichment results
- Principle of line of sight tracking and explanation of the paper
- 【JVM】JVM表示浮点数
猜你喜欢
![Train your own classification [Bao Jiaobao, the data are ready]](/img/bd/08d0fbf0d41bb5ba7c418848ea1a4c.jpg)
Train your own classification [Bao Jiaobao, the data are ready]

2022年起重机司机(限桥式起重机)考试题库及模拟考试

Openshift 4 - use verticalpodautoscaler to optimize application resource request and limit

C simply call FMU for simulation calculation

【vscode】vscode使用

【打包部署】

KEGG通路的从属/注释信息如何获取

MDM data quality application description

js数组去重,id相同对某值相加合并

Marketing play is changeable, and understanding the rules is the key!
随机推荐
ES6 let and Const
[advanced drawing of single cell] 07. Display of KEGG enrichment results
From development to testing: I started from scratch and worked for six years of automated testing
Kubernetes data persistence scheme
Promise学习笔记
VS2015使用dumpbin 查看库的导出函数符号
Map of China province > City > level > District > Town > village 5-level linkage download [2019 and 2021]
力扣题(1)—— 两数之和
Hyperlink label
[English postgraduate entrance examination vocabulary training camp] day 15 - analyze, general, avoid, surveillance, compared
【vscode】vscode使用
[592. Fraction addition and subtraction]
1.5 merge\rebase\revert\stash\branch
478-82(56、128、718、129)
c语言数组指针和指针数组辨析,浅析内存泄漏
DAPP safety summary and typical safety incident analysis
LeetCode_406_根据身高重建队列
使用 GBase C API 执行存储过程是怎样的?
DIY system home page, your personalized needs PRO system to meet!
Send a message to the background when closing the page