vendredi 9 août 2019

If statement and post-increment homework: I'm not getting the result I'm expecting - why?

Sorry if this is a dumb beginner question but I'm completely stumped.

int i = 0;

if (i == 0)
    i++;
    i++;
if (i == 3)
    i += 2;
    i += 2;

Console.WriteLine(i);

Ok, my logic, so if i = 0, add 1, then add 1 to that. So in the end i = 2.

Except it's not, it prints out 4.

The only way that could happen is if it went trough the second "if statement". Right?

What am I missing?

Aucun commentaire:

Enregistrer un commentaire