mIRC Homepage

'if'

Posted By: Collective

'if' - 10/12/02 10:00 AM

When I type
//if ( 0 ) { echo -a a }
it doesn't echo 'a', but when I type
//if ( 1 ) { echo -a a }
(or anything else in the ( )) it does echo 'a'.

What are the rules for whether it will echo or not, as it seems a bit odd to not echo it just when 0 is put in, because 0 isn't anything special.
Posted By: Insane

Re: 'if' - 10/12/02 10:18 AM

yeah, that's the way it should be, replace 0 with $false and 1 with $true - same result :>
Posted By: Skip

Re: 'if' - 10/12/02 10:20 AM

the 0 being 'false' would be logical if you were using integer based return replies etc. Its somewhere in the .hlp file i think, but one part of the help file is wrong:

"If-then-else statements"
Code:
if (%x == $null) echo x has no value

if (!%x) echo x has no value


.. because %x could indeed be 0. It's good practice to play it safe when you don't know what a variable/identifier could return.

Other 'false' values are $null and $false - there might be more wink
Posted By: Hammer

Re: 'if' - 10/12/02 11:12 AM

0 = $false = $null for conditional expressions .. all fail.

!0 = $true = string .. all match the condition as not being false.

These are handy to use in several instances:

Code:
  var %i = $nick($chan,0) | ; %i is the total number of nicks on $chan
  ;
  ; while we have not reached 0, process the nicks backwards
  while (%i) {
    ; Do whatever you need to do in here
    dec %i
  }

The reverse is also handy when working backwards is not an option, such as if you're writing something to a file in order. You can, however, simply check to see if you've run past the end of whatever list you're working from, such as a nicklist or a list @window.
Code:
  var %i = 1
  ; While there are more lines to write to the file, keep going.
  while ($line(@ListWin,%i)) {
    write ListWin.txt $ifmatch
    inc %i
  }

or
Code:
  if (!%some.timed.variable) {
    ; !%variable means the logical opposite of that variable; either $true or $false, as described above.
    ;
    ; Do whatever needs to be done if the timed variable has:
    ;  1) expired or been /unset ($null)
    ;  2) been reduced to zero (0)
    ;  3) been set to $false ($false)
  }
  else {
    ; Update other stuff if it hasn't. (Anything else is a logical $true.)
  }

Hopefully, these examples will help you to understand. laugh
Posted By: Skip

Re: 'if' - 10/12/02 11:35 AM

I guess I didn't quote the help file properly.

What I meant to show was that using if (%var == $null) compared to if (!%var) is not actually the same comparision (as is quoted by the help file - but I grabbed the wrong lines.. :P).

Strings _can_ effect conditionals:
Code:
alias testif {
  var %test = $1-
  echo -s String: %test
  echo -s Lazy True: $iif(%test,y,n)
  echo -s Full True: $iif(%test != $null,y,n)
  echo -s Lazy False: $iif(!%test,y,n)
  echo -s Full False: $iif(%test == $null,y,n)
}

then /testif 1, /testif 0, /testif $false, /testif $true - you could even manually set variables as '$true' '$false' '0' '1' etc and use $iif - same result.

Of course when one knows exactly what their variable will contain then using the shorter comparisions would come as an advantage, but for things like remote events the 'full' comparisions are more fitting.

I didn't mean to sound confusing. wink
Posted By: Hammer

Re: 'if' - 10/12/02 12:16 PM

I see what you are saying but you have made just a few incorrect assumptions:

You say that strings *can* affect conditionals. This is correct because they are *supposed to* affect them.

Quote:
What I meant to show was that using if (%var == $null) compared to if (!%var) is not actually the same comparision (as is quoted by the help file - but I grabbed the wrong lines.. :P).


This is correct, as far as it goes. "if (!%var)" literally means "if ((%var == 0) || (%var == $false) || (%var == $null))" .. and not just any of the three checks. However, since you, the scripter, should know what values you're putting into that %var, then you should check for exactly what it is you want to check for, be that == $null), == $false), == 0) or (!%var). No, 0 != $null; you can decrement a variable to 0, at which point is becomes !$true, but you might still need that 0 value as a value that is valid in and of itself...or you might not. It depends on the needs of your script and what, exact, it is that you're checking for.

For example: a clone kicker could use a while condition to kick clone nicks out of the channel such that:

Code:
  ; Ban the clones using $wildsite
  mode $chan +b $wildsite
  ;
  ; Once $ialchan($wildsite,$chan,0) reaches 0, then the following condition will return the number
  ; of remaining nicks (also 0 .. or $false) on $chan.
  while ($ialchan($wildsite,$chan,0)) {
    ;
    kick $chan $ialchan($wildsite,$chan,$ifmatch).nick Clones begone.
    ;
    ; After the kick, the number of remaining clones *should* be reduced by one unless more have 
    ; made it into the channel (lag can sometimes let this happen).
  }

In the above script snippet, I *could* have checked to see if ($ialchan($wildsite,$chan,0) == 0) specifically, but that would simply add another layer of comparison that isn't needed since the original comparison resolves the way I want it to. If I'd have wanted to, I could have just had it check for ($ialchan($wildsite,$chan,0).nick) and then kicked $ifmatch, since the condition will return $null (which equates to $false) when no more nicks are found and drop out of the while loop ... same principal, slightly different application.
Code:
  mode $chan +b $wildsite
  while ($ialchan($wildsite,$chan,0).nick) kick $chan $ifmatch Clones begone.

As for your alias, your Full False (and Full True, for that matter) ONLY checks to see if it's $null or not; it doesn't check for 0 or $false, thus it's incomplete in terms of complete (Full) evaluation of the variable. Anything that is not 0, $false or $null is true. ANYTHING at all...be it a string, a non-zero number or $true. Of course, the reverse is also true. Anything that is 0, $false or $null is !$true and will fail in any conditional expression (such as in an If, ElseIf or While condition).
Posted By: GrimZ

Re: 'if' - 10/12/02 12:34 PM

0 is equal to the boolean FALSE in any programming/scripting language I code in.
© 2019 mIRC Discussion Forums