please explain this simple construct

J

jpbisguier

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?
 
J

Jeffrey Schwab

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) {
//...
}
 
T

Thomas Weidenfeller

Jeffrey said:
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
 
R

Robert Klemme

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
 
J

Jeffrey Schwab

Thomas said:
Jeffrey said:
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.
 
J

Jeffrey Schwab

Robert said:
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) {

}

}
 
J

jpbisguier

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!
 
R

Robert Klemme

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
 
J

Jeffrey Schwab

Robert said:
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. :)
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Jeffrey said:
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
 
J

Jeffrey Schwab

Arne said:
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().
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top