FAQ: Someone know where to use "continue"

This is a discussion on FAQ: Someone know where to use "continue" within the FAQ Board forums, part of the General Programming Boards category; in a usefull way? I dont see any useful charge to the continue command. You guys got a useful code ...

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    FAQ: Someone know where to use "continue"

    in a usefull way? I dont see any useful charge to the continue command. You guys got a useful code with Continue?

    In other words, why to use continue?

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    In my opinion, it is usually better to avoid such statements like continue or goto. Avoiding these statements leads in general to more structured code and forces you to design a structured algorithm without statements which make the program jump through the code.

    So, no, I don't see a usefull way to use continue.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Ok, thanks vVv and shiro, for the help.

    I wont use continue and goto

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Is continue really that bad? I've never really used it, but I can't see how it can screw things up like goto can.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    You can eliminate any continue-using an if

    You can use continue to write clever code!!
    Mr. C: Author and Instructor

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Using continue is not bad. Goto is bad! Continue doesn't screw with the general flow of the program like a goto does.

  7. #7
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    continue is good
    hello, internet!

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    It's not bad...

    I didn't saw any *real* good example for continue..
    it's not a bad thing, right. but not so usefull too right?

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>it's not a bad thing, right.
    Right, it's not a bad thing.

    >>but not so usefull too right?
    Wrong, all valid statements are useful in their rightful places. Here's an example that reads input from a FILE* processing only the lines that don't start with a # (comment marker in some shells): (same layout as vVv's)
    Code:
    while (fgets(buf, sizeof(buf), fp) != NULL)
    {
        if (buf[0] == '#') continue;
        /* now go process the line */
        ...
        ...
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    Here is an example I use to show my students: from C How to Program

    continue- used to alter flow of control

    Code:
     #include <stdio.h>
    int main()
    {
        int x;
        for (x = 1; x <= 10; x++)
        {
             if (x == 5)
               continue;
         printf("%d ", x);
        }
        printf("Used continue to skip printing the value 5\n");
        return 0;
    }
    Result : 1 2 3 4 6 7 8 9 10
    Used continue to skip printing the value 5
    Mr. C: Author and Instructor

  11. #11
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    Originally posted by Magos
    Is continue really that bad? I've never really used it, but I can't see how it can screw things up like goto can.
    refer to the structure / flowchart, is there any "CONTINUE" in the program?? i dont think that continue should be good in program structure. just one statements .. goto, continue, break (except for switch usage) is not recommended in program. this is BAD program. try to avoid using the continue anyway.

    -- beely

  12. #12
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by beely
    .. goto, continue, break (except for switch usage) is not recommended in program. this is BAD program. try to avoid using the continue anyway.
    continue and break don't mess with the flow of the program in the way that goto does. goto is naughty, but continue and break are still your friends. If anything, switch statements are more "bad practice" than break. While continue statements can often times be avoided (though often times they shouldn't be because it often times makes "more logical sense" to use a continue depending on what you'd want to accomplish), break often times can't be avoided (unless you use return) because many times you need to break out of a loop after part of the body has been executed for that particular iteration.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Along with what Polymorphic OOP is saying, break is often time under used. For example lets look at a for loop that scans an array for a value:

    Code:
    int i, contains_value = 0;
    
    for(i = 0; i < sizeof(array); i++)
        if(array[i] == target_value) {
            contains_value = 1;
            break;
        }
    Without the break statement the whole array would continue to look for the target_value even after it found it. Continue can also add a level of optimization analogous to this. Even goto's have their place (that doesn't mean I embrace them).

  14. #14
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    Code:
    int i, contains_value = 0;
    
    for(i = 0; i < sizeof(array); i++)
        if(array[i] == target_value) {
            contains_value = 1;
            break;
        }
    according to your code. it's bad programming althoug it's working. why not try this ??

    Code:
    int i, contains_value = 0;
    int error = 0;
    
    for(i = 0; i < sizeof(array) && error == 0 ; i++)
        if(array[i] == target_value) {
            contains_value = 1;
            error = 1;
        }
    --i; // replace back to the value if you wish.
    so, decide whether which program is better. for me , i would choose my code. as i said, break OR continue in the for-statement is bad structure. have you seen break or continue in flowchart??? no .. is it??? then why should put for break & continue.

  15. #15
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    Originally posted by Polymorphic OOP
    ..., switch statements are more "bad practice" than break. While continue statements can often times be avoided (though often .....
    well.... just for comment. switch may use it at anytime. it's not bad practice, and the usage of switch is better than using nested if-statement. switch-statement could found in the flowchart, and it's consider a good structure too!

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  2. Help with FAQ
    By JoshG in forum Game Programming
    Replies: 19
    Last Post: 10-29-2002, 06:31 PM
  3. FAQ Check/Lock
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-15-2002, 11:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21