当前位置:网站首页>The difference between the increment and decrement operators before and after variables i+, +i, I –, – I

The difference between the increment and decrement operators before and after variables i+, +i, I –, – I

2022-06-13 05:54:00 Python's path to becoming a God

Auto increment and auto decrement operator yes Composite operators A simpler way of writing

Variable a Add yourself 1 Can be expressed as :

a=a+1;

Composite operators notation

a+=1;

Self - increment operator notation

a++;
++a;

Correspondingly, there are Self - subtracting operator notation

a--;
--a

After the completion of self increase and self decrease , Meeting Replace old value with new value Save in current Variable in .

The result of self increasing and self decreasing must have a variable to receive , Therefore, self increase and self decrease can only be applied to Variable , Cannot be for numbers or expressions .

Pre auto increment ( Self reduction ) Self increasing after and ( Self reduction ) The difference between :
An example : Output i The value after self increment and are respectively i++ and ++i The assignment of j Value , Analyze the difference between the two kinds of autoincrement

#include <stdio.h>

int main() { 
    
	int i = 5, j;
	j = i++;
	printf("i=%d,j=%d\n", i, j);
	i = 5;
	j = ++i;
	printf("i=%d,j=%d\n", i, j);
	return 0;
}

 Insert picture description here
According to the observation results i All values change to 6, however j But they are assigned to 5 and 6, That is to say, the difference between pre auto increment and post auto increment is that the value is assigned after auto increment , The other one assigns values first and then increases automatically .
Mnemonics :
【 According to the assignment character ‘=’ After Variable i In the form of Judge i Whether the value before or after auto increment is assigned to j】
if 【++i】, Then put ++i As a whole , Put the self increasing i value (++i) Assign to j( Self increment and then assignment );
if 【i】, First put i Value is assigned to j, let i Self increasing ( First assign value and then auto increment ).
( Self subtraction as above )

原网站

版权声明
本文为[Python's path to becoming a God]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280506192090.html