Indefinite for loop syntax question

Discussion in 'Java' started by Jenny, Sep 15, 2004.

  1. Jenny

    Jenny Guest

    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
    }
    }}
    Jenny, Sep 15, 2004
    #1
    1. Advertising

  2. Jenny wrote:
    > 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.
    Michael Borgwardt, Sep 15, 2004
    #2
    1. Advertising

  3. Jenny

    Jim Cochrane Guest

    In article <>, Jenny wrote:
    > 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);
    }
    }
    }


    --
    Jim Cochrane;
    [When responding by email, include the term non-spam in the subject line to
    get through my spam filter.]
    Jim Cochrane, Sep 15, 2004
    #3
  4. "Michael Borgwardt" <> schreef in bericht
    news:...
    > Jenny wrote:
    > > 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.


    Wrong, j becomes 1 just after the statement "j++;"

    > > 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.


    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.
    Boudewijn Dijkstra, Sep 15, 2004
    #4
  5. Jenny

    Sudsy Guest

    Boudewijn Dijkstra wrote:
    <snip>
    >>>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.
    Sudsy, Sep 15, 2004
    #5
  6. Jenny

    Nigel Wade Guest

    Jenny wrote:

    > 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
    > }
    > }}


    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
    Nigel Wade, Sep 16, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?T2xlZw==?=

    Session timeout indefinite

    =?Utf-8?B?T2xlZw==?=, Dec 9, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    742
    Patrice
    Dec 9, 2005
  2. Dave Thompson

    Re: Structures of indefinite size

    Dave Thompson, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    758
    Dave Thompson
    Jun 26, 2003
  3. Replies:
    3
    Views:
    277
    Jakob Bieling
    Mar 27, 2006
  4. Replies:
    6
    Views:
    326
    Diego Martins
    Mar 30, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    369
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page