why am I getting the following output: 4 8 12 16 20
int i, j = 1, k;
for (i = 0; i < 5; i++)
{
k = j++ + ++j;
Console.Write(k + " ");
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t think the other answers here are correct.
The variables are evaluated in order according to mathematical ordering operations. In this case, we are just adding, so they are evaluated left to right.
j++and then++jjlet’s call the value ofjat the start of the looppj++jevaluates to the pre-increment value (p) and thenjis incremented (p+1)++jjis incremented (p+2) and evaluates as the post-increment value (p+2)So, the two evaluated numbers are
p+p+2: