Thread: Starting young.

  1. #31
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As I mentioned, just do
    Code:
    for(;;)
    {
    // Your code here
    }
    and break the loop when you don't want it to loop anymore with break. It will work fine, you'll see.

  2. #32
    Registered User
    Join Date
    Nov 2007
    Posts
    40
    So how do I get it to loop back to the beginning of the loop?

    By the way, thanks :P.

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So how do I get it to loop back to the beginning of the loop?
    It does so automatically when control reaches the end of the loop body, unless there is a break.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it's usually clear to use continue with IF statements in the loop rather than big, complicated loop conditions.

  6. #36
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Yes, it's usually clear to use continue with IF statements in the loop rather than big, complicated loop conditions.
    Yes, it's a judgement call. I tend to avoid continue if it's possible.

    --
    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.

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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 do
    Code:
    for(;;)
    and add IFs to break and continue. It's much easier to read that way methinks.

  8. #38
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  10. #40
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  11. #41
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    Why is using break and continue in loops bad and similair to using goto's?
    This parameter is reserved

  12. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Jumping all over the place, I believe. OR something such. Plus the compiler might not be able to apply optimizations.

  13. #43
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  14. #44
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting programs
    By Molokai in forum C Programming
    Replies: 1
    Last Post: 04-16-2009, 10:10 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  4. question about reading inputs starting with 0
    By jibbles in forum C Programming
    Replies: 8
    Last Post: 08-09-2004, 03:27 AM
  5. we of the cage
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-21-2002, 10:14 AM