C Board  

Go Back   C Board > General Programming Boards > FAQ Board

 
 
LinkBack Thread Tools Display Modes
Old 12-08-2002, 01:50 PM   #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?
Vber is offline  
Old 12-08-2002, 01:59 PM   #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.
Shiro is offline  
Old 12-08-2002, 02:05 PM   #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
Vber is offline  
Old 12-08-2002, 03:09 PM   #4
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
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.
Magos is offline  
Old 12-08-2002, 04:01 PM   #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
Mister C is offline  
Old 12-08-2002, 04:06 PM   #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.
Polymorphic OOP is offline  
Old 12-08-2002, 04:31 PM   #7
moi
Registered User
 
moi's Avatar
 
Join Date: Jul 2002
Posts: 945
continue is good
__________________
hello, internet!
moi is offline  
Old 12-08-2002, 04:45 PM   #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?
Vber is offline  
Old 12-08-2002, 07:15 PM   #9
End Of Line
 
Hammer's Avatar
 
Join Date: Apr 2002
Posts: 6,240
>>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]
Hammer is offline  
Old 12-08-2002, 11:14 PM   #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
Mister C is offline  
Old 12-08-2002, 11:21 PM   #11
~- Y u n a -~
 
beely's Avatar
 
Join Date: Dec 2001
Posts: 291
Quote:
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
beely is offline  
Old 12-08-2002, 11:30 PM   #12
Programming Sex-God
 
Polymorphic OOP's Avatar
 
Join Date: Nov 2002
Posts: 1,078
Quote:
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.
Polymorphic OOP is offline  
Old 12-08-2002, 11:44 PM   #13
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
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).
master5001 is offline  
Old 12-09-2002, 03:07 AM   #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.
beely is offline  
Old 12-09-2002, 03:10 AM   #15
~- Y u n a -~
 
beely's Avatar
 
Join Date: Dec 2001
Posts: 291
Quote:
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!
beely is offline  
 

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wiki FAQ dwks General Discussions 192 04-29-2008 01:17 PM
Help with FAQ JoshG Game Programming 19 10-29-2002 07:31 PM
FAQ Check/Lock RoD A Brief History of Cprogramming.com 2 10-15-2002 11:21 AM


All times are GMT -6. The time now is 11:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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