Author |
Message |
Zip Newbie
Joined: Aug 31 2004 Posts: 9 Offline
|
|
Back to top |
|
 |
Zip Newbie
Joined: Aug 31 2004 Posts: 9 Offline
|
Posted: Fri Sep 03, 2004 8:16 pm Post subject: |
 |
|
|
|
mm thats not even whats causeing it to crash but just the same how do you terminate a loop before it reaches the end of one loop? |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
|
Back to top |
|
 |
2dragons Novice
Joined: Feb 17 2004 Posts: 95 Offline
|
Posted: Sat Sep 04, 2004 2:13 pm Post subject: |
 |
|
|
|
However using break is not a good programming practice.
Break is as bad as using goto or continue.
The expression you evaluate in your loop should break you out of it. |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Sat Sep 04, 2004 2:42 pm Post subject: |
 |
|
|
|
What?
I'll agree with goto, since goto should really be a "hidden" ASM thing, but break and continue are high-level enough to be useful. And they're not at all dangerous. The ASM code for a loop basically uses the same as break and continue normally in a loop... _________________ This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him. |
|
Back to top |
|
 |
2dragons Novice
Joined: Feb 17 2004 Posts: 95 Offline
|
Posted: Sat Sep 04, 2004 4:52 pm Post subject: |
 |
|
|
|
Heheh, continue and break are just covered versions of goto. All 3 have nearly have the same problem of making code hard to understand and maintain. |
|
Back to top |
|
 |
50% Packetloss Server Help Squatter

Age:40 Gender: Joined: Sep 09 2003 Posts: 561 Location: Santa Clarita, California Offline
|
Posted: Sat Sep 04, 2004 5:15 pm Post subject: |
 |
|
|
|
Umm, breaks are not high level. They are the equivelent of an unconditional branch (Aka BRA). Continues and breaks are perfect, ekted would not give you bad info. He has been programming all his life, i think he knows what he is talking about.
This is how a branch works. Depending on if its conditional or not, it will check flags that are set after arethmetic operations. If the flag its checking matches up to what it wants then it will execute the branch by adding a number to the program counter. So something such as BEQ (branch if equal to 0) if the Zero flag is set, the branch will occur and the 1 or 2 hex numbers after the opcode will be added to the program counter. Branches are used for loops and for jumping around within sections of code.
A goto is competely different, in a goto you are taking the program counter and completely changing the value. This is often risky and a branch is better. |
|
Back to top |
|
 |
2dragons Novice
Joined: Feb 17 2004 Posts: 95 Offline
|
Posted: Sat Sep 04, 2004 5:48 pm Post subject: |
 |
|
|
|
branch/continue are anagolous with goto, they both can jump to other sections of code, the only think with break/continue is they are bounded |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Sat Sep 04, 2004 6:21 pm Post subject: |
 |
|
|
|
I disagree with everyone. Break, goto, and continue are very good things to use, each in their proper place. The only bad one to use is return in the middle of a function. People will argue that it's just opinion, but my statement comes from 20 years of coding and dealing with others' code. The bottom line is, anything is ok if you know what you are doing. You can make "proper" code by making it look ugly, or you can do it right and make it elegent.
Look at the unexpected bug induced by the first code sample above. And see how my code does it right. |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Sat Sep 04, 2004 9:44 pm Post subject: |
 |
|
|
|
I didn't say they were high-level. I said they were high-level enough, which is basically exactly what you said, 50%. |
|
Back to top |
|
 |
D1st0rt Miss Directed Wannabe

Age:37 Gender: Joined: Aug 31 2003 Posts: 2247 Location: Blacksburg, VA Offline
|
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
|
Back to top |
|
 |
D1st0rt Miss Directed Wannabe

Age:37 Gender: Joined: Aug 31 2003 Posts: 2247 Location: Blacksburg, VA Offline
|
Posted: Sat Sep 04, 2004 10:17 pm Post subject: |
 |
|
|
|
To me, that doesn't look as clean. I tend to try and keep my bracket structures as simple as possible. Then again I really know very little about programming. Level 0.1 Knowledge on the Ekted Scale w00t! |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
|
Back to top |
|
 |
Smong Server Help Squatter

Joined: 1043048991 Posts: 0x91E Offline
|
Posted: Sun Sep 05, 2004 6:40 am Post subject: |
 |
|
|
|
Going back to the loop, is while(1) better than for(;;)? |
|
Back to top |
|
 |
50% Packetloss Server Help Squatter

Age:40 Gender: Joined: Sep 09 2003 Posts: 561 Location: Santa Clarita, California Offline
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Sun Sep 05, 2004 11:24 am Post subject: |
 |
|
|
|
Smong wrote: | Going back to the loop, is while(1) better than for(; ? |
They are identical in function and performance. I like to use for loops only when I have a specific start/end conditions. I also never initialize or modify anything but the loop counter in the for statement.
Last edited by Mr Ekted on Sun Sep 05, 2004 4:44 pm, edited 1 time in total |
|
Back to top |
|
 |
Mine GO BOOM Hunch Hunch What What

Age:41 Gender: Joined: Aug 01 2002 Posts: 3615 Location: Las Vegas Offline
|
Posted: Sun Sep 05, 2004 1:24 pm Post subject: |
 |
|
|
|
50% Packetloss wrote: | while sets up a conditional jump while the for loop will just loop forever in this case. So ASM wise, an infinate FOR loop takes less code |
Did you test this with an optomized code, because almost all smart compilers should make them identical in assembly. |
|
Back to top |
|
 |
50% Packetloss Server Help Squatter

Age:40 Gender: Joined: Sep 09 2003 Posts: 561 Location: Santa Clarita, California Offline
|
Posted: Sun Sep 05, 2004 2:57 pm Post subject: |
 |
|
|
|
I dont know, I used VC++ .net. It seems logical that it would do that though |
|
Back to top |
|
 |
Solo Ace Yeah, I'm in touch with reality...we correspond from time to time.

Age:37 Gender: Joined: Feb 06 2004 Posts: 2583 Location: The Netherlands Offline
|
Posted: Sun Sep 05, 2004 3:56 pm Post subject: |
 |
|
|
|
Optimization is disabled in VC++.net in debug mode.  |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Mon Sep 06, 2004 11:37 am Post subject: |
 |
|
|
|
int foo(int n)
{
if (n > 0)
return 1;
else if (n < 0)
return -1;
else
return 0;
} |
Pretty.
int foo(int n)
{
int ret;
if (n > 0)
ret = 1;
else if (n < 0)
ret = -1;
else
ret = 0;
return ret;
} |
Ugly. |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Mon Sep 06, 2004 12:38 pm Post subject: |
 |
|
|
|
You will remember this thread in 10 years when you get severely bitten by this coding style. |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Mon Sep 06, 2004 3:19 pm Post subject: |
 |
|
|
|
I don't use this style in cases that you have said are dangerous. I only use it in cases like the above, or in window procedures.
It especially makes sense to use multiple returns in window procs since, depending on the message, the function's code is completely different and isolated from the rest. |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Mon Sep 06, 2004 3:35 pm Post subject: |
 |
|
|
|
Cyan~Fire wrote: | I don't use this style in cases that you have said are dangerous. I only use it in cases like the above, or in window procedures.
It especially makes sense to use multiple returns in window procs since, depending on the message, the function's code is completely different and isolated from the rest. |
Disagreed. Always bad style. Window procs return a value, just like any other function. Besides, you shouldn't have lots of code in your window proc; you should be calling handler functions.  |
|
Back to top |
|
 |
-Smong- Guest
Offline
|
Posted: Mon Sep 06, 2004 6:51 pm Post subject: |
 |
|
|
|
I read somewhere the more handler functions you use the harder it is to use a debugger on the program. |
|
Back to top |
|
 |
|