Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Delphi]Looping With Limits

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
DeletedUser14087
I post too much
Reputation: 2

Joined: 21 Jun 2006
Posts: 3069

PostPosted: Mon Jan 21, 2008 4:55 am    Post subject: [Delphi]Looping With Limits Reply with quote

Today i'm going to show you two examples of how useful the While loop is.

A method for how to stop the loop until the value = true

Code:
Procedure Eh;
var no:integer;
Begin
no:=0;

 While 1=1 do
  if Statement = True then
  Begin
  //Code
  no:=no+1;
  if no:=X then break; //X = any number you disire
  End;
End;



Another method is to check wether a boolean is true or false (thanks to appalsap for the hint)

Code:

var
Lol:Boolean //Global dec


Procedure Renko;
Begin
Lol:=True;
 
 While Lol = True do
  Begin
  ShowMessage('Hi');
  Lol:=False; //breaking the loop here while we turn the true boolean to false
  End;
End;


Last edited by DeletedUser14087 on Mon Jan 21, 2008 5:11 am; edited 1 time in total
Back to top
View user's profile Send private message
Symbol
I'm a spammer
Reputation: 0

Joined: 18 Apr 2007
Posts: 5094
Location: Israel.

PostPosted: Mon Jan 21, 2008 5:09 am    Post subject: Reply with quote

You can use for loop the same way, instead of assigning a new value to the variable, using while, incrasing the value of a variable and then breaking, you can just use
for variable := value to/downto value do begin
ShowMessage('This is better');
end;
Back to top
View user's profile Send private message
Reak
I post too much
Reputation: 0

Joined: 15 May 2007
Posts: 3496

PostPosted: Mon Jan 21, 2008 6:04 am    Post subject: Reply with quote

And don't forget to allways add the line Application.ProcessMessages in a loop.
Back to top
View user's profile Send private message
DeletedUser14087
I post too much
Reputation: 2

Joined: 21 Jun 2006
Posts: 3069

PostPosted: Mon Jan 21, 2008 6:06 am    Post subject: Reply with quote

rEakW0n wrote:
And don't forget to allways add the line Application.ProcessMessages in a loop.


not always needed.
Back to top
View user's profile Send private message
Symbol
I'm a spammer
Reputation: 0

Joined: 18 Apr 2007
Posts: 5094
Location: Israel.

PostPosted: Mon Jan 21, 2008 7:01 am    Post subject: Reply with quote

Application.ProcessMessages is DoEvents, right?
Its not needed, unless its an infinite loop/very long loop.
Back to top
View user's profile Send private message
Reak
I post too much
Reputation: 0

Joined: 15 May 2007
Posts: 3496

PostPosted: Mon Jan 21, 2008 7:15 am    Post subject: Reply with quote

Ya it's not needed on short loops.
But if the loop takes more then 5 seconds or so, you should use it or the process wont handle messages like Close, ...etc.

Just wanted to say this, not that ppl start wondering why they can't close their application anymore or handle any other message/procedure.
Back to top
View user's profile Send private message
DeletedUser14087
I post too much
Reputation: 2

Joined: 21 Jun 2006
Posts: 3069

PostPosted: Mon Jan 21, 2008 7:22 am    Post subject: Reply with quote

rEakW0n wrote:
Ya it's not needed on short loops.
But if the loop takes more then 5 seconds or so, you should use it or the process wont handle messages like Close, ...etc.

Just wanted to say this, not that ppl start wondering why they can't close their application anymore or handle any other message/procedure.


thanks for pointing it out, i hope people will start using it
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Mon Jan 21, 2008 10:54 am    Post subject: Reply with quote

While 1=1 is bad practice and can lead to an infinite loop. You should always use a condition that can be triggered from an outside source like your second example.

I don't code in Delphi but from looking at other code and shit:

Code:
var
   bWantsExit:Boolean

Procedure WhileLoop
Begin
   bWantsExit := False;

   While bWantsExit = False do
      Begin
      // .. Code Here ..
   End;
End;


Like your second example, this lets other procedures have access to bWantsExit to kill the loop and let the program exit without issue. Highly suggested to do this with threads a well as adding a Sleep( ) of a minimum of 10milliseconds in the loop too to not hammer the system to hell and cause freezing.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
DeletedUser14087
I post too much
Reputation: 2

Joined: 21 Jun 2006
Posts: 3069

PostPosted: Mon Jan 21, 2008 12:33 pm    Post subject: Reply with quote

Wiccaan wrote:
While 1=1 is bad practice and can lead to an infinite loop. You should always use a condition that can be triggered from an outside source like your second example.

I don't code in Delphi but from looking at other code and shit:

Code:
var
   bWantsExit:Boolean

Procedure WhileLoop
Begin
   bWantsExit := False;

   While bWantsExit = False do
      Begin
      // .. Code Here ..
   End;
End;


Like your second example, this lets other procedures have access to bWantsExit to kill the loop and let the program exit without issue. Highly suggested to do this with threads a well as adding a Sleep( ) of a minimum of 10milliseconds in the loop too to not hammer the system to hell and cause freezing.


yes, that's the point.

and for Sleep(10) is to save cpu useage
Back to top
View user's profile Send private message
rapion124
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Mar 2007
Posts: 1095

PostPosted: Mon Jan 21, 2008 5:17 pm    Post subject: Reply with quote

While loops are usually used if you want an infinite loop (threads). If you want your program to do something a speicified number of times, use the for loop. It's much faster than while.
Back to top
View user's profile Send private message
DeletedUser14087
I post too much
Reputation: 2

Joined: 21 Jun 2006
Posts: 3069

PostPosted: Tue Jan 22, 2008 7:01 am    Post subject: Reply with quote

rapion124 wrote:
While loops are usually used if you want an infinite loop (threads). If you want your program to do something a speicified number of times, use the for loop. It's much faster than while.


Code:
Procedure flashget;
var appal:integer;
begin
for appal:=X to Y do
 Begin
 //code
 End;
End;


X = From Y = To

Eg. from 1 to 4 do (it'll do it 4 times)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot 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 cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites