Yes you have to remember that we have pre-increment and post-increment so what you are doing is pre-increment the increment by 1 is taking effect by the help of the m+1 not he ++m cuz that is not working. okay it is pre-increment if i am clear. google and learn more.
Prince Annan Koomson annanworld.wordpress.com --- On Sat, 12/12/09, py2akv <[email protected]> wrote: From: py2akv <[email protected]> Subject: [c-prog] Re: Where am i Wrong? To: [email protected] Date: Saturday, December 12, 2009, 8:16 PM Asad, "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 :(" m is incremented by 1 for sure and it's easy to see why. When (i==3 ? ++m+1 : m--) is evaluated and i equals 3 the result is: 1. m incremented by 1 first (because the prefix increment operator has higher precedence over the addition operator; 2. then 1 is added to m's new value and cout'ed, but m doesn't change at all. One thing is ++m+1, another m=++m+1 (yes, in this case m's final value is greater than m's initial value by 2: m's initial value is first incremented by 1, then 1 is added to the result and finally the result is stored back to m. Should you really add 2 to m when i equals 3, make (i==3 ? m=++m+1 : m--). No problem placing an assignment inside the ternary conditional if you don't know: it has/returns the value of the assignment. To boot, and since the = operator associates from right to left, it's possible an assigment like this: i=j=k=0. Best, Geraldo --- In c-p...@yahoogroups. com, Asad Abbas <cyberstudent99@ ...> 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 :( > > --- > > Where am i Wrong? > > Thanx in advance! > > Regards! > Asad Abbas > Pakistan > --- > > For latest technology news > visit > http://latest- technologies. tk/ > To keep urself updated with changing world! > > > > > [Non-text portions of this message have been removed] > [Non-text portions of this message have been removed]
