Thread: FAQ: Someone know where to use "continue"

  1. #16
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Why the heck do you keep saying THE flow chart. What the heck are you talking about?

  2. #17
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    sorry for the error, the code should be 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;
        }

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I was going to make a joke about the flow chart thing but you replied too fast

    Anyways, the two samples of code are functionally identical and both fairly optimized. But many times I don't happen to have the variable error handy. I never add variable when I don't need to. Our code is identical, only mine uses one less variable.

  4. #19
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Why? because your version introduces an entire new variable that has to be compared every single time it loops. A break handles the situation fine and doesn't mess with the flow in the manner that a goto does. Your version requires more space on the stack and adds a comparison on every iteration. Using a break doesn't introduce anything else.

  5. #20
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    hmm, a bit question,
    is the program better refer what's shown on flowchart ?
    if it's true, then goto, continue, and break is not recommended replace at the certain program.

    master5001, any comment on flowchart question ?

  6. #21
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    so how's the "break" statement on the flowchart look like ??

  7. #22
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    Originally posted by master5001
    I was going to make a joke about the flow chart thing but you replied too fast

    Anyways, the two samples of code are functionally identical and both fairly optimized. But many times I don't happen to have the variable error handy. I never add variable when I don't need to. Our code is identical, only mine uses one less variable.
    did you read a book called "C how to program" by deitel...

    so, their comment said that goto, break, and continue, most of programmer didn't use them ..... bla bla bla ....

    as a programmer, should they follow on the flowchart???

  8. #23
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hmmm, now here is something that is very interesting. I wrote a quick program that tested the two loops it turns out that mine (the one with a break statement) was better optimized by the compiler since it took 9 seconds to go through an array 16777215 times whereas, yours took 27 seconds. That was with full optimizations turned on.

  9. #24
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Of course it took longer! Without the break he added an entire extra check on every single loop!

  10. #25
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    so ?? without "break" . it's also can use other way. for eg:

    for (i = 0 ; i<= MAX && end== 0 ; i++)
    {
    ...
    ...
    if (...){
    ..
    ..
    end = 1;
    }

    so....?

  11. #26
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well beely, I tried code that looked something like:

    Code:
    int i, contains_value = 0;
    int error = 0;
    size_t size;
    
    for(size = sizeof(array);size-- && !contains_value;)
        if(array[array_size] == target_value)
            contains_value = 1;
    And it inlined it (of course to keep things even I inlined mine as well) and the results are 15 secons for your method and 7 for mine. Sorry, but break, continue, and goto have their place in this universe. The bottom line is that your compiler understands that it needs to optimize a loop a certain way when a break or continue is present. It won't necessarily give a loop that doesn't contain a break as much care in terms of optimization. Also, as Polymorphic said, you have an additional operation that I don't.

  12. #27
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    hmm... i guess i'm better use on my way. everyone will have a lot of way to do the program. as i said, as a good programmer, he /she would follow on the flowchart. goto, continue & break is not recommended to use. but if feel that need to use it, it' okay, but not recommended. yeah, master5001, i got your point. but if you are using break statement, this is not a good sturture in program (for me). but i would another variable the control the loop-statement. anyway, you did find out alot of situation where they did talk about goto, continue & break on the previous threads. but what's the conclusion? they did say that it's bad structure.... bla bla bla. dependent on you which one you prefer.
    just one statement, goto, continue & break is not found in flowchart. and if yes, how to flowchart look like? if found out the flowchart look like, this might be unstructure / untidy structure. anyway, dependent on what type want to use ... or you may find out most of the refer book should tell about the comment that i said.

    -- beely

  13. #28
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    Originally posted by master5001
    Well beely, I tried code that looked something like:

    Code:
    int i, contains_value = 0;
    int error = 0;
    size_t size;
    
    for(size = sizeof(array);size-- && !contains_value;)
        if(array[array_size] == target_value)
            contains_value = 1;
    And it inlined it (of course to keep things even I inlined mine as well) and the results are 15 secons for your method and 7 for mine. Sorry, but break, continue, and goto have their place in this universe. The bottom line is that your compiler understands that it needs to optimize a loop a certain way when a break or continue is present. It won't necessarily give a loop that doesn't contain a break as much care in terms of optimization. Also, as Polymorphic said, you have an additional operation that I don't.
    hmm... may you are right. but i still thinking the break won use it for my whole life (if necessary) on my program

  14. #29
    fou
    Guest
    Originally posted by beely
    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.
    Your code requires an extra compare for each iteration of the loop as well as being less easy to read. If you are reading a couple of million characters (MB) or tens of millions this is a big waste just to avoid a simple break statement. I also use continue as using multiple ifs gets so complicated and so many tabs it's not worth it. Continue is also easier to read.

  15. #30
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hmmm. In my program I added a loop that uses a continue statement and one that uses a goto's. Here are the results:

    use condition: 10 seconds
    use break: 4 seconds
    use continue: 8 seconds
    use goto: 0 seconds

    It is only fitting that a goto statement would be broken during my test The goto thing would not work properly because I believe the compiler optimized it out.

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, 07: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