What is the difference between adding 1 by prefix and adding 1 by suffix? prefix 1 and
The increment operator ++ is a unary operator. The increment operator can appear before the variable as the prefix or suffix after the variable. What is the difference between the two?
Prefix plus 1
First, add 1 as the prefix, and assign the value of the variable after adding 1 as the prefix to another temporary variable temp.
static void Main(string[] args)
{
int num1 = 0;
while (num1 < 3)
{
int temp;
Console. WriteLine ("the value of the variable num1 is:" + num1.ToString ());
temp = ++num1;
Console. WriteLine ("the value of the variable temp is:" + temp. ToString ());
Console.WriteLine();
}
Console.ReadKey();
}
It can be seen that for the variable num1 that uses the prefix method to add 1, the result after adding 1 is that num1 itself adds 1. however, if you assign the value of num1 variable after adding 1 as the prefix to another variable temp, temp obtains the value after num1 and 1.
Suffix plus 1
First, add 1 as the suffix, and assign the value of the variable after adding 1 as the suffix to another temporary variable temp.
static void Main(string[] args)
{
int num2 = 0;
while (num2 < 3)
{
int temp;
Console. WriteLine ("the value of the variable num2 is:" + num2.ToString ());
temp = num2++;
Console. WriteLine ("the value of the variable temp is:" + temp. ToString ());
Console.WriteLine();
}
Console.ReadKey();
}
It can be seen that for the variable num2 that uses the suffix to add 1, the result after adding 1 is that the value of num2 itself is increased by 1. However, if you assign the value of num2 variable after adding 1 to another variable temp, temp obtains the value before num2 and 1.
Summary: The result of adding 1 to a variable by prefix or suffix is the same. However, if the value after increment 1 is involved, the prefix increment 1 will assign the value after increment 1 to other variables, if the suffix is increased by 1, the value before the increment is assigned to other variables. Auto increment/decrease 1 is similar to auto increment 1.
What is the difference between the prefix and suffix of the C language incremental operator? Come in and help me
I ++
Is to read the value of I first and Add 1
++ 1
Is to add 1 first and then read the value of I
Put it in for has no effect, because no matter I ++ or ++ I, it will not take the I value at that time, but it will get its value after this step.
The following is a simple example:
Main ()
{
Int I = 1;
Int j = 1;
While (I ++ <4)
Printf ("I ++ say hello! \ N ");
While (++ j <4)
Printf ("++ j say hello! \ N ");
}
You will know the result after running it.
The while loop runs until the condition is met.
The answer to this formula is B = 2, c = 3.
Main ()
{
Int c = 2;
Int B = 5;
B = c ++;
Printf ("c = % d, B = % d \ n", c, B );
}
Verify with this program
First, 2 of c is assigned to B, and then c is auto-incrementing.
If it is ++ c, it is first c Auto-increment, and then assigned to B
What is the difference between the prefix and suffix of auto-incrementing operators in C language for loop update expressions?
Well, there is no difference here... The programs you are exposed to are post-processed .. In fact, people are used to writing like this. Of course, there are also a lot of online experts who like prefix writing ~~ No need to worry about this ~~
Hope to adopt