mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jan 2003
Posts: 4
I
Self-satisified door
OP Offline
Self-satisified door
I
Joined: Jan 2003
Posts: 4
There is an example on the "If-then-else statements" page of the mIRC help file that displays a misleading message:

Code:
number {
  if (($1 > 0) && ($1 < 10)) {
    if ($1 < 5) echo Number is less than five
    else echo Number is greater than five
  }
  else echo Number is out of bounds
}
 


If you run this alias with the number 5 it reports "Number is greater than five". Hmmm... maybe it should say "Number is five or greater" smirk


Buy the best and cry only once
Joined: Dec 2002
Posts: 212
V
Fjord artisan
Offline
Fjord artisan
V
Joined: Dec 2002
Posts: 212
hehe
a BIG one! ;b


And all I need now is intellectual intercourse, a soul to dig the hole much deeper
Joined: Dec 2002
Posts: 87
V
vcv Offline
Babel fish
Offline
Babel fish
V
Joined: Dec 2002
Posts: 87
You NEED the { }'s
i.e.
Code:
number {

  if (($1 > 0) && ($1 < 10)) {
    if ($1 < 5) { echo Number is less than five }
    else { echo Number is greater than five }
  }
  else { echo Number is out of bounds }
}

Joined: Dec 2002
Posts: 6
A
Nutrimatic drinks dispenser
Offline
Nutrimatic drinks dispenser
A
Joined: Dec 2002
Posts: 6
no you dont.. but in this case you do laugh

Last edited by airliner; 25/01/03 02:38 PM.
Joined: Dec 2002
Posts: 1,321
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Dec 2002
Posts: 1,321
Why do you NEED the braces? Braces are used to 1) group commands together under a conditional statement (if-elseif-else or while) or 2) to continue the script across multiple lines without using a $& line break identifier.
Code:

      if (condition) {
        single command
      }
 
      if (condition) single command
 
      if (condition) $&
        single command

All three of these IF examples are exactly equivalent in what they do and how they do it, but they vary in how they are expressed. You might choose to always include every possible brace you can think of, and that's your choice. Sometimes it's quite useful for readability to do so, but it is certainly not required. I frequently will do so if I'm posting code in the code blocks (which do not line wrap) to prevent the entire thread from scrolling to the right. The same reasoning can be applied in your own code: if it helps you to read and understand what you're writing, by all means, do so. All you lose is a little script optimization.
Code:
 
number {
 
  if ($1 !isnum 1-9) echo Number is out of bounds.
  elseif ($1 < 5) echo Number is less than five.
  else echo Number is greater than [color:red]or equal to[/color] five.
 
}

The only braces that are REQUIRED in this example are the ones that open and close the alias because it extends across multiple lines, and because it contains multiple commands under one block (the alias name). In some cases, it makes absolutely no difference if you use one form or the other. In a one-time through alias that happens very rarely, there will be no noticable difference, even if you time with ticks. However, the differences RAPIDLY become apparent if you time it using $ticks and you are using a loop or multiple loops. Time it yourself and see. If you are doing something that can happen many times, such as on TEXT (in a flood condition) or on JOIN (in a join/part flood condition), speed becomes a factor. The tighter your code, the less it will appear to lag you. The above rewrite of the original doesn't need any of the extraneous braces and it does exactly the same thing without all the extra punctuation.
smirk
What Innkeeper was objecting to was the fact that or equal to wasn't in the original. Thus, if the number was exactly 5, the original alias would report that 5 was greater than 5. The choices here are simple: Number is less than five, Number is not less than 5, or Number is out of bounds. How you choose to say Number is not less than five is entirely up to you. The original alias is not correct in that respect.

However, the purpose of the original number alias was to show several things: multi-line braces and multi-conditional expressions, code blocks and how to use if-else. It just needs a semantic correction. Try the original yourself and you will see the problem and that it DOES work as written (other than Innkeeper's original correction).


DALnet: #HelpDesk and #m[color:#FF0000]IR[color:#EEEE00]C

Link Copied to Clipboard