aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes query on for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "query on for loop" Watch "query on for loop" New topic
Author

query on for loop

preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
please look at the following code
public class Calc {
public static void main (String args []) {
int total = 0;
for (int i = 0, j = 10; total > 30; ++i, --j) {
System.out.println(" i = " + i + " : j = " + j);
total += (i + j);
}
System.out.println("Total " + total);
}
how come the output is

Total 0
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I think you meant total < 30


"JavaRanch, where the deer and the Certified play" - David O'Meara
preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
i am sorry ,yes the conditional expression is total < 30
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well if you actually RAN it the other way, the for loop would never get executed and the answer would be 0.
preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
thanks cindy,
it was so simple and i did not pay attention to that point it was really total > 30, that the for loop was never executed
but there is another piece of code creating problems

public class Q46 {
public static void main(String[]args) {
int x;
int y;

for (x = 0, y = 5 ; x < y ; x=x++)
{
System.out.println( x + " " + y );
}
System.out.println( x + " " + y );
}
}
why this one turned to be an infinite one???
mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91
hi. in the first example, it seems to me the it does not matter if we did a post or a pre ++,-- on i and j.
but it shd shdnt it?
if j=10 at start. then --j shd mean that j is decremented by 1 (leaving j=9) and the body is executed.
this gets to me all the time an my exam time is getting near!...
sai challa
Ranch Hand

Joined: Feb 06, 2001
Posts: 54
Originally posted by preeti dengri:
thanks cindy,
it was so simple and i did not pay attention to that point it was really total > 30, that the for loop was never executed
but there is another piece of code creating problems

public class Q46 {
public static void main(String[]args) {
int x;
int y;

for (x = 0, y = 5 ; x < y ; x=x++)
{
System.out.println( x + " " + y );
}
System.out.println( x + " " + y );
}
}
why this one turned to be an infinite one???

I think in the statement x=x++ x is always assigned 0 always as it's a postincrement operator and hence x is always less than y.Therefore the loop is infinite.
preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
hi,
acc to me , the x=0,y=5 part is executed only once at the beginning of the for loop then after testing the condition the increment part is performed which should change the value of x to 1 for the second looping???shouldn't it be that way .please correct me
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: query on for loop