siabanie banie

siabanie banie

  • NA
  • 68
  • 74.2k

C++ while loop using i--

Jan 24 2012 8:37 PM
Hi all,

Can anyone assist me on this;

What are the values of i and j after this snippet code executed:

int i(10), j(0);
while (i- -)
j+=i;

I try to do some working steps as:

When i = 10, j = j = 0 + 10 which is 10;
When i = 9, j = j = 10 + 9 which is 19;
When i = 8, j = j = 19 + 8 which is 27;
When i = 7, j = j = 27 + 7 which is 34;
When i = 6, j = j = 34 + 6 which is 40;
When i = 5, j = j = 40 + 5 which is 45;
When i = 4, j = j = 45 + 4 which is 49;
When i = 3, j = j = 49 + 3 which is 52;
When i = 2, j = j = 52 + 2 which is 54;
When i = 1, j = j = 54 + 1 which is 55;
When i = 0, j = j = 55 + 0 which is 55 ;
When i = -1, the while loop exit.

So i = -1, j =55.

Can anyone help me what I did wrong please. Thanks

Answers (2)