![]() |
| | #16 |
| Programming Sex-God Join Date: Nov 2002
Posts: 1,078
| |
| Polymorphic OOP is offline |
| | #17 |
| ~- Y u n a -~ 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;
}
__________________ [ S ] Digital Design : Superiority "design" Style |
| beely is offline |
| | #18 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| 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. |
| master5001 is offline |
| | #19 |
| Programming Sex-God 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. |
| Polymorphic OOP is offline |
| | #20 |
| ~- Y u n a -~ 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 ?
__________________ [ S ] Digital Design : Superiority "design" Style |
| beely is offline |
| | #21 |
| ~- Y u n a -~ Join Date: Dec 2001
Posts: 291
| so how's the "break" statement on the flowchart look like ??
__________________ [ S ] Digital Design : Superiority "design" Style |
| beely is offline |
| | #22 | |
| ~- Y u n a -~ Join Date: Dec 2001
Posts: 291
| Quote:
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???
__________________ [ S ] Digital Design : Superiority "design" Style | |
| beely is offline |
| | #23 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| 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. |
| master5001 is offline |
| | #24 |
| Programming Sex-God 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! |
| Polymorphic OOP is offline |
| | #25 |
| ~- Y u n a -~ 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....? |
| beely is offline |
| | #26 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| 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;
|
| master5001 is offline |
| | #27 |
| ~- Y u n a -~ 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
__________________ [ S ] Digital Design : Superiority "design" Style |
| beely is offline |
| | #28 | |
| ~- Y u n a -~ Join Date: Dec 2001
Posts: 291
| Quote:
__________________ [ S ] Digital Design : Superiority "design" Style | |
| beely is offline |
| | #29 | |
| Guest
Posts: n/a
| Quote:
| |
| | #30 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| 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. |
| master5001 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 |