Q: Consider following code snippet
int i=5; i=i++;
Please find original post from here
after these two lines what will be the value of i ?
Ans: 5
illustration: -
Please find original post from here
Most of us answer 6 but it's not the case, Value of i remains unchanged (i remains as 5). But somewhere along the execution, value of i was 6 but it is assigned with value 5 finally.
Consider the following program
class Program { static int i; static void Main(string[] args) { i = 5; i = postIncrement(); } static int postIncrement() { return i++; } }separate function for post Increment is written for better understanding of problem.Now While debugging the program in visual studio it will look like this
Debugging of the problem in VS 2012 |
Steps Involved:-
1. i initialised to 5.
2. i++ operator returns 5 but not assigned to i.
3. i++ operator increments value of i to 6.
4. i is assigned with 5 which is the value returned by i++ in step 2.
try to solve related questions like
after thes two lines what will be the value of i? Comment you answer please.
◙ Done.Happy Coding ☺ ◙
»Keep Sharing and Learning«
Answer the last questions answer here
ReplyDelete5+6+7=18
Delete:) Yes
Delete5+5+6 = 16
ReplyDeleteam i right?
ReplyDeleteSorry Wrong Answer
ReplyDelete18
ReplyDeleteright??
ReplyDeleteAnswer Format should be _ + _ + _ = _
Delete6+6+6=18
DeleteAnswer is 18 but not triple 6
Deleteif we go from right to left then?
Deleteit is 5 + 6 + 7 = 18
Delete:) Yes
DeleteAwesome post!!
ReplyDeletethank you
DeleteAwesome post and a great blog
ReplyDeletePython Training in Bangalore