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😥

14th Aug 2019, 7:24 PM
DataStream Cowboy
DataStream Cowboy - avatar
4 odpowiedzi
+ 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.
14th Aug 2019, 8:14 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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!
14th Aug 2019, 9:06 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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
14th Aug 2019, 7:33 PM
MsJ
MsJ - avatar
0
ij
17th Aug 2019, 9:03 AM
Chi boy Emma
Chi boy Emma - avatar