Why the heck do you keep saying THE flow chart. What the heck are you talking about?
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 ??
did you read a book called "C how to program" by deitel...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 programOriginally 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.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.