Seems find a Bug, Can anyone plz confirm

This topic contains 5 replies, has 4 voices, and was last updated by  js 3 weeks, 1 day ago.

  • Author
    Posts
  • #99928

    Sankhadip Roy
    Participant

    Hi Team,

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

    Regards,
    Roy.

  • #99933

    Olaf Soyk
    Participant

    You're not checking numbers. You are checking strings at the moment! 😉

  • #99936

    Don Jones
    Keymaster

    Agreed. The odds of there being a bug with ElseIf are slim ;).

    $X is a string, not a number. “A” cannot be greater than zero; even if the string is a digit, comparisons don't work that way.

    Try:

    [int]$X = Read-Host “Whatever”

    To further understand this, try running this in the console, typing each line and hitting Enter:

    $a = 5
    $b = “5”
    $a + $b
    $b + $a
    $a -gt 1
    $b -gt 1
    
    • #99985

      Sankhadip Roy
      Participant

      Hi Don / Team,

      Did the test as Mr.Don suggested. And as usual get confused and looking for the answer.

      $a = 5 | As per rule, this is automatically treated as Int32 type.

      $b = “5” | As per rule, the DoubleQuotes help to treat as String type.

      $a + $b | Is the concept is like, if the first variable type is Int, then the second variable also treated as Int?

      $b + $a | Same concept, for string type it's concatenating!! Am I right?

      $a -gt 1 |Both cases it's true!! For int, it's okay? But for string!! How?
      $b -gt 1

      I am confused on the math and comparison operation. What is the concept? Please help me to understand.

      Regards,
      Roy.

    • #99994

      js
      Participant

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

      PS C:\users\admin> [int[]][char[]]'-5'
      45
      53
      PS C:\users\admin> [int[]][char[]]'1'
      49
      
  • #99951

    js
    Participant

    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 "") 
    

You must be logged in to reply to this topic.