please explain this simple construct

Discussion in 'Java' started by jpbisguier@yahoo.ca, Oct 31, 2006.

  1. Guest

    my instructor likes to use this construct in class but never really
    explained why/how it works:

    while (true)
    {
    if (something) return; // break from while
    }

    can someone shed some light how this works?
    , Oct 31, 2006
    #1
    1. Advertising

  2. wrote:
    > my instructor likes to use this construct in class but never really
    > explained why/how it works:
    >
    > while (true)
    > {
    > if (something) return; // break from while
    > }
    >
    > can someone shed some light how this works?


    That is hideous. The loop will execute the block over and over again,
    until "something" happens to be true at the same time the if statement
    is executed. At that point, the whole function call will suddenly end.
    That's different from an traditional "break," which just exits the
    current loop. The (IMHO) preferable way to loop looks more like this:

    while(!something) {
    //...
    }
    Jeffrey Schwab, Oct 31, 2006
    #2
    1. Advertising

  3. Jeffrey Schwab wrote:
    > wrote:
    >> my instructor likes to use this construct in class but never really
    >> explained why/how it works:
    >>
    >> while (true)
    >> {
    >> if (something) return; // break from while
    >> }

    [...]
    > (IMHO) preferable way to loop looks more like this:
    >
    > while(!something) {
    > //...
    > }


    I assume the OP didn't tell us the full truth, and the loop in fact
    looks like

    initialize_some_stuff();
    while(true) {
    do_some_calculations();
    check_some_things();
    if(something) break; // or return
    do_some_preparation_for_next_iteration();
    }

    And this is not some special construct. This is just a while-loop and a
    conditional break (or conditional return).

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    Thomas Weidenfeller, Oct 31, 2006
    #3
  4. On 31.10.2006 14:29, Jeffrey Schwab wrote:
    > wrote:
    >> my instructor likes to use this construct in class but never really
    >> explained why/how it works:
    >>
    >> while (true)
    >> {
    >> if (something) return; // break from while
    >> }
    >>
    >> can someone shed some light how this works?

    >
    > That is hideous. The loop will execute the block over and over again,
    > until "something" happens to be true at the same time the if statement
    > is executed. At that point, the whole function call will suddenly end.
    > That's different from an traditional "break," which just exits the
    > current loop. The (IMHO) preferable way to loop looks more like this:
    >
    > while(!something) {
    > //...
    > }


    I don't find a "return" hideous - certainly not more than a "break". In
    fact, I usually prefer "return" inside a loop over a "break". The
    "return" gives pretty easy short circuit exit and IMHO it is far
    superior in cases like this:

    public Foo findIt( String name ) {
    for ( Iterator iter = myFoos.itererator(); iter.hashNext(); ) {
    Foo f = (Foo) iter.next();

    if ( name.equals( f.getName() ) ) {
    return f;
    }
    }

    // alternatively throw an exception
    return null;
    }

    Using the loop condition to break the loop makes this piece of code much
    more complex and probably also less efficient.

    Kind regards

    robert
    Robert Klemme, Oct 31, 2006
    #4
  5. Thomas Weidenfeller wrote:
    > Jeffrey Schwab wrote:
    >> wrote:
    >>> my instructor likes to use this construct in class but never really
    >>> explained why/how it works:
    >>>
    >>> while (true)
    >>> {
    >>> if (something) return; // break from while
    >>> }

    > [...]
    >> (IMHO) preferable way to loop looks more like this:
    >>
    >> while(!something) {
    >> //...
    >> }

    >
    > I assume the OP didn't tell us the full truth, and the loop in fact
    > looks like
    >
    > initialize_some_stuff();
    > while(true) {
    > do_some_calculations();
    > check_some_things();
    > if(something) break; // or return
    > do_some_preparation_for_next_iteration();
    > }


    Agreed. I would prefer:

    initialize_some_stuff();
    do_some_calculations();
    check_some_things();

    while(!something) {
    do_some_preparation_for_next_iteration();
    do_some_calculations();
    check_some_things();
    }

    If there's too much redundancy, the calculating & checking can be moved
    into a separate routine.
    Jeffrey Schwab, Oct 31, 2006
    #5
  6. Robert Klemme wrote:
    > On 31.10.2006 14:29, Jeffrey Schwab wrote:
    >> wrote:
    >>> my instructor likes to use this construct in class but never really
    >>> explained why/how it works:
    >>>
    >>> while (true)
    >>> {
    >>> if (something) return; // break from while
    >>> }
    >>>
    >>> can someone shed some light how this works?

    >>
    >> That is hideous. The loop will execute the block over and over again,
    >> until "something" happens to be true at the same time the if statement
    >> is executed. At that point, the whole function call will suddenly
    >> end. That's different from an traditional "break," which just exits
    >> the current loop. The (IMHO) preferable way to loop looks more like
    >> this:
    >>
    >> while(!something) {
    >> //...
    >> }

    >
    > I don't find a "return" hideous - certainly not more than a "break". In
    > fact, I usually prefer "return" inside a loop over a "break". The
    > "return" gives pretty easy short circuit exit


    Yep, agree completely. I don't like a return statement being comment
    "break from while," though, especially in a course for people who don't
    yet know the language.

    > and IMHO it is far
    > superior in cases like this:
    >
    > public Foo findIt( String name ) {
    > for ( Iterator iter = myFoos.itererator(); iter.hashNext(); ) {
    > Foo f = (Foo) iter.next();
    >
    > if ( name.equals( f.getName() ) ) {
    > return f;
    > }
    > }
    >
    > // alternatively throw an exception
    > return null;
    > }
    >
    > Using the loop condition to break the loop makes this piece of code much
    > more complex and probably also less efficient.


    Maybe, but I still find it clearer, and easier to debug.

    package cljp;

    import java.io.PrintWriter;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Iterator;

    public class Main {

    private static PrintWriter out = new PrintWriter(System.out, true);

    List<Integer> myInts = Arrays.asList(
    new Integer[] { 1, 2, 3, 4, 5 });

    public Integer findIt(Integer n) {
    Iterator<Integer> iter = myInts.iterator();
    Integer i = null;
    boolean found = false;
    while(iter.hasNext() && !found) {
    i = iter.next();
    found = (n == i);
    }

    return found ? i : null;
    }

    public static void main(String[] args) {

    }

    }


    >
    > Kind regards
    >
    > robert
    Jeffrey Schwab, Oct 31, 2006
    #6
  7. Guest

    OK thanks for your replies fellas the confusion was about the while
    (true), which quoting from

    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/while.html :

    You can implement an infinite loop using the while statement as
    follows:

    while (true){
    // your code goes here
    }

    and also the return keyword, which i usually see in the context of:
    return whatever; and not used for breaking a loop, i find the break
    keyword more intuitive at the beginner level but i guess thats how you
    learn, by seeing new things!
    , Oct 31, 2006
    #7
  8. On 31.10.2006 15:43, Jeffrey Schwab wrote:
    >> and IMHO it is far superior in cases like this:
    >>
    >> public Foo findIt( String name ) {
    >> for ( Iterator iter = myFoos.itererator(); iter.hashNext(); ) {
    >> Foo f = (Foo) iter.next();
    >>
    >> if ( name.equals( f.getName() ) ) {
    >> return f;
    >> }
    >> }
    >>
    >> // alternatively throw an exception
    >> return null;
    >> }
    >>
    >> Using the loop condition to break the loop makes this piece of code
    >> much more complex and probably also less efficient.

    >
    > Maybe, but I still find it clearer, and easier to debug.


    Amazing. It would never occur to me that (below) is clearer or easier
    to debug than (above). But obviously people are very different.

    > public Integer findIt(Integer n) {
    > Iterator<Integer> iter = myInts.iterator();
    > Integer i = null;
    > boolean found = false;
    > while(iter.hasNext() && !found) {
    > i = iter.next();
    > found = (n == i);
    > }
    >
    > return found ? i : null;
    > }


    Cheers

    robert
    Robert Klemme, Oct 31, 2006
    #8
  9. Robert Klemme wrote:
    > On 31.10.2006 15:43, Jeffrey Schwab wrote:
    >>> and IMHO it is far superior in cases like this:
    >>>
    >>> public Foo findIt( String name ) {
    >>> for ( Iterator iter = myFoos.itererator(); iter.hashNext(); ) {
    >>> Foo f = (Foo) iter.next();
    >>>
    >>> if ( name.equals( f.getName() ) ) {
    >>> return f;
    >>> }
    >>> }
    >>>
    >>> // alternatively throw an exception
    >>> return null;
    >>> }
    >>>
    >>> Using the loop condition to break the loop makes this piece of code
    >>> much more complex and probably also less efficient.

    >>
    >> Maybe, but I still find it clearer, and easier to debug.

    >
    > Amazing. It would never occur to me that (below) is clearer or easier
    > to debug than (above). But obviously people are very different.


    You said it, brother. I am continually amazed at how smart people can
    have such different opinions about religion, politics and software
    design. :)

    >> public Integer findIt(Integer n) {
    >> Iterator<Integer> iter = myInts.iterator();
    >> Integer i = null;
    >> boolean found = false;
    >> while(iter.hasNext() && !found) {
    >> i = iter.next();
    >> found = (n == i);
    >> }
    >>
    >> return found ? i : null;
    >> }
    Jeffrey Schwab, Oct 31, 2006
    #9
  10. Jeffrey Schwab wrote:
    > Thomas Weidenfeller wrote:
    >> I assume the OP didn't tell us the full truth, and the loop in fact
    >> looks like
    >>
    >> initialize_some_stuff();
    >> while(true) {
    >> do_some_calculations();
    >> check_some_things();
    >> if(something) break; // or return
    >> do_some_preparation_for_next_iteration();
    >> }

    >
    > Agreed. I would prefer:
    >
    > initialize_some_stuff();
    > do_some_calculations();
    > check_some_things();
    >
    > while(!something) {
    > do_some_preparation_for_next_iteration();
    > do_some_calculations();
    > check_some_things();
    > }
    >
    > If there's too much redundancy, the calculating & checking can be moved
    > into a separate routine.


    I like the version without the duplicated code.

    Even though I usually prefer the alternative:

    initialize_some_stuff();
    for(;;) {
    do_some_calculations();
    check_some_things();
    if(something) break; // or return
    do_some_preparation_for_next_iteration();
    }

    The reason is that a break in the middle of a loop is
    actually a valid language construct.

    It is not in Pascal/C/C++/Java/C# but it is in Modula-2 and
    other non mainstream languages.

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Nov 1, 2006
    #10
  11. Arne Vajhøj wrote:
    > Jeffrey Schwab wrote:
    >> Thomas Weidenfeller wrote:
    >>> I assume the OP didn't tell us the full truth, and the loop in fact
    >>> looks like
    >>>
    >>> initialize_some_stuff();
    >>> while(true) {
    >>> do_some_calculations();
    >>> check_some_things();
    >>> if(something) break; // or return
    >>> do_some_preparation_for_next_iteration();
    >>> }

    >>
    >> Agreed. I would prefer:
    >>
    >> initialize_some_stuff();
    >> do_some_calculations();
    >> check_some_things();
    >>
    >> while(!something) {
    >> do_some_preparation_for_next_iteration();
    >> do_some_calculations();
    >> check_some_things();
    >> }
    >>
    >> If there's too much redundancy, the calculating & checking can be
    >> moved into a separate routine.

    >
    > I like the version without the duplicated code.
    >
    > Even though I usually prefer the alternative:
    >
    > initialize_some_stuff();
    > for(;;) {
    > do_some_calculations();
    > check_some_things();
    > if(something) break; // or return
    > do_some_preparation_for_next_iteration();
    > }
    >
    > The reason is that a break in the middle of a loop is
    > actually a valid language construct.
    >
    > It is not in Pascal/C/C++/Java/C# but it is in Modula-2 and
    > other non mainstream languages.


    Break in the middle of a loop is fine in C & C++, regardless of whether
    the loop uses while() or for().
    Jeffrey Schwab, Nov 1, 2006
    #11
    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. Replies:
    3
    Views:
    452
    Matimus
    Jun 17, 2008
  2. Mukesh
    Replies:
    4
    Views:
    608
    Paul N
    Mar 26, 2010
  3. Kaye Ng
    Replies:
    8
    Views:
    167
    Josh Cheek
    Jun 8, 2010
  4. BKDotCom
    Replies:
    3
    Views:
    71
    Jonas Raoni
    Dec 22, 2005
  5. Replies:
    1
    Views:
    107
    Thomas 'PointedEars' Lahn
    Dec 31, 2007
Loading...

Share This Page