Do Until

This topic contains 11 replies, has 4 voices, and was last updated by  Gary 4 months ago.

  • Author
    Posts
  • #89774

    Gary
    Participant

    Hi guys, I have some spare time today so thought I would spend it on getting better at PS.
    I am doing this challenge but I am a little confused with the output of a do/until.

    My code.

    $Minimum = 1
    $Maximum = 100
    
    Do{
        $Target = Read-Host "Please enter a number between $Minimum - $Maximum"
        }
    Until ($Target -gt "0" -and $Target -lt "101")
    

    I was expecting this to allow any number between 1-100., however, I am only allowed '100' everything else continues in the Do loop.

    Can someone explain why this happens? I assume it is to do with the -and operator...
    I'm fairly sure that I could do this another way, but I would like to work out why it's behaving in this way.

  • #89777

    js
    Participant

    You're comparing strings instead of integers.

    $Minimum = 1
    $Maximum = 100
    
    Do{
        [int]$Target = Read-Host "Please enter a number between $Minimum - $Maximum"
        }
    Until ($Target -gt "0" -and $Target -lt "101")
    
    • #89779

      Andy
      Participant

      I think Powershell is smart enough to interpret $target as an int even though this wasn't explicitly declared [int]$target.
      So the loop should continue until it is true? So if you enter any integer outside the range you keep looping?

    • #89785

      Gary
      Participant

      yes, anything outside of that that isn't '100' i.e. 0,1,2..99 and 101,102..
      at least for all the ones that i tried.

    • #89791

      Andy
      Participant

      $Minimum = 1
      $Maximum = 100

      Do{
      [int]$Target = Read-Host "Please enter a number between $Minimum – $Maximum"
      }
      Until ($Target -gt "0" -and $Target -lt "101")
      Please enter a number between 1 – 100: 99

      PS H:\> $Target | gm

      TypeName: System.Int32

  • #89782

    Gary
    Participant

    Your right and that fixed my problem, but, I still don't get why 100 worked. Shouldn't it fail as well due to the same problem, comparing a string to an integer?

    • #89783

      js
      Participant

      It's comparing 2 strings. I guess it compares them character by character, and "100" is less than "101".

    • #89788

      Gary
      Participant

      not sure if I am just being dense, but so is '99' but that fails.
      Oh wait, I think I get you. Your right, because 1 works as well, i just hadn't tested it.

    • #89789

      js
      Participant

      Try it on the command line:

      "99" -lt "100"
      
      False
      

      I guess in strings if the 9 is greater than the one it's greater.

  • #89794

    Gary
    Participant

    Awesome. Obviously, my issue was not declaring the [int] (Thanks @Andrew). But now I understand the behaviour I was getting where it was comparing each number in 101 individually (thanks @js).

    This is what I came up with, is there anything I could have done better?

    $Minimum = 1
    $Maximum = 100
    
    Do{
        Write-Output "Invalid Number Selected"
        #Must be read as a integer so that -lt and -gt work as expected, otherwise it compairs string to integer
       [int]$Target = Read-Host "Please enter a number between $Minimum - $Maximum"
        }
    Until ($Target -gt "0" -and $Target -lt "101")
        
        # Computer tries to guess number
        $Guess = get-random -Maximum $Maximum -Minimum $Minimum
        Write-Output "My Guess is $Guess"
        
        DO {
            # User says Higher/Lower
            $Direction = Read-Host "Please Type 'H' for Higher, 'L' for Lower."
        
            If ($Direction -eq "L"){
                Write-Output "I will guess a lower value than $Guess."
                $Maximum = $Guess
                $Maximum--
            }
            ElseIf ($Direction -eq "H"){
                Write-Output "I will guess a higher value than $Guess."
                $Minimum = $Guess
                $Minimum++
            }
            Else {
                Write-Output "You have not selected a valid value."
            }
            $Guess = get-random -Maximum $Maximum -Minimum $Minimum
            Write-Output "My new guess is $Guess"
        }
        Until ($Guess -eq $Target)
        $Guess
        Write-Output "I guessed right! 'nWinner, Winner, Chicken Dinner!!"
    
  • #89831

    Bob
    Participant

    In this section of code:
    If ($Direction -eq "L"){
    Write-Output "I will guess a lower value than $Guess."
    $Maximum = $Guess
    $Maximum–
    }
    ElseIf ($Direction -eq "H"){
    Write-Output "I will guess a higher value than $Guess."
    $Minimum = $Guess
    $Minimum++

    if you increment the min or max, don't that crash your code if you are one off?

    • #89863

      Gary
      Participant

      yes 🙁
      Didn't happen until I tried again today...

      Let me see what I can do to fix that.

You must be logged in to reply to this topic.