10 Replies - 4153 Views - Last Post: 02 October 2009 - 02:40 PM

#1 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Using Infinite Loops and break Statements

Posted 24 September 2009 - 11:29 AM

While learning my first language (Java), it was drilled into me that coding deliberate infinite loops (such as for( ; ; ) or while( true )) and breaking out of them using break statements was poor programming form, but since I started formally studying computer science for the first time last fall, I have had numerous professors use these structures in programs demonstrated to the class. In the Java class I am currently a TA for, the professor has even gone so far as to say that this is the right way to handle a loop in which you don't know how many times you will be looping. (This was in reference to a for loop, which she created with for(k=0; ; k++).)

What is your opinion on using infinite loops and break statements in a program? Is it common usage in industry? Is it considered poor form?

Did I open a can of worms? I am good at that. :P

Is This A Good Question/Topic? 0
  • +

Replies To: Using Infinite Loops and break Statements

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,165
  • Joined: 14-September 07

Re: Using Infinite Loops and break Statements

Posted 24 September 2009 - 12:32 PM

Everything has a time and place. Having that said, I hate infinite loops with break statements that can be easily converted to a conditional. Using while(true) when it isn't necessary is just lazy. It logically says "this loop should NEVER exit", but it breaks out later? That's hypocritical.

Anyone who uses a for loop as an infinite loop needs to be immediately put out to pasture. For loops are designed for specific iteration. Put the condition in the loop.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,854
  • Joined: 16-October 07

Re: Using Infinite Loops and break Statements

Posted 24 September 2009 - 12:33 PM

Well, they beat the hell out of using a goto. ;)

If you think about it, standard looping structures offer a conditional check at the beginning or the end. The reason for an infinite / break design is because the conditional exit logic occurs inside the block. In some applications this is simply unavoidable, like a thread that runs until a given state. In some, it's a relatively clean solution.

Here's an example that you'll find on this site a lot:

cout << "Please enter a Month (1-12): ";
int monthNum;
cin >> monthNum;
if (monthNum < 1 || monthNum > 12) {
	cout << "Value out of range." << endl;
	cout << "Please enter a Month (1-12): ";
	cin >> monthNum;
}



This is usually followed by the words, "I'm stuck." You know you want to deal with a second invalid entry. You need a loop, but what to do? The trick is showing an error after the first screw up, but not before

One solution is:
int monthNum = 0;
while(monthNum < 1 || monthNum > 12) {
	cout << "Please enter a Month (1-12): ";
	cin >> monthNum;
	if (monthNum < 1 || monthNum > 12) {
		cout << "Value out of range." << endl;
	}
}



This has obvious issues of a double check. You can clean it up some, but it always looks a little kludgey.

The cleaner solution, IMHO, is the break:
int monthNum;
while(true) {
	cout << "Please enter a Month (1-12): ";
	cin >> monthNum;
	if (monthNum >=1 && monthNum <= 12) { break; }
	cout << "Value out of range." << endl;
}



Sometimes the use of break is banned, because people dislike it that much. In such cases, you can mimic a break by putting code in functions and using return for break.

Strange example:
bool getMonth(int &monthNum) {
	cout << "Please enter a Month (1-12): ";
	cin >> monthNum;
	return (monthNum < 1 || monthNum > 12);
}
//...
int monthNum;
while (!getMonth(monthNum)) { cout << "Value out of range." << endl; }


Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Using Infinite Loops and break Statements

Posted 24 September 2009 - 12:49 PM

One pattern I then to use, especially when the condition uses some index into an array.
Dim StillLooping As Boolean=True
Dim i As Integer=0
While StillLooping
' DoStuff
If  ThisConditionIsTrue Then
 StillLooping=False
Else
 i+=1
End If
End While


For example
Dim AnArray(9) As Integer
Dim i As Integer=0
While i<10 And AnArray(i)<100
 i+=1
End While



Cos both i<10 and AnArray(i)<100 evaluated, when i reached 10 it throws an error since the index is out of bound of the array.

So the above is transformed into
Dim StillLooping As Boolean=True
Dim i As Integer=0
While StillLooping
' DoStuff
 i+=1
If i<10
 If AnArray(i)>=100 Then
   StillLooping=False
 EndIf
Else 
 StillLooping=False
End If
End While


Was This Post Helpful? 0
  • +
  • -

#5 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Using Infinite Loops and break Statements

Posted 25 September 2009 - 07:04 AM

View PostKYA, on 24 Sep, 2009 - 01:32 PM, said:

Anyone who uses a for loop as an infinite loop needs to be immediately put out to pasture. For loops are designed for specific iteration. Put the condition in the loop.


Thanks for the laugh, KYA. :D

I happen to agree, but I couldn't very well stand up in class and tell the professor that when she recommended such a thing. It was all I could do to bite my tongue and let class continue.

View Postbaavgai, on 24 Sep, 2009 - 01:33 PM, said:

Well, they beat the hell out of using a goto. :)


Doesn't just about anything? :)

View Postbaavgai, on 24 Sep, 2009 - 01:33 PM, said:

Here's an example that you'll find on this site a lot:

cout << "Please enter a Month (1-12): ";
int monthNum;
cin >> monthNum;
if (monthNum < 1 || monthNum > 12) {
	cout << "Value out of range." << endl;
	cout << "Please enter a Month (1-12): ";
	cin >> monthNum;
}



This is usually followed by the words, "I'm stuck." You know you want to deal with a second invalid entry. You need a loop, but what to do? The trick is showing an error after the first screw up, but not before

One solution is:
int monthNum = 0;
while(monthNum < 1 || monthNum > 12) {
	cout << "Please enter a Month (1-12): ";
	cin >> monthNum;
	if (monthNum < 1 || monthNum > 12) {
		cout << "Value out of range." << endl;
	}
}



This has obvious issues of a double check. You can clean it up some, but it always looks a little kludgey.

The cleaner solution, IMHO, is the break:
int monthNum;
while(true) {
	cout << "Please enter a Month (1-12): ";
	cin >> monthNum;
	if (monthNum >=1 && monthNum <= 12) { break; }
	cout << "Value out of range." << endl;
}



Or you could use a do-while construction:

int monthNum;
do {
	cout << "Please enter a Month (1-12): ";
	cin >> monthNum;
	if (monthNum < 1 || monthNum > 12) {
		cout << "Value out of range." << endl;
	}
}while(monthNum < 1 || monthNum > 12) ;



Is that less clean, in your opinion, than using the break? If so, I don't see why, so please explain.

Just to be clear to all contributors: I know how to avoid using infinite loops and break statements. (I'm not sure I made that clear in the first place, so I'm doing so now.) I'm just asking what your opinion is on doing so--is it poor form or not? (Explanations of why don't hurt, either.) :)
Was This Post Helpful? 0
  • +
  • -

#6 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: Using Infinite Loops and break Statements

Posted 26 September 2009 - 04:28 AM

View Postsmacdav, on 25 Sep, 2009 - 06:04 AM, said:

<snip>
Or you could use a do-while construction:

int monthNum;
do {
	cout << "Please enter a Month (1-12): ";
	cin >> monthNum;
	if (monthNum < 1 || monthNum > 12) {
		cout << "Value out of range." << endl;
	}
}while(monthNum < 1 || monthNum > 12) ;



Is that less clean, in your opinion, than using the break? If so, I don't see why, so please explain.

Just to be clear to all contributors: I know how to avoid using infinite loops and break statements. (I'm not sure I made that clear in the first place, so I'm doing so now.) I'm just asking what your opinion is on doing so--is it poor form or not? (Explanations of why don't hurt, either.) :)


This is what I immediately thought of. Surely to use a do/while is better than breaking a for loop? Although I guess at the end of the day both will work - Its kind of like putting jam on your toast, then the butter. The taste will probably be the same, but most people will think you are odd...

B)

Steve
Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Using Infinite Loops and break Statements

Posted 26 September 2009 - 05:08 AM

That still tests to see if the month is valid twice. If you wanted to change the definition of a valid month (say to 0-11 instead of 1-12) you could inadvertently forget to change one of those checks. Breaking out in the middle only has the check once.

You could improve the do while version by assigning the validity to a variable. At the end of the day, it's a matter of style. Both work, neither are terrible. Sometimes the one you don't like might make things clearer.
Was This Post Helpful? 0
  • +
  • -

#8 brianarn  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 27
  • Joined: 02-October 09

Re: Using Infinite Loops and break Statements

Posted 02 October 2009 - 01:15 PM

That infinite for loop with k makes me cringe - what a waste. :D

Infinite loops are also useful in situations where you don't know *when* you'll get input. For example, network code listening on a port. You have to keep checking the port until you get something, and then do something with what you get. Without infinite loops, writing a network service would be rather difficult.
Was This Post Helpful? 0
  • +
  • -

#9 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Using Infinite Loops and break Statements

Posted 02 October 2009 - 01:35 PM

View Postbrianarn, on 2 Oct, 2009 - 02:15 PM, said:

That infinite for loop with k makes me cringe - what a waste. :D

Infinite loops are also useful in situations where you don't know *when* you'll get input. For example, network code listening on a port. You have to keep checking the port until you get something, and then do something with what you get. Without infinite loops, writing a network service would be rather difficult.


Interesting. I don't know anything about network code listening to ports. Is it not possible to do something like the following pseudocode?

msgrcvd = false
while (!message)
{
   if (message received)
      msgrcvd = true
}


Was This Post Helpful? 0
  • +
  • -

#10 brianarn  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 27
  • Joined: 02-October 09

Re: Using Infinite Loops and break Statements

Posted 02 October 2009 - 02:11 PM

That would work if you just want to handle one request and then exit, sure. However, things like web servers and proxies and MUDs and whatever all generally want to handle more than one request. :)

So then, it's generally going to be more something like this pseudocode:
while (true) {
	if (message) {
		process(message);
	}
} 


If you'd like to see a sample (with my Vim syntax highlighting in effect), I found an old MUD proxy in my CS account (not my code, but freely distributable). The MUD preferred people use their specific client, but for those who preferred to run other clients (like zMud or something), you would run the proxy locally, and connect to it. It would then translate the MUD's color codes into ANSI, strip out special lines that were meant just for their client, etc. It may not be the best network code, but it's a fun sample to browse through, and it also demonstrates forking and uses two infinite loops to accomplish the task of listening on ports.

I also have a Perl script I wrote to act instead of the proxy, to let me play the MUD in a terminal directly, which does some similar machinations to listen to ports. It's not quite right though, as it hangs on input in a way that doesn't seem right to me, but I never took the time to fully fix it because it's not like I run it all the time. :D
Was This Post Helpful? 0
  • +
  • -

#11 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Using Infinite Loops and break Statements

Posted 02 October 2009 - 02:40 PM

View Postbrianarn, on 2 Oct, 2009 - 03:11 PM, said:

That would work if you just want to handle one request and then exit, sure. However, things like web servers and proxies and MUDs and whatever all generally want to handle more than one request. :)

So then, it's generally going to be more something like this pseudocode:
while (true) {
	if (message) {
		process(message);
	}
} 


Good point. I can see that. On the other hand, that's a truly infinite loop. The kind I was talking about have a break statement in them because they aren't really intended to run forever.

View Postbrianarn, on 2 Oct, 2009 - 03:11 PM, said:

If you'd like to see a sample (with my Vim syntax highlighting in effect), I found an old MUD proxy in my CS account (not my code, but freely distributable).


That is very cool. I'm going to have to look through that code in detail. Thanks for sharing. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1