• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

++ and --

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple question for java gurus:
I would like to request some detailed explainaition on ++ and -- operator.

I know in the above case it will display 0

but it confuses me when used in for loop

1. In the above statement at what point, i will be incremented ?
2. If I want to see the value immediately after the change, then at what point should I execute the println method ?

Now if i++ in for loop is changed to ++i the output still remains the same.
So exactly at what point I can see the changed value ?

Thank You!
 
Greenhorn
Posts: 12
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'i' will be incremented after 'i<=2' has been run once. the initial println will be displayed until the i<=2; condition is met.

Im a java newb and this is mu first answer to anyones question ... so you experts feel free to chime in here...

hope this helps
-later
chorizo
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jugal Hans:
A simple question for java gurus:
I would like to request some detailed explainaition on ++ and -- operator.

I know in the above case it will display 0

but it confuses me when used in for loop

1. In the above statement at what point, i will be incremented ?
2. If I want to see the value immediately after the change, then at what point should I execute the println method ?

Now if i++ in for loop is changed to ++i the output still remains the same.
So exactly at what point I can see the changed value ?

Thank You!


Hi
Well as i know pretty well, when ever you use ++ or -- in a for loop disregard the rules of post increment and pre-increment, it doesnt for FOR loops.
Well as for a detailed explanation:
e.g
class pre{
public static void main(String args[]){
int i =10;
System.out.println(i++);//gives 10 still cause u print then increment
System.out.println(i);//gives 11 cause present value now 11
System.out.println(--i);//gives 10 decrease then print
System.out.println(i--);//gives 10 still cause print then decrease but ..
System.out.println(i);//gives 9 cause present value is 9
System.out.println(i++);//gives 9 still but present value is 10 after print
}// end of main

}// end of pre

Hope this clears it a little! So get a compiler and try it out and hope you understand! If compiler says otherwise please reply to this posting.. However good luck!
Saheed.
SCJP 1.4
SCJD(preparing..)
Imagination is better than Knowledge..
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you execute a "for" loop, you do things in a particular order. Here's the general syntax of a "for" loop:



When you first reach the loop, the init is executed. After that, the condition is checked. If the condition is true, the body is executed. When the body has completed, the update is executed. The condition is again checked and, if true, we continue the cycle.

The key is that the update is executed at the end of each body execution.

You can read the JLS, if you're interested: §14.13 The for Statement
 
Jugal Hans
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for ur replies..
ok, so according to Corey McGlone, there is no way to find out the changed value immed after the compiler actually changes it, right ?

Then is it safe to assume that pre and post increment operator doesnt really matter in FOR loop ? This is what Saheed Adepoju the prevoius poster also thinks !!

Thanks
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to look at this way, each part of the for statement is an individual statement itself, so they have no effect of pre and post because the get executed one by one. E.g.

for (int i=0; i<=2; i++ ) ; try to read as (only for understanding purposes :

for {

int i=0;

i <= 2;


System.println.out( "value of i " + i ); (Sorry did not include this earlier)

i++;

}


Also if you want to see the effect of pre and post in the for loop try the following :-


and then change i++ to ++i and see the effect.

sorry made an error while typing..

[ December 02, 2004: Message edited by: seemapanth Joshi ]

[ December 02, 2004: Message edited by: seemapanth Joshi ]

[ December 02, 2004: Message edited by: seemapanth Joshi ]
[ December 02, 2004: Message edited by: seemapanth Joshi ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for the code you have written, using a pre- or post-increment operator will indeed make no difference.

but if you do something wacky like

for (int x = 0, y= 0; x < 7; x += ++y)

vs.

for (int x = 0, y= 0; x < 7; x += y++)

it WILL make a diference.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short of using a debugger (probably in Eclipse or BlueJ, rather than jdb) you could rewrite your code to get something nearly the same:
 
Jugal Hans
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all.....
Feels good
 
reply
    Bookmark Topic Watch Topic
  • New Topic