Structure If not working

Contributor

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

1 Reply

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

 

LainRobertson_0-1668169509759.png

 

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

 

 

LainRobertson_1-1668169620489.png

 

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