Asad Abbas <cyberstuden...@...> wrote:
>
> Consider the following code
>   
>   main()
>      { 
>      for(int i=1,m=9;++i< 4;)
>      for(int j=1;j<4;j++)
>      for (int k=1;k++<4;)
>      cout<<(i==3? ++m+1:m-- );
>      getch();
>      }
> 
> Its output: (on Dev C++)
> 
> 9876543212345678910
> 
> ---
> 
> I could not understand when 'i' is not equal to 3 'm--' is
> executed so value of m is decremented by '1' (thats right)
> But when i==3 and ++m+1 is executed then why the value of m
> is incremented by 1 only? 
> (++m+1) This should increment the value of m by 2 :(

Why do you think that?

(++m+1) is just (++m) + 1

> Where am i Wrong?

I'm suprised the program even compiled, but let's rewrite it
to something that conforms to C++, and that shows you each
step...

  #include <iostream>
  
  using namespace std;
  
  int main()
  {
    for(int i = 1, m = 9; ++i < 4; )
      for(int j = 1; j < 4; j++)
        for (int k = 1; k++ < 4; )
        {
          cout << i << ' ' << j << ' ' << k;
          cout << ": m before = " << m;
          cout << " - " << (i == 3 ? ++m + 1 : m--);
          cout << " - m after = " << m << '\n';
        }
  }

-- 
Peter

Reply via email to