During writing a script, I found elseif is not working properly. I feel it may be a bug, can you please check your end and confirm me, also if you find any error, please rectify me too…
The Code - (Sample)
do {
$X= Read-Host " Please type any number"
if ($X -gt 0) {
"$X is greater than zero"
}
ElseIf ($X -lt 0) {
"$X is negative"
}
Else {
"This Number appears to be zero"
}
}
until ($X -eq "")
The problem is the negative value cannot be detected. But, if I use a number with decimal point, then it’s working. Suppose if I input .1, then it shows “.1 is negative”.
I tried removing the DO loop too, but the same result…
You’ll also see that $X.GetType() is a string. But actually, if you put the 0 first in the comparison, it will cast $X to an int32 for the comparison.
do {
$X= Read-Host " Please type any number"
if (0 -lt $X) {
"$X is greater than zero"
}
ElseIf (0 -gt $X) {
"$X is negative"
}
Else {
"This Number appears to be zero"
}
}
until ($X -eq "")
Looks like ‘5’ -gt ‘1’ is true. But not ‘-5’ -lt ‘1’ and not ‘15’ -gt ‘2’. I think it goes by the ascii order of the first character. That’s why a numeric sort is different from a string sort. Actually I’m not sure why the ‘-5’ isn’t less than a ‘1’.