Forum Discussion

balubeto's avatar
balubeto
Brass Contributor
Nov 11, 2022

Structure If not working

Hi

 

I created this simple script

 

 

$Number1 = 0
$Number2 = 0

$Number1 = Read-Host "Insest an first number"
$Number2 = Read-Host "Insest an second number"

If ($Number1 -ne $Number2)
{
    If ($Number1 -gt $Number2)
    {
        Write-Host "$Number1 > $Number2"
    }
    elseif ($Number1 -lt $Number2)
    {
        Write-Host "$Number1 < $Number2"
    }
else
{
    Write-Host "$Number1 = $Number2"
}
}

 

 

but, sometimes, it gives wrong results. How come?

 

Thanks

 

Bye

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    balubeto 

     

    Because the output from Read-Host is a string, not a number.

     

    So, if you answer "5" for $Number1 and "44" for $Number2, your script will incorrectly inform you that 5> 44.

     

     

    Now, if you tell PowerShell that the results from the Read-Host commands are intended to be integers, you get the proper outcome.

     

     

    $Number1 = 0
    $Number2 = 0
    
    [int] $Number1 = Read-Host "Insest an first number"
    [int] $Number2 = Read-Host "Insest an second number"
    
    If ($Number1 -ne $Number2)
    {
        If ($Number1 -gt $Number2)
        {
            Write-Host "$Number1 > $Number2"
        }
        elseif ($Number1 -lt $Number2)
        {
            Write-Host "$Number1 < $Number2"
        }
        else
        {
            Write-Host "$Number1 = $Number2"
        }
    }

     

     

     

    Of course, there's no error checking in the script, so if you type in something that cannot be converted to an integer, you'll just get a bunch of errors, but this is the answer to why something that looks like a smaller number can be evaluated as being bigger.

     

    Cheers,

    Lain

Resources