| View previous topic :: View next topic |
| Author |
Message |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Mon Jan 21, 2008 4:55 am Post subject: [Delphi]Looping With Limits |
|
|
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 |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Mon Jan 21, 2008 5:09 am Post subject: |
|
|
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 |
|
 |
Reak I post too much
Reputation: 0
Joined: 15 May 2007 Posts: 3496
|
Posted: Mon Jan 21, 2008 6:04 am Post subject: |
|
|
| And don't forget to allways add the line Application.ProcessMessages in a loop. |
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Mon Jan 21, 2008 6:06 am Post subject: |
|
|
| rEakW0n wrote: | | And don't forget to allways add the line Application.ProcessMessages in a loop. |
not always needed. |
|
| Back to top |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Mon Jan 21, 2008 7:01 am Post subject: |
|
|
Application.ProcessMessages is DoEvents, right?
Its not needed, unless its an infinite loop/very long loop. |
|
| Back to top |
|
 |
Reak I post too much
Reputation: 0
Joined: 15 May 2007 Posts: 3496
|
Posted: Mon Jan 21, 2008 7:15 am Post subject: |
|
|
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 |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Mon Jan 21, 2008 7:22 am Post subject: |
|
|
| 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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Jan 21, 2008 10:54 am Post subject: |
|
|
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 |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Mon Jan 21, 2008 12:33 pm Post subject: |
|
|
| 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 |
|
 |
rapion124 Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Mar 2007 Posts: 1095
|
Posted: Mon Jan 21, 2008 5:17 pm Post subject: |
|
|
| 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 |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Tue Jan 22, 2008 7:01 am Post subject: |
|
|
| 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 |
|
 |
|