![]() |
| | #1 |
| Registered User Join Date: Nov 2002
Posts: 807
| FAQ: Someone know where to use "continue" In other words, why to use continue? |
| Vber is offline |
| | #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 |
| | #3 |
| Registered User Join Date: Nov 2002
Posts: 807
| Ok, thanks vVv and shiro, for the help. I wont use continue and goto |
| Vber is offline |
| | #4 |
| Confused 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 |
| | #5 |
| CS Author and Instructor Join Date: Sep 2002
Posts: 511
| 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 |
| | #6 |
| Programming Sex-God 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 |
| | #7 |
| Registered User Join Date: Jul 2002
Posts: 945
| continue is good
__________________ hello, internet! |
| moi is offline |
| | #8 |
| Registered User 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 |
| | #9 |
| End Of Line 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 |
| | #10 |
| CS Author and Instructor Join Date: Sep 2002
Posts: 511
| 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;
}
Used continue to skip printing the value 5
__________________ Mr. C: Author and Instructor |
| Mister C is offline |
| | #11 | |
| ~- Y u n a -~ Join Date: Dec 2001
Posts: 291
| Quote:
-- beely
__________________ [ S ] Digital Design : Superiority "design" Style | |
| beely is offline |
| | #12 | |
| Programming Sex-God Join Date: Nov 2002
Posts: 1,078
| Quote:
| |
| Polymorphic OOP is offline |
| | #13 |
| Banned 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;
}
|
| master5001 is offline |
| | #14 |
| ~- Y u n a -~ 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;
}
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.
__________________ [ S ] Digital Design : Superiority "design" Style |
| beely is offline |
| | #15 | |
| ~- Y u n a -~ Join Date: Dec 2001
Posts: 291
| Quote:
__________________ [ S ] Digital Design : Superiority "design" Style | |
| beely is offline |
| Thread Tools | |
| Display Modes | |
|
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 |