Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

please explain this simple construct

Thread view: 
jpbisguier@yahoo.ca - 31 Oct 2006 14:19 GMT
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?
Jeffrey Schwab - 31 Oct 2006 14:29 GMT
> my instructor likes to use this construct in class but never really
> explained why/how it works:
[quoted text clipped - 5 lines]
>
> 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) {
    //...
}
Thomas Weidenfeller - 31 Oct 2006 14:38 GMT
>> my instructor likes to use this construct in class but never really
>> explained why/how it works:
[quoted text clipped - 3 lines]
>>   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
Signature

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

Jeffrey Schwab - 31 Oct 2006 15:27 GMT
>>> my instructor likes to use this construct in class but never really
>>> explained why/how it works:
[quoted text clipped - 20 lines]
>     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.
Arne Vajhøj - 01 Nov 2006 01:58 GMT
>> I assume the OP didn't tell us the full truth, and the loop in fact
>> looks like
[quoted text clipped - 21 lines]
> 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
Jeffrey Schwab - 01 Nov 2006 14:55 GMT
>>> I assume the OP didn't tell us the full truth, and the loop in fact
>>> looks like
[quoted text clipped - 39 lines]
> 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().
Robert Klemme - 31 Oct 2006 15:10 GMT
>> my instructor likes to use this construct in class but never really
>> explained why/how it works:
[quoted text clipped - 15 lines]
>     //...
> }

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
Jeffrey Schwab - 31 Oct 2006 15:43 GMT
>>> my instructor likes to use this construct in class but never really
>>> explained why/how it works:
[quoted text clipped - 20 lines]
> 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:
[quoted text clipped - 14 lines]
> 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
Robert Klemme - 31 Oct 2006 17:07 GMT
>> and IMHO it is far superior in cases like this:
>>
[quoted text clipped - 15 lines]
>
> 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();
[quoted text clipped - 7 lines]
>         return found ? i : null;
>     }

Cheers

    robert
Jeffrey Schwab - 31 Oct 2006 17:31 GMT
>>> and IMHO it is far superior in cases like this:
>>>
[quoted text clipped - 18 lines]
> 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();
[quoted text clipped - 7 lines]
>>         return found ? i : null;
>>     }
jpbisguier@yahoo.ca - 31 Oct 2006 16:07 GMT
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!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.