please explain this simple construct  
Author Message
jpbisguier





PostPosted: 2006-10-31 21:20:00 Top

java-programmer, please explain this simple construct 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





PostPosted: 2006-10-31 21:30:00 Top

java-programmer >> please explain this simple construct email***@***.com 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) {
//...
}
 
Thomas Weidenfeller





PostPosted: 2006-10-31 21:38:00 Top

java-programmer >> please explain this simple construct Jeffrey Schwab wrote:
> email***@***.com 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
 
 
Robert Klemme





PostPosted: 2006-10-31 22:11:00 Top

java-programmer >> please explain this simple construct On 31.10.2006 14:29, Jeffrey Schwab wrote:
> email***@***.com 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
 
 
Jeffrey Schwab





PostPosted: 2006-10-31 22:28:00 Top

java-programmer >> please explain this simple construct Thomas Weidenfeller wrote:
> Jeffrey Schwab wrote:
>> email***@***.com 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





PostPosted: 2006-10-31 22:43:00 Top

java-programmer >> please explain this simple construct Robert Klemme wrote:
> On 31.10.2006 14:29, Jeffrey Schwab wrote:
>> email***@***.com 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
 
 
jpbisguier





PostPosted: 2006-10-31 23:08:00 Top

java-programmer >> please explain this simple construct 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!

 
 
Robert Klemme





PostPosted: 2006-11-1 0:07:00 Top

java-programmer >> please explain this simple construct 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
 
 
Jeffrey Schwab





PostPosted: 2006-11-1 0:31:00 Top

java-programmer >> please explain this simple construct 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;
>> }
 
 
Arne Vajh鴍





PostPosted: 2006-11-1 8:59:00 Top

java-programmer >> please explain this simple construct 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
 
 
Jeffrey Schwab





PostPosted: 2006-11-1 21:56:00 Top

java-programmer >> please explain this simple construct Arne Vajh鴍 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().