From WikiChip
Difference between revisions of "mirc/while loops"
< mirc

(Infinite Loops)
(Infinite Loops)
Line 213: Line 213:
 
   }
 
   }
 
}
 
}
echo -a %nick
+
 
 
;Equivalent:
 
;Equivalent:
  

Revision as of 09:59, 25 November 2014

Template: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.

Syntax

The basic syntax of a while loop is:

while (<condition>) {
  ;Code here will be executed
}

Here is how the while loop works:

  1. The conditional statement is checked.
    If the statement is true, continue on to step 2.
    If the statement is false go to step 4.
  2. The code inside the while loop (inside the brackets) is executed.
  3. The entire process starts all over again. Going back to step 1.
  4. If the statement was false.
    No code inside the while loop is executed and the script skips right down to any code below it.

$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, $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.

Example 1

Take a look at the following alias:

alias whileExample1 {
  echo -a Line number: 1
  echo -a Line number: 2
  echo -a Line number: 3
  echo -a Line number: 4
  echo -a Line number: 5
  echo -a Line number: 6
  echo -a Line number: 7
  echo -a Line number: 8
  echo -a Line number: 9
  echo -a Line number: 10
}

This simple alias prints "line number: " follows by the line number, 1 to 10. This simple alias has lots of repeated code; The ideal place for a while loop.

We can rewrite that alias:

Alias whileExample1 {
  var %line = 1
  while (%line <= 10) {
    echo -a Line number: %line
    inc %line
  }
}

Using the while loop, we can repeat the echo statement as many times as we want. Let's take a look at what's going on:

  1. We create a local variable called "%line" and assign it the number 1
  2. The while loop checks our conditional statement.
    As long as "%line" is less than or equal to 10, we can enter the while loop.
  3. The first statement inside the while loop will cause mIRC to print to the active window "Line number: " follows by the value of "%line".
  4. The second statement inside the while loop will cause the "%line" variable to increase by 1 (if no number is specified the default is one).
  5. Go back to step 2.

Nested Loops

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:

alias nestedLoopExample {
  var %x = 1
  ;outer loop
  while (%x <= 3) {
    var %y = 1
    ;inner loop
    while (%y <= 3) {
      echo -a %x - %y
      inc %y
    }
    inc %x
  }
}

This code will generate the following output:

1 - 1
1 - 2
1 - 3
2 - 1
2 - 2
2 - 3
3 - 1
3 - 2
3 - 3

Jump Statements

Jump statements are used to perform an immediate transfer of control. Using jump statements, you can effectively break out of the current loop, jump to the beginning of the current, or transfer program control to another part of the program.

mIRC support the following types of statements:

  • The break statement
  • The continue statement
  • The return statement
  • The goto statement

Note: In this tutorial, we will not cover the /return or the /goto commands.

break statement

The break statement lets you break out of the currently executing while loop at any point. The break statement will only break out of the while loop in which it is nested in.

break

In the example below, we set variable "%x" to 10 and decrease it by one each time. When "%x" reaches 5, break out of the loop.

alias breakDemo {
  var %x = 10
  while (%x > 0) {
    if (%x == 5) break
    echo -a %x
    dec %x
  }
}

The output is:

10
9
8
7
6

If multiple while loops are involved, the outer loops will not be effected.

alias multLoopDemo {
  var %x = 5
  while (%x) {
    echo -a %x
  
    while ($true) {
      break
      ; anything here will never be executed
      echo -a You will never see this.
    }
  
    dec %x
  }
}

The output is:

5
4
3
2
1

continue statement

A continue statement in mIRC will cause the program control to jump to the end of the loop body. causing it to evaluate the conditional statement again skipping any subsequent code. A continue statement can only be used within a loop.

The continue statement has the form

continue

Take a look at this example:

alias listEven {
  var %x = 1
  while (%x < 20) {
    inc %x
    if (%x & 1) continue
    echo -a %x
  }
}

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:

2
4
6
8
10
12
14
16
18
20

Notice, the result is all the even numbers between 2 to 20. If you are wondering how did it echo 20 even though our conditional statement tells mIRC anything less than 20. We have an answer: when %x gets to 19, the if statement will cause the /continue command to execute, as a result, the program control goes back to the conditional statement, 19 < 20, which is true. %x then gets increased by 1 to 20, which will then make it to the echo command.

Reference of parameters

mIRC provides two identifiers to retrieve the first or second parameter of the while's conditional statement. Please note, the identifiers will return the first and second parameter of the $TRUE condition following short-circuit evaluation guidelines.

$v1 and $v2

In the example below we will count from 1 to 10 using a while loop. Variable "%a" will be set to 1, the loop will keep executing as long as %a is less than or equal to 10.

alias refExample {
  var %a 1
  while (%a <= 10) {
    echo -a Count: $v1
    inc %a
  }
}


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. 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

;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) /
echo -a $v1