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


边栏推荐
- Promise学习笔记
- Send a message to the background when closing the page
- Activiti启报错: Cannot create PoolableConnectionFactory (Could not create connection to database server
- The chess robot pinched the finger of a 7-year-old boy, and the pot of a software testing engineer? I smiled.
- Dapp安全总结与典型安全事件分析
- Magic Bracelet-【群论】【Burnside引理】【矩阵快速幂】
- AMQ streams (1) of openshift 4 - multiple consumers receive data from partition
- Oracle-11gR2默认的系统JOB
- An entry artifact tensorflowplayground
- Marketing play is changeable, and understanding the rules is the key!
猜你喜欢

golang 协程的实现原理

AMQ streams (1) of openshift 4 - multiple consumers receive data from partition

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

Sentinel

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

公众号简介

快速上手Flask(一) 认识框架Flask、项目结构、开发环境

The cooperation between starfish OS and metabell is just the beginning

51单片机存储篇:EEPROM(I2C)

CSV file storage
随机推荐
Bluetooth technology | the total scale of charging piles in Beijing will reach 700000 in 2025. Talk about the indissoluble relationship between Bluetooth and charging piles
ES6 let与const
5 运算符、表达式和语句
v-bind指令的详细介绍
App加速读取显示IPFS图片的解决方案和实现
VS2015使用dumpbin 查看库的导出函数符号
Implementation principle of golang synergy
Machine learning (11) -- time series analysis
Go panic and recover
Prometheus TSDB analysis
侯捷STL标准库和泛型编程
网络层的IP协议
Path and attribute labels of picture labels
CSV file storage
【多线程】println方法底层原理
Learn to draw with nature communications -- complex violin drawing
Sentinel
Vs2015 use dumpbin to view the exported function symbols of the library
10、学习MySQL LIKE 子句
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