Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Array with decrement operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array with decrement operator" Watch "Array with decrement operator" New topic
Author

Array with decrement operator

Devaraj Rajakumar
Greenhorn

Joined: Apr 16, 2007
Posts: 6
Can someone please help me with the following issue?

I tried to process some data in an array using �for� loop and came across following problem with the condition statement.

int [] intArray = {1,5,4,6};
for(int x = intArray.length-1; x = =0; x--){
System.out.println(intArray[x]);
}

Since the condition statement should be of Boolean type I used �x= =0 � and also I tried �x<=0� but both compiles OK, not running and no run time errors.

When I changed to �x>=0� it works perfectly. I�m bit confused why this is happening.

Regards,
Devaraj
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
The body of the for loop is executed as long as the conditional statement is true. So you should use x >= 0, then it will loop unitl x gets to zero, what is what you want.
Devaraj Rajakumar
Greenhorn

Joined: Apr 16, 2007
Posts: 6
Thanks bart zagers, I guess the condition statement should be always applied so that it does not refer to "0" initially.
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
Just to explain it properly

for statement
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37980
    
  22
BTW: Did you really write x = =0? You can't have a space between the two = in an equality operator.
Devaraj Rajakumar
Greenhorn

Joined: Apr 16, 2007
Posts: 6
well, I didnt have any space in between the two equal signs. (==)

thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37980
    
  22
The space might have been a misprint wherever you found your example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array with decrement operator
 
Similar Threads
Help with Arrays!
Doubt on Local Arrays
Proving 2-d array stores references
locking objects that only read fields
2D Arrays giving me hell, please help!!