As I mentioned, just doand break the loop when you don't want it to loop anymore with break. It will work fine, you'll see.Code:for(;;) { // Your code here }
As I mentioned, just doand break the loop when you don't want it to loop anymore with break. It will work fine, you'll see.Code:for(;;) { // Your code here }
So how do I get it to loop back to the beginning of the loop?
By the way, thanks :P.
It does so automatically when control reaches the end of the loop body, unless there is a break.So how do I get it to loop back to the beginning of the loop?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
And if you are in the middle of a loop and want to "start over at the top", you can use "continue;" - although it's often clearer and better if you use a big if/else statement to do those sort of things.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Yes, it's usually clear to use continue with IF statements in the loop rather than big, complicated loop conditions.
I tend to use break and continue as much as possible instead of loop conditions. The only loops I typically use are for. Very unusual I use anything else.
Usually, I just doand add IFs to break and continue. It's much easier to read that way methinks.Code:for(;;)
Well, the problem with both continue and break is that "you have to hunt around for them" when you try to follow the loop. If the loop is real trivial, that's no problem [and perhaps loops should be broken up more often than they are, when they aren't real trivial - but that's not always that easy either], but when the loop becomes a bit complex, and the break/continue statements are two or more levels away from the actual loop, it starts to become "interesting" to figure out what's actually going on. And, yes, I've seen a few examples of such loops in my working life - rarely of my construction.
There's nothing directly wrong with break, continue, etc. But if you use the right form of loop, then you give a clue to the reader what's about to happen. A do-while loop indicates "this is about to happen at least once", a while-loop may not happen at all, and a for-loop iterates over something [or is a for-ever loop]. There is a reason all three exist in the language. And using only one sort of restricts the amount of information you convey to your fellow programmers.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Perhaps. But I find it usually best to control when to break the loop myself. When it's relatively simple, I use a for, or maybe while. And when I need to check for a condition later and let the loop run once, I use do...while.
It's also a matter of style. Using break/continue is pretty much the same as creating a variable and setting to true/false.
Compilers are also better at finding valid optimizations for loops that are free of early exits. You'll also find that any company with any code review at all will most likely reject your code as being unidiomatic. You might as well use gotos, really.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Why is using break and continue in loops bad and similair to using goto's?
This parameter is reserved
Jumping all over the place, I believe. OR something such. Plus the compiler might not be able to apply optimizations.
The problem is that you suddenly have places in the code you can jump to, but it's not immediately obvious where you jump from.
With an if...else, it's simple. You jump from the end of the then-block to the end of the else-block, and from the if to the start of the else-block. No other jumps are possible, and these are at indentation borders.
With any loop, you jump from the bottom of the loop to the top. That's it.
With gotos, you jump to the goto labels, but where's the goto? You have to go look for it.
With break and continue, you jump to the end/start of a loop, but where from? Again, you have to look for it. The situation is better in that you only have to search the loop body, but it's still not an indentation border.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
In this case I think that a controlled infinite loop with an appropriate break is a correct choice. The alternatives would involve either setting a flag with a while loop, or using a do while loop with a function that prints the "correct!"/"incorrect!" message and returns a bool accordingly. For the former, the problem becomes one of looking for where the flag is set (as opposed to where the break is done).
Okay, there's probably yet another way: instead of setting a flag, test the condition twice. Once in the loop body to print the message, another in the loop condition to determine if further iterations are necessary. It makes no difference here since a simple comparison is not particularly expensive to test twice.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)