Endless loops in poor condition?

So, I have C ++ code for feedback tracking nodes in the BFS algorithm. It looks something like this:

typedef std::map<int> MapType; bool IsValuePresent(const MapType& myMap, int beginVal, int searchVal) { int current_val = beginVal; while (true) { if (current_val == searchVal) return true; MapType::iterator it = myMap.find(current_val); assert(current_val != myMap.end()); if (current_val == it->second) // end of the line return false; current_val = it->second; } } 

However, while (true) seems ... suspicious to me. I know this code works, and logically I know that it should work. However, I cannot shake the feeling that there should be some condition in while , but in fact the only possible way is to use the bool variable to tell if this has been done. Should I stop worrying? Or is it really a bad form.

EDIT . Thanks to everyone for noticing that there is a way around this. However, I would still like to know if there are other valid cases.

+7
c ++ language-agnostic coding-style infinite-loop
source share
12 answers

I believe that there are times when this is normal for seemingly infinite loops. However, this does not seem to be one of them. It looks like you could just write the code like this

 while (current_val != searchVal ) { MapType::iterator it = myMap.find(current_val); assert(current_val != myMap.end()); if (current_val == it->second) // end of the line return false; current_val = it->second } return true; 

This seems to better reflect the true intent of the cycle.

+22
source share

My two cents: the code should be self-documenting . That is, when you get a piece of code, I would rather look and say the programmer’s intention, and then read the comments or drag and drop through the surrounding code. When I read:

 while(true) 

This tells me that a programmer needs an infinite loop; that the final condition cannot be indicated. In some cases, this implies programmers; for example, a server loop, and that is when it should be used.

In the above code, the loop does not have to last forever, it has a clear final condition and in order to be semantically understandable, as others have pointed out:

 while (currentVal != searchVal) 

works, so while (true) is clearly inferior, in which case it should be avoided.

+13
source share

There are times and places for endless cycles - I'm not sure if this is one of them. On the other hand, this is far from a blatant problem.

 while (currentVal != searchVal) { ... } return true; 

One place to use them is when the process is really vague - a daemon process with a monitor loop that will not be completed.

+6
source share

There are situations when such a design makes sense:

  • The interrupt condition is calculated in a loop.
  • There are more violation conditions, and they are all equally important.
  • You really need an infinite loop;) ..
+5
source share

I agree with other answers that in this case there is no need for an infinite loop.

However, another point may be that when you have an infinite loop, for(;;) may be the best way to express this. Some compilers generate warnings for while(true) (the condition is always evaluated as false), and your intentions are less clear, because it is like any other loop. Perhaps he said while (x == true) , and you accidentally deleted x instead of true . for(;;) says pretty clearly that this is supposed to be an infinite loop. Or maybe you intended to write something like while(t) , but Intellisense in your IDE clicked and decided to autocomplete to true .

for(;;) , on the other hand, is not something you ever accidentally entered. (and it's easier to look for. while (true) can also be written as while (1))

None of these versions are correct, but for(;;) can be more intuitive because there is no loop condition.

+5
source share

while(true) used in games for the main game cycle - games constantly read the player’s input, interact between objects and draw a screen, and then repeat. This cycle continues indefinitely until some other action leaves this cycle (exit the game, complete the level).

I tried to quickly find this main loop in the Quake 1 source code for you, but there were at least 50 occurrences of “ while(1) ”, and some of them were written as “ for(;;) ”, and I didn’t immediately determined which one was the main game cycle.

+4
source share

Despite the fact that I did this before, I voted for always trying to find a clearer solution using something readable that would usually include a valid expression in the while loop - otherwise you will scan the code to look for a break.

I am not very afraid of them or something else, but I know some people.

+1
source share

Well, a comment saying this is not an infinite loop will help:

  while (true) // Not really an infinite loop! Guaranteed to return. 

I agree that it should have a condition, but this is normal in some situations (and it is not always possible or easy to make a condition).

0
source share

Stop worrying. This is not a bad form if it helps simplify code logic and improve maintainability and readability. It’s worth at least writing down in a comment about the expected exit conditions and why the algorithm will not slip into an infinite loop.

0
source share

Ok, yes, but the two pages of code you have to write if you don't want your main loop to be something like while(true) are even worse.

0
source share

You can often find endless loops in embedded code systems - often surrounding end machines, checking peripheral chips and devices, etc.

0
source share

I love endless loops as an external control structure of a finite state machine. This is an efficiently structured transition:

 for (;;) { int c = ReadInput(); if (c == EOF) return kEOF; switch (state) { case inNumber: state = HandleNumber(c); break; case inToken: state = HandleToken(c); break; case inWhiteSpace: state = HandleWhiteSpace(c); default: state = inError; break; } if (state == inError) ThrowError(); 

}

0
source share

All Articles