From WikiChip
Editing mirc/while loops

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.

Latest revision Your text
Line 1: Line 1:
{{mirc title|While Loops}}
+
{{mIRC Guide}}
In many occasions, you may end up doing a task over and over again in a single script (For example, counting from 0 to 10, or sending a message to multiple people or channels). A '''while loop''' is a control flow statement that allows code to be executed repeatedly based on a given condition. The code inside the while loop block will get executed as long as the condition is true.
+
In many occasions, you may end up doing a task over and over again in a single script (For example, counting from 0 to 10, or sending a message to multiple people or channels). A '''while loop''' is a control flow statement that allows code to be executed repeatedly based on a given condition. The code inside the while loop block will get executed as long as the condition is $true.
  
 
== Syntax ==
 
== Syntax ==
Line 11: Line 11:
 
Here is how the while loop works:
 
Here is how the while loop works:
  
#The conditional statement is checked. $v1 and $v2 are generated according to the condition being evaluated.<br />If the statement is true, continue on to step 2.<br />If the statement is false go to step 4.
+
#The conditional statement is checked.<br />If the statement is true, continue on to step 2.<br />If the statement is false go to step 4.
 
#The code inside the while loop (inside the brackets) is executed.
 
#The code inside the while loop (inside the brackets) is executed.
 
#The entire process starts all over again. Going back to step 1.
 
#The entire process starts all over again. Going back to step 1.
 
#If the statement was false.<br />No code inside the while loop is executed and the script skips right down to any code below it.
 
#If the statement was false.<br />No code inside the while loop is executed and the script skips right down to any code below it.
  
=== true conditions ===
+
=== $true conditions ===
So we said the while loop will continue to iterate as long as the condition is true. But what exactly does that mean? In mSL, a condition is true if the outcome of the condition is NOT 0, {{mIRC|$null}}, or {{mIRC|$false}}. For example let %x be 5, if the condition is while ({{mIRC|$calc}}(%x - 5)) {, since 5-5 is 0, the while loop's condition is false, thus it will not execute any code inside it.
+
So we said the while loop while loop will continue to iterate as long as the condition is $true. But what exactly does that mean? In mSL, a condition is true if the outcome of the condition is NOT 0, $null, or $false. For example let %x be 5, if the condition is while ($calc(%x - 5)) {, since 5-5 is 0, the while loop's condition is false, thus it will not execute any code inside it.
 
 
'''Note''': If you are using an operator, for example while (0 == 0) {, the operator is going to define if the condition is true or not, 0 being equal to 0, this condition is true.
 
  
 
==== Example 1 ====
 
==== Example 1 ====
Line 59: Line 57:
 
== Nested Loops ==
 
== Nested Loops ==
  
A ''nested loop'' is a loop that is situated within the body of the other. In a nested loop, the first iteration of the outer loop causes the inner loop to execute. The inner loop will execute its body as long as its condition is true. Upon completion the outer loop executes again, causing the inner loop to execute again. This sequence of events will keep on executing until the outer loop is complete. There is no limit to how many loops can be nested inside each other.
+
A ''nested loop'' is a loop is situated within the body of the other. In a nested loop, the first iteration of the outer loop causes the inner loop to execute. The inner loop will execute its body as long as its condition is true. Upon completion the outer loop executes again, causing the inner loop to execute again. This sequence of events will keep on executing until the outer loop is complete. There is no limit to how many loops can be nested inside each other.
  
 
Take a look at this example:
 
Take a look at this example:
Line 95: Line 93:
 
mIRC support the following types of statements:
 
mIRC support the following types of statements:
  
*The {{mIRC|/break|break statement}}
+
*The break statement
*The {{mIRC|/continue|continue statement}}
+
*The continue statement
*The {{mIRC|/return|return statement}}
+
*The return statement
*The {{mIRC|goto statements|goto statement}}
+
*The goto statement
  
'''Note:''' In this tutorial, we will not cover the {{mIRC|/return}} or the {{mIRC|/goto}} commands.
+
'''Note:''' In this tutorial, we will not cover the /return or the /goto commands.
  
 
==== break statement ====
 
==== break statement ====
Line 168: Line 166:
 
}</syntaxhighlight>
 
}</syntaxhighlight>
  
In the example above we created a loop to go from 0 to 20. The if statement checks if the number is odd. If true, we make it jump to the next iteration (Via the {{mIRC|/continue}} command). The last statement of the loop's body is used to print the number.
+
In the example above we created a loop to go from 0 to 20. The if statement checks if the number is odd. If true, we make it jump to the next iteration (Via the /continue command). The last statement of the loop's body is used to print the number.
  
 
The output is:
 
The output is:
Line 204: Line 202:
 
== Infinite Loops ==
 
== Infinite Loops ==
  
An infinite loop happens when a condition always evaluates to true. Most times, its due to an error. If that's the case, you can force mIRC to break out of it using the '''Ctrl+Break''' key combinations.
+
An infinite loop happens when a condition always evaluates to true. Most times, its due to an error. If that's the case, you can force mIRC to break out of it using the '''Ctrl+Break''' key combinations. In some, very rare occasions, you might want to create an infinite loop on purpose:
Such a condition may be used on purpose, where you need to use the break statement to break out of the loop, but you can always rewrite the code otherwise to avoid this type of condition
 
<syntaxhighlight lang="mirc">
 
;returns a random nickname on a channel while excluding yourself ($me) from the list
 
while (1) {
 
  if ($nick($chan,$r(1,$nick($chan,0))) != $me) {
 
    echo -a $v1
 
    break
 
  }
 
}
 
 
 
;Equivalent:
 
  
while ($nick($chan,$r(1,$nick($chan,0))) == $me) /
+
<syntaxhighlight lang="mirc">while (1) {
echo -a $v1
+
  ;code
 
+
}</syntaxhighlight>
</syntaxhighlight>
 
 
 
== Keeping mIRC responsive ==
 
 
 
Whilst your loops are looping, mIRC is not able to process any other activities such as messages sent from the server or your own keystrokes or mouse clicks. So while loops which loop a lot of times can result in mIRC appearing to lag or be unresponsive.<br>
 
 
 
For these situations there are several techniques you can use to mitigate this:
 
 
 
a. Rather than iterating through a hash table item by item or a custom list window line by line to find what you want, use mIRC functionality to search for what you are seeking i.e. using $hfind, $fline etc.
 
 
 
b. Where mIRC allows it, use the /command parameter for identifiers like $hfind $fline $regsubex etc. or using /filter's -k alias, so that the identifier calls the code to process what you found rather than exiting the find, doing the processing and then re-entering the find again.
 
 
 
For example, if you use $findfile inside a while() loop handling the Nth file individually, mIRC is forced to repeat the disk search N times, while using the /command parameter inside $findfile allows performing the disk search just 1 time.
 
 
 
Note: If you use the /command parameter, mIRC will not be able to process any other messages whilst the identifier and the /commands process and mIRC may become unresponsive for an extended period. If you don't use the /command parameter you can use the next technique to keep mIRC responsive, but at the cost of additional elapsed time to complete the search.
 
 
 
c. Split the loops into smaller chunks and use .timer 0 1 to queue the next chunk of iterations, letting mIRC process any server messages, keystrokes and mouse clicks before running the timer.
 
 
 
d. Make your while() loop more efficient by enabling it to use less overhead. If you are processing the lines in a disk file in a way which can't be accomplished using /filter -k, ensure your while (condition) itself uses fewer resources.
 
 
 
<pre>
 
var %i 0 | while (%line <= $lines(file.txt)) { inc %i | do stuff }
 
var %i 0 , %total $lines(file.txt) | while (%line <= %total) { inc %i | do stuff }
 
</pre>
 
 
 
The 1st alternative will be slower due to repeatedly calculating the same thing.<br>
 
  
e. If your while loop is echoing screen output, you can speed up the loop by temporarily (or permanently) setting the /fupdate value to a higher number in the 0-100 range. If your loop is adding lines to a @window, the write is much faster if the window is hidden or minimized.
+
[[Category:mIRC]]

Please note that all contributions to WikiChip may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see WikiChip:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)