Indefinite for loop syntax question

J

Jenny

Hi,

I read somewhere that a for loop can have a form for ( ; ; ){...}. I
try to understand the code below.

1. Could you tell me why after j becomes 1, it jump to line a?
2. Could you tell me why after line a, it jump to line int j =0;?
3. How could one use for ( ; ; ){...} in a real app?

public class Ball {
public static void main(String[] args){
int k;
k=1;
for ( ; ; ){
int j =0;
j++;
if (j>1){
System.out.println("j is " + j);
break;} //line a
}
}}
 
M

Michael Borgwardt

Jenny said:
I read somewhere that a for loop can have a form for ( ; ; ){...}. I

Yup, that's an infinite loop, but very bad style.
while(true){} is clearer.
1. Could you tell me why after j becomes 1, it jump to line a?

j never becomes 1, it is reset to 0 at the beginning of each
loop iteration.
2. Could you tell me why after line a, it jump to line int j =0;?

It doesn't. If you change the program so that the condition of the
if statement is fulfilled, the break terminates the for loop and ends the program.
3. How could one use for ( ; ; ){...} in a real app?

Either by using break to exit the loop, or by using an exception to do the same.
Both are considered bad style. A loop should have a proper termination condition,
and that is best put in a while clause.
 
J

Jim Cochrane

Hi,

I read somewhere that a for loop can have a form for ( ; ; ){...}. I
try to understand the code below.

1. Could you tell me why after j becomes 1, it jump to line a?
2. Could you tell me why after line a, it jump to line int j =0;?
3. How could one use for ( ; ; ){...} in a real app?

public class Ball {
public static void main(String[] args){
int k;
k=1;
for ( ; ; ){
int j =0;
j++;
if (j>1){
System.out.println("j is " + j);
break;} //line a
}
}}

Your formatting is lousy and is confusing you. Here's a cleaner version
that does the same thing, except with an added print for clarity:

public class Ball {
public static void main(String[] args){
for ( ; ; ) {
int j =0;
j++;
if (j > 1) {
System.out.println("j is " + j);
break;
} //line a
System.out.println("j be " + j); //line b
}
}
}
1. Could you tell me why after j becomes 1, it jump to line a?

It doesn't - after finding "if (j > 1)" false (j is 1)
it steps to line b. There is no jump involved.
2. Could you tell me why after line a, it jump to line int j =0;?

After line b, the next iteration of the for loop begins, the first line of
which is "int j =0;". Your loop will never terminate, of course, because
the only exit point is the break statement, which will never be executed
because j will never become > 1.
3. How could one use for ( ; ; ){...} in a real app?

The same way one would use "while (true) { ... }".

By the way, the following code behaves exactly the same as the above code:

public class Ball2 {
public static void main(String[] args){
int j;
for ( ; ; ) {
j =0;
j++;
if (j>1) {
System.out.println("j is " + j);
break;
} //line a
System.out.println("j be " + j);
}
}
}
 
B

Boudewijn Dijkstra

Michael Borgwardt said:
Yup, that's an infinite loop, but very bad style.
while(true){} is clearer.


j never becomes 1, it is reset to 0 at the beginning of each
loop iteration.

Wrong, j becomes 1 just after the statement "j++;"
It doesn't. If you change the program so that the condition of the
if statement is fulfilled, the break terminates the for loop and ends the
program.

Wrong again, the answer is: because the loop hasn't finished yet.
3. How could one use for ( ; ; ){...} in a real app?

[...]

I know I would never use a for loop without proper loop parameters.

A few notes about the program:
- variable k is never used;
- each time the loop is repeated, j is set to 0.
 
S

Sudsy

Boudewijn Dijkstra wrote:
3. How could one use for ( ; ; ){...} in a real app?

[...]


I know I would never use a for loop without proper loop parameters.

I seem to recall that there was a C compiler which generated more
efficient code for
for(;;)
than
while(1)
Whether it's true or not should not matter to Java programmers.
 
N

Nigel Wade

Jenny said:
Hi,

I read somewhere that a for loop can have a form for ( ; ; ){...}. I
try to understand the code below.

1. Could you tell me why after j becomes 1, it jump to line a?

I presume that, by "jump to line a" you are running the code in a debugger
and single stepping through to watch the execution - otherwise it doesn't
make much sense.

The reason it "jumps to line a" is that you've appended the closing brace
for the if block at the end of "line a", so when the debugger skips the
block and places the cursor on the closing brace it actually shows it as
being "line a".
2. Could you tell me why after line a, it jump to line int j =0;?

If you mean "if it was on line a, why didn't it execute the break", that's
because it was on the terminating brace of the if, not the break. The loop
continues on the first line of the loop.
3. How could one use for ( ; ; ){...} in a real app?

I never would.
public class Ball {
public static void main(String[] args){
int k;
k=1;
for ( ; ; ){
int j =0;
j++;
if (j>1){
System.out.println("j is " + j);
break;} //line a
}
}}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top