0
A little help here guys am confused with outputs...
int a =4; int b =6; b=a++; Console.WriteLine(b++); // note the (b++) // Output 4 but how? int a =4; int b =6; b=a++; Console.WriteLine(++b); // note the (++b) //Output 5 (for this i understand but the earlier am confused can i get some explanation😥
4 ответов
+ 7
An easy way to remember how these operators behave involves these two rules:
• If the operator appears 'before' the operand, the operation will occur 'before' the expression has been evaluated.
• If the operator appears 'after' the operand, the operation will occur 'after' the expression has already been evaluated.
+ 6
Steven Ambroce
Here is another example,
Postfix increment a++ will add 1 to a:
int a = 12;
a++;
Console.WriteLine(a); // 13
Postfix decrement a-- will subtract one:
a = 12;
a--;
Console.WriteLine(a); // 11
++a is called prefix increment it increments the value of 'a' and then
returns 'a' while a++ returns the value of 'a' and then increments:
int b = 12;
Console.WriteLine(++b); // 13
while
b = 12;
Console.WriteLine(b++); // 12
**Remember to use 🔍SEARCH... bar future you can find many similar threads; and
Read the COMMENTS in the lessons!
+ 2
Steven Ambroce post increment is first used the store value then update the value in calling same expression again with same variables so in first code script
b =a++ means store value of a++
The value of a=4 so post increment will use first stored value which is 4 so b = 4 is store at end.
Then in print statement you are doing post increment which use stored value first then increment so b store value is 4 so output is 4 for first code script.
In second code pre increment used which first increment then use so value is increases and 5 is came as output
0
ij