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? :)
Printable View
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? :)
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.
I wont use continue and goto :)
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.
You can eliminate any continue-using an if
You can use continue to write clever code!!
Using continue is not bad. Goto is bad! Continue doesn't screw with the general flow of the program like a goto does.
continue is good :)
I didn't saw any *real* good example for continue..
it's not a bad thing, right. but not so usefull too right? :)
>>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 */
...
...
}
Here is an example I use to show my students: from C How to Program
continue- used to alter flow of control
Result : 1 2 3 4 6 7 8 9 10Code:#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
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.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.
-- beely
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.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.
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:
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).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;
for(i = 0; i < sizeof(array); i++)
if(array[i] == target_value) {
contains_value = 1;
break;
}
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.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.
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!Quote:
Originally posted by Polymorphic OOP
..., switch statements are more "bad practice" than break. While continue statements can often times be avoided (though often .....
Why the heck do you keep saying THE flow chart. What the heck are you talking about?
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;
}
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.
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.
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 ?
so how's the "break" statement on the flowchart look like ??
:rolleyes: did you read a book called "C how to program" by deitel...Quote:
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.
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???
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.
Of course it took longer! Without the break he added an entire extra check on every single loop!
so ?? without "break" . it's also can use other way. for eg:
for (i = 0 ; i<= MAX && end== 0 ; i++)
{
...
...
if (...){
..
..
end = 1;
}
so....?
Well beely, I tried code that looked something like:
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.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;
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
hmm... may you are right. but i still thinking the break won use it for my whole life (if necessary) on my programQuote:
Originally posted by master5001
Well beely, I tried code that looked something like:
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.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;
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.Quote:
Originally posted by beely
according to your code. it's bad programming althoug it's working. why not try this ??Code:int i, contains_value = 0;
for(i = 0; i < sizeof(array); i++)
if(array[i] == target_value) {
contains_value = 1;
break;
}
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.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.
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.
To All:
Contrary to popular stupidity, there is nothing wrong with using goto or continue. both perform the same thing in assembler, a jump to an address.
Normally, you don't need to use them. However, you cannot ever rule their use out 100%-- if you try, I'm sure you can find an example of logic that would require the use of each one, with no other way to do it.
But it is incorrect to simply rule both statements out simply because you "feel" or some other fool told you that you shouldn't ever use them.
Every tool in your toolbox has a purpose. Learn to use them all well, at the appropriate time. Only an amateur beats on a nail with pliers when the hammer is right there in the toolbox...
Developing is all about being open-minded to conventional and non-conventional solutions. 'goto' and 'continue' are valid, C statements supported by all C/C++ compilers.
I agree, but can someone mention any good usage for goto? :)Quote:
Originally posted by Sayeh
Every tool in your toolbox has a purpose. Learn to use them all well, at the appropriate time. Only an amateur beats on a nail with pliers when the hammer is right there in the toolbox...
for continue, Salem gave a perfect example.
I found a text talking about the goto statement:
http://www.acm.org/classics/oct95/
But again, if someone can give a nice example of using Goto, I'll be happy.
Another think, if goto is good or not, I think that label's aren't so pretty to the program, it's make some confusion on the code, at least for me.
I just read it to understand why he think that goto isn't so usefull.
But still, I think that putting label in the code, isn't so pretty, in other words, this complicate a little bit my code, maybe because I'am new to C :)
[flame=on]
very old debate. Dijkstra, Hoare, & Wirth trot this old nonsense out every few years. And It's spouted by academics who tire of teaching comp sci 101 students the art (yes art!) of programming. Which reqires clear thinking and a thorough knowlege of the tools at one's disposal not silly slogans like: "Goto considered harmful!", which stop thinking about your art.
Think for yourselves & ask what is the clearest design of the algorithm that makes the best use of the resources at hand. I can't count the number of bad programs I've seen by profs (I work with academics) who "like to think in the abstract". Newbie cluelessness is usually a temporary condition, but only if you're willing to explore the forbidden zones of programming when the code starts to offend your artisan's sensibilities. I.e. learn enuf to know when to break the rules.
Make no bones about it, both the "continue" & "break" statements are nothing but pretty-fied "goto"s. In c++ there are also the pretty-fied exceptions. As noted above, control structs like "if", "case", &c reduce to incuding them but they're oddly more removed from the debate.
As for using a "goto" directly, well I've only been using c for a few months & only at the applications level. I haven't needed the "goto" statement yet and the "break" only a few times, but I use the "continue" often. However, at the systems level (Isn't c a systems language too?) I can't imagine what gnarled logic would ensue if I couldn't use a vector table now and then; these are even considered worse because they're :eek: :eek: indirect "goto"s.
You don't need to use them often but when you do you really really need them. Or else you'll have to drop down to assembly.
Saw these examples, these doesn't show at all why to use continue.
Can be replaced byQuote:
while (fgets(buf, sizeof(buf), fp) != NULL)
{
if (buf[0] == '#') continue;
/* now go process the line */
...
...
}
Another example:Code:while (fgets(buf, sizeof(buf), fp) != NULL)
{
if (buf[0] != '#')
{
/* now go process the line */
...
...
}
}
It can easily replaced byQuote:
#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;
}
Code:#include <stdio.h>
int main()
{
int x;
for (x = 1; x <= 10; x++)
{
if (x != 5)
printf("%d ", x);
}
printf("Used continue to skip printing the value 5\n");
return 0;
}
It is okay to learn all tools in your toolbox, but you also must learn why to use certain tools and why not to use certain tools. Some tools may seem very usefull, but a bit more thought can show that there exists a "better" solution for which you don't need the tool. Note that I put better between "", in my opinion it is always possible to avoid things like goto and continue, just by adapting your logic, it sometimes requires a bit more thinking about your algorithm, but that's not bad.Quote:
Every tool in your toolbox has a purpose. Learn to use them all well, at the appropriate time. Only an amateur beats on a nail with pliers when the hammer is right there in the toolbox...
Anyway, I've seen lots of embedded systems passing by the last few years since I'm working and never seen goto or continue being used. So I don't think it is necessary to use, even in such time-critical and low-resource things like embedded systems.
I don't see why not to use continue, instead opening another block.
Take a look at to following to code-fragments.
The first fragment is much better maintainable. It makes the logic more explicit and therefore also more readable which increases maintainability of your code.Code:while (....)
{
if (....)
{
....
}
else
{
....
}
}
while (....)
{
if (....)
{
....
continue;
}
....
}
Another thing about the readability. If, in the second fragment, before the continue-statement there is a a large amount of code being placed. Then you have a large block after the if-statement and then after the if-block there can also be a block of code. If you just look at the code, it seems that if some condition is true, then the first block will be executed before the second block, but it isn't, because of the continue.
When scanning to large amounts of code to check the structure of the code, you usually (also) use the identation, it gives a quick overview of the structure of code. When using continue in this situation, it would break up the structure and lead to an incorrect interpretation of the code its structure.
so? all conditionals in C could be replaced by only if and goto if one wanted to. the continue version of this code is clearer and makes more sense than your version, simple as that.Quote:
Originally posted by Shiro
Saw these examples, these doesn't show at all why to use continue.
Can be replaced by
Code:while (fgets(buf, sizeof(buf), fp) != NULL)
{
if (buf[0] != '#')
{
/* now go process the line */
...
...
}
}
one thing btw i think java got right was the labeled loops and labeled break&continue, they replace almost all legitamant goto usages.
>the continue version of this code is clearer and makes more
>sense than your version, simple as that.
Well, that's your opinion. Mine is different, simple as that.
It's a matter of opinion as to which is more readable. In the contrived examples given you don't see what can happen when you nest nest nest... ad nauseum. Over nesting also makes things very hard to read and thus difficult to maintain, defeating HLL too. I've seen this kinda junk where one makes a 4 page loop and no breaks and nests the "if"s with some going to the bottom and others closing in the middle of the loop. A common newbie structure, all so very structured & all so very unreadable.Quote:
When scanning to large amounts of code to check the structure of the code, you usually (also) use the identation, it gives a quick overview of the structure of code. When using continue in this situation, it would break up the structure and lead to an incorrect interpretation of the code its structure.
?!... I'm quite skeptical that they run at all well. maybe you've got some really really smart compilers.Quote:
Anyway, I've seen lots of embedded systems passing by the last few years since I'm working and never seen goto or continue being used. So I don't think it is necessary to use, even in such time-critical and low-resource things like embedded systems.
You can recite poetry in Morse code too but...
>It's a matter of opinion as to which is more readable.
True.
>Over nesting also makes things very hard to read and thus
>difficult to maintain, defeating HLL too. I've seen this kinda junk
>where one makes a 4 page loop and no breaks and nests
>the "if"s with some going to the bottom and others closing in
>the middle of the loop. A common newbie structure, all so very
>structured & all so very unreadable.
It is true that deep nesting makes things very hard to read. But this usually can be avoided by redesigning the algorithm.
>?!... I'm quite skeptical that they run at all well. maybe you've
>got some really really smart compilers.
They run quite well, I didn't say they all run optimal, but they run well, without the use of things like continue or goto. The most important thing is designing an algorithm which is fast. The compiler is the second thing to look at.
[edit]
I'm very curious if someone can show me an example of an algorithm using continue which is faster than not using continue. I've never seen such, but it doesn't mean that it doesn't exist. So if someone knows such example, I'd be thankfull to learn from it.
[/edit]
1) choose a good algorithmQuote:
Originally posted by Shiro
[The most important thing is designing an algorithm which is fast. The compiler is the second thing to look at. [/B]
2) make the code work while keeping it readable/maintainable
3) tune it by making it faster or smaller or whatever in the tight spots. One should consider this step all the time but apply it only sometimes and in limited areas. "Count your cycles where they count." But do count them.
We're not that far apart. :cool: I'll bet we could read & maintain each other's code too.
This is a ok place to use it here mainly because it's a pretty popular idiom. It's also probably in the code toCode:while (fgets(buf, sizeof(buf), fp) != NULL)
{
if (buf[0] == '#') continue;
/* now go process the line */
...
...
}
one of those functions such
as scanf. Most of the time you might have something like
Code:while (fgets(buf, sizeof(buf), fp) != NULL)
{
if (buf[0] == '#')
continue;
if (buf[0] == ';')
continue;
// 50 lines of code that you don't want to indent
}
I don't think so, but most of the boring stuft like parsing whereQuote:
It is true that deep nesting makes things very hard to read. But this usually can be avoided by redesigning the algorithm.
you might want to use continue can be done by a tool.
We aren't talking about self-modifying code here. Once compiled, your program is done. All checks are finished, unless it's a scriptible application, in which case, this whole point is moot anyway.Quote:
Originally posted by vVv
[code]It's not hard to redesign proof-of-concept snippets. What would you do if further condition(s) occur while processing (i.e you can't add an ``&& other_condition'' to the first check), that render the rest of the code useless and require you to stop? Would you really want to make all that nested if-checks (which decreases readability), redesign that or simply use a quick continue? If you'd choose the second option, how would you do it?
As such, there is no scenario I can envision where your example would be valid. Furthremore, why couldn't I just add another && sitution?
This isn't a holy war over the evils of continue. By all means, do whatever you want. Just make your code readable and clear so the next person that has to maintain your code knows what the hell is going on. That's the whole point.
In everything you code, make sure the next idiot that follows you knows what's going on. Code as if an idiot is going to have to read your code after you're done with it.
Quzah.
As I said before, in my tests continue beats out not using continue. For those who are interested here is the code I used during my last test.Quote:
Originally posted by Shiro
I'm very curious if someone can show me an example of an algorithm using continue which is faster than not using continue. I've never seen such, but it doesn't mean that it doesn't exist. So if someone knows such example, I'd be thankfull to learn from it.
For an accurate test, you really should use identical loops. Furthermore, you need to make sure in each case you end up testing for the same exact thing. Without it, the test is hardly accurate. IE: Just because you write code to use the word 'continue' doesn't mean you're using the same number of checks.
[edit]
To reply to the above post, no, it wasn't directly specificly at you. It was more of an "in general" observation of these types of threads. They in the just end up as threads where one person argues their point, another argues theirs, and neither one will ever convince the other because in some situations, there is no real "right way". That's mainly what that comment was about.
[/edit]
Quzah.
And that's my entire point.Quote:
Originally posted by vVv
Personally, I can't remember the last time I used nested ``continue'''s or even a single ``goto'' - yet I don't think they should be avoided by all costs.
As for the comment regarding checks, if you look at the provided code, you'll notice that the "for" loops are different on two or three of the checks. Additionally, they end up checking for different values.
If you're trying to compare one method versus another, your tests should be as near identical as possible. That's the point. (Look at the attached .c file, you'll see what I'm talking about.) It's unfair to compare tests if the tests themselves do not have the same standards or criteria.
Quzah.
Preach on Quzah! And that is the bottom line. My tests merely show that using continue is faster sometimes, but a program doesn't always call for it. Also, there are a number of other optimizations that can be made before using break or continue. My point was not to show that you need to use break to make a fast loop, rather, my point was to show an example of where a break statement is best.
goto is very dangerous in programming structure. this could make your programming more confusing.... just like looping there and looping here... and then going there and then going here... it's not recommended to use it anyway.Quote:
Originally posted by Magos
I agree, but can someone mention any good usage for goto? :)
i agree to quzah's comment. however, using the bad structure could make the whole program more confusing. i try to avoid using "GOTO, continue, break" if necessary. all of the certain things that i mention is bad structure in my mind. if let say that if ppl wan to use with this statement, never mind. it's ok for that ppl, but if they use it in certain time, the whole program look like untidy, unstructure. This is not included on the flowchart.Quote:
Originally posted by quzah
We aren't talking about self-modifying code here. Once compiled, your program is done. All checks are finished, unless it's a scriptible application, in which case, this whole point is moot anyway.
As such, there is no scenario I can envision where your example would be valid. Furthremore, why couldn't I just add another && sitution?
This isn't a holy war over the evils of continue. By all means, do whatever you want. Just make your code readable and clear so the next person that has to maintain your code knows what the hell is going on. That's the whole point.
In everything you code, make sure the next idiot that follows you knows what's going on. Code as if an idiot is going to have to read your code after you're done with it.
Quzah.
&& shouldn't hardly disturb on the exe time. i just add a variable to control it. am i right ?
another point is...
:: a good structure on program that made by the programmer usually follow on flowchart. most of the book, said that "break, goto, continue " is not recommended to place it on the program -- it's bad structure, you may find the the prove from : -
-- c how to program (deitel)
-- approch using c structure
-- how to design in c
i'm not argue that "break, goto, continue" is can't used it among others. everybody have freedom to choose which one is better. but i would prefer for my own design by adding another variable and control to loop, that's all. hope you dont mind on my comment.
Actually, "continue" is convenient to use, for example, in a for:next loop, when you want the loop to iterate again without executing some additional code within the loop.
As for 'goto', the primary reason to use goto is for peformance. In many cases you can eliminate a test entirely or have an algorithm be much tighter and more direct by judicious use of a goto.
There is nothing wrong with either one. To say you should never use either one is like telling someone they should never pick their nose because it looks uncouth. Well I'm sorry, but there _is_ an appropriate time to pick your nose. Period. End of Discussion.
[rant = on]
I just wish a portion of people on this board would spend a little more time actually getting concepts and understanding what is going on in the machine, as opposed to getting hung up on truly pointless hyperbole. I watch too many people get "lost" on syntactic, grammatic, and "purist" issues, and they never actually get to concentrate on what's important: implementing _logic_.
Because that's what development is really about-- implementing logic in software.
well... sayeh, depend on it. what you prefer on is depend on situation. :D :D
i did tell this URL to my friends, you know one of my friend (expert in c program) did say ?
" .... depend on situation what type of program, i would rather choose your way (that's mean he agree on my program with adding && and variable) than using "break' because that kind of program is bad structure...."
2 of c program lecture did say...
" .... break's bad structure, avoid using it .... "
ok there guys, that's all about end of thread , and thanks for getting this challenging posted. wishing this thread will closed anyway.
:: depend on what you prefer to use, may use own style :: :D :D :D
No point in arguing any more... Some people drank the cool-aid and some people didn't.:p
yeah, every ppl have diff mind and ideals , right :D :D no wonder there's will conflict with other co-worker on their project programming. LOL