Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
Terminating a Loop
Goto page 1, 2  Next
 
Post new topic   Reply to topic Printable version
 View previous topic  MervBot problem... Post :: Post Continuing (MERVBot CTF plugin)  View next topic  
Author Message
Zip
Newbie


Joined: Aug 31 2004
Posts: 9
Offline

PostPosted: Fri Sep 03, 2004 7:58 pm    Post subject: Terminating a Loop Reply to topic Reply with quote

Im wondering what the best way to terminate a loop is, stop it from continuing. Maybe its not possible in this situation but I dont think thats the case. Here is the Methot that I tried, which also crashed the bot. This isnt an exact copy paste but shows the structure.

bool die=false;
Code: Show/Hide
while(die == false)
{
an If statment { bla bla bla  die=true}
an If statment {another 'If'{blablabla die=true;} bla bla bla  die=true;}
an If statment {another 'If'{blablabla die=true;} bla bla bla  die=true;}
And so on
}


I wasnt sure if there is a predefined function for terminating a loop.
Or do I need to have something in the "case EVENT_Tick:" to check if die has been set to true??
true and false doesnt need to be capitalized right?
[/code]
Back to top
View users profile Send private message Add User to Ignore List
Zip
Newbie


Joined: Aug 31 2004
Posts: 9
Offline

PostPosted: Fri Sep 03, 2004 8:16 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Fri Sep 03, 2004 8:25 pm    Post subject: Reply to topic Reply with quote

Code: Show/Hide
while (1)
   {
   if ()
      break;

   if ()
      break;

   if ()
      break;
   }


break jumps out of the inner-most while/for/do structure.
_________________
4,691 irradiated haggis!
Back to top
View users profile Send private message Add User to Ignore List
2dragons
Novice


Joined: Feb 17 2004
Posts: 95
Offline

PostPosted: Sat Sep 04, 2004 2:13 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sat Sep 04, 2004 2:42 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website
2dragons
Novice


Joined: Feb 17 2004
Posts: 95
Offline

PostPosted: Sat Sep 04, 2004 4:52 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
50% Packetloss
Server Help Squatter


Age:40
Gender:Gender:Male
Joined: Sep 09 2003
Posts: 561
Location: Santa Clarita, California
Offline

PostPosted: Sat Sep 04, 2004 5:15 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email AIM Address
2dragons
Novice


Joined: Feb 17 2004
Posts: 95
Offline

PostPosted: Sat Sep 04, 2004 5:48 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sat Sep 04, 2004 6:21 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sat Sep 04, 2004 9:44 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website
D1st0rt
Miss Directed Wannabe


Age:37
Gender:Gender:Male
Joined: Aug 31 2003
Posts: 2247
Location: Blacksburg, VA
Offline

PostPosted: Sat Sep 04, 2004 10:07 pm    Post subject: Reply to topic Reply with quote

Ekted, so basically this would be bad?

Code: Show/Hide
void foo(int n)
{
    if(n < 0) return;

    //lots of code here
}

_________________

Back to top
View users profile Send private message Add User to Ignore List Visit posters website
CypherJF
I gargle nitroglycerin


Gender:Gender:Male
Joined: Aug 14 2003
Posts: 2582
Location: USA
Offline

PostPosted: Sat Sep 04, 2004 10:13 pm    Post subject: Reply to topic Reply with quote

Technically according to Ekted, yes that would be bad.

Code: Show/Hide
void foo(int n)
{
     if (n >= 0)
     {
          //code here...
      }
}


Personally I'd add the pre/post conditions as documentation icon_wink.gif ie:
Code: Show/Hide
void foo(int n)
{
//Pre: n must be an integer >= 0.
//Post: (w/e the function does as a result.)
     if (n >= 0)
     {
          //code here...
      }
}

Heck thats a really crappy pre/post condition but it'd work. tongue.gif
_________________
Performance is often the art of cheating carefully. - James Gosling
Back to top
View users profile Send private message Add User to Ignore List
D1st0rt
Miss Directed Wannabe


Age:37
Gender:Gender:Male
Joined: Aug 31 2003
Posts: 2247
Location: Blacksburg, VA
Offline

PostPosted: Sat Sep 04, 2004 10:17 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sat Sep 04, 2004 11:37 pm    Post subject: Reply to topic Reply with quote

D1st0rt wrote:
Ekted, so basically this would be bad?

Code: Show/Hide
void foo(int n)
{
    if(n < 0) return;

    //lots of code here
}


Yes it is. Think of a function as having 3 parts: init, main, cleanup. A return in the middle says "skip the cleanup". Even with tiny funcs it's good practice. I can't tell you the number of times I have seen resource/memory leaks etc caused by internal returns. It's also nicer when you are looking at the function. You know the code "always gets here". You can see what's being cleanup. Less chance of mistakes, easier on the eye.
Back to top
View users profile Send private message Add User to Ignore List
Smong
Server Help Squatter


Joined: 1043048991
Posts: 0x91E
Offline

PostPosted: Sun Sep 05, 2004 6:40 am    Post subject: Reply to topic Reply with quote

Going back to the loop, is while(1) better than for(;;)?
Back to top
View users profile Send private message Add User to Ignore List Visit posters website MSN Messenger
50% Packetloss
Server Help Squatter


Age:40
Gender:Gender:Male
Joined: Sep 09 2003
Posts: 561
Location: Santa Clarita, California
Offline

PostPosted: Sun Sep 05, 2004 11:17 am    Post subject: Reply to topic Reply with quote

Code: Show/Hide
while(1)
0041470E  mov         eax,1
00414713  test        eax,eax
00414715  je          main+29h (414719h)
   {
   }
00414717  jmp         main+1Eh (41470Eh)



   for(;;)
   {
   }
00414719  jmp         main+29h (414719h)


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
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sun Sep 05, 2004 11:24 am    Post subject: Reply to topic Reply with quote

Smong wrote:
Going back to the loop, is while(1) better than for(;icon_wink.gif?


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
View users profile Send private message Add User to Ignore List
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:41
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3615
Location: Las Vegas
Offline

PostPosted: Sun Sep 05, 2004 1:24 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email
50% Packetloss
Server Help Squatter


Age:40
Gender:Gender:Male
Joined: Sep 09 2003
Posts: 561
Location: Santa Clarita, California
Offline

PostPosted: Sun Sep 05, 2004 2:57 pm    Post subject: Reply to topic Reply with quote

I dont know, I used VC++ .net. It seems logical that it would do that though
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address
Solo Ace
Yeah, I'm in touch with reality...we correspond from time to time.


Age:37
Gender:Gender:Male
Joined: Feb 06 2004
Posts: 2583
Location: The Netherlands
Offline

PostPosted: Sun Sep 05, 2004 3:56 pm    Post subject: Reply to topic Reply with quote

Optimization is disabled in VC++.net in debug mode. sa_tongue.gif
Back to top
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Mon Sep 06, 2004 11:37 am    Post subject: Reply to topic Reply with quote

Code: Show/Hide
int foo(int n)
{
   if (n > 0)
      return 1;
   else if (n < 0)
      return -1;
   else
      return 0;
}

Pretty.

Code: Show/Hide
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
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Mon Sep 06, 2004 12:38 pm    Post subject: Reply to topic Reply with quote

You will remember this thread in 10 years when you get severely bitten by this coding style.
Back to top
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Mon Sep 06, 2004 3:19 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Mon Sep 06, 2004 3:35 pm    Post subject: Reply to topic Reply with quote

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. icon_smile.gif
Back to top
View users profile Send private message Add User to Ignore List
-Smong-
Guest


Offline

PostPosted: Mon Sep 06, 2004 6:51 pm    Post subject: Reply to topic Reply with quote

I read somewhere the more handler functions you use the harder it is to use a debugger on the program.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Bot Questions All times are GMT - 5 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 38 page(s) served in previous 5 minutes.

phpBB Created this page in 0.477405 seconds : 49 queries executed (90.7%): GZIP compression disabled