Hey everyone,

I am a novice when it comes to powershell programming. I am currently taking a college course and have a question about a statement my teacher sent me in an email. We are working on a project to create a rock paper scissors game and the code has a while loop. I have used the following statement for this loop:

while ($gamePlayControl -eq “True”) {

“do stuff in loop”

}

I had some questions about our code as it was not lining up with some of the instructions and needed to know if our group needed to scrap our code and start from scratch. She sent me an email with a few tips, but one specifically said the following:

" If you are having issues with the main loop, try using this instead:
While ($gamePlayControl -ne “False”)

You are saying while it’s not equal to True, so that leaves a lot of options open, when what you really want is to loop as long as it’s not False, then you set it to False later."

I thought “While ($gamePlayControl -eq “True”)” and “While ($gamePlayControl -ne “False”)” is saying the same thing. Is that true, or am I missing something? Thanks in advance.

3 Spice ups

Welcome

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

ok so there is $true (boolean) and there is ‘true’ which is actually a string

If it would be a boolean value, yes it would be the same thing, however since you are comparing strings, no it is not the same.

While ($gamePlayControl -eq "True")

That means it is true as long as ‘$gamePlayControl’ has the value ‘true’ [string]

While ($gamePlayControl -ne "False")

This means ‘$gamePlayControl’ that it can have ANY value as long as it is not ‘false’ [string]

2 Spice ups

Look at the ouput if this:

$value = $true  #boolean
$value.GetType()

$value = 'true' #string
$value.GetType()
IsPublic IsSerial Name                                     BaseType                                
-------- -------- ----                                     --------                                
True     True     Boolean                                  System.ValueType                        
True     True     String                                   System.Object                           

while($gamePlayControl){ #eval for boolean to be $true
    # do something
}

while($gamePlayControl -eq 'true'){ #eval for string to be 'true'
    # do something
}

I hope that makes sense. You have to compare the same type, you can not compare apples to oranges (strings to boolean)

1 Spice up

Two things:
Is your teacher really comparing to the string “False” or did she have $false?
What do you need to do if $gamePlayControl is Null or not instantiated or populated yet?

2 Spice ups

$true and $false are Boolean


```
PS> test $TRUE
TRUE
PS> test $FALSE
FALSE
PS> test TRUE
TRUE
PS> test FALSE
TRUE
```

function test ($VALUE) 
{
     if ($VALUE) {
          Write-Host -ForegroundColor GREEN “TRUE”
          } 
      else {
           Write-Host -ForegroundColor RED   “FALSE”
           }
}

“rue” and 'false are strings

“true” is not equal to "“True”

Thank you everyone for your replies.

Neally: Thank you so much for that clarification. I was under the incorrect impression that “True” and “False” is how Powershell interprets a boolean. It makes a lot more sense now.

Gary M G: The variable is initiated at the beginning of the script with the string “True”. This is how she said to do it in her instructions. I know her instructions are based on a script she has that she did not write for a game of “Rock, Paper, Scissors”. I get the impression that she is not very strong in Powershell scripting, but that is just my opinion. It seems to me that there are better ways to write this script, but again, that’s my opinion.

David452309: Thank you for your reply. It was very helpful.