While Loop with multiple conditions displaying incorrect message

Hello Guys
This script prompts a user for input values 0 or 1 or 2. if the user input doesn’t equal to 0 or 1 or 2 then an error message will be displayed “Incorrect option selected”. But when I execute the script the user inputs equal 0 or 1 or 2 the “Incorrect option selected” error message is displayed instead of displaying a message for the option that the user selected. Can someone please review my code below and help me figure this out. I used PowereShell ISE and Visual Studio Code and got the same result. Thanks in advance.

#Declaring variable name $select and setting the value to be empty
$select = ""

#Check $select variable is not equal to 0 or 1 or 2. If $Select is equal to 0 or 1 or 2 loops breaks
while (($select -ne 0) -or ($select -ne 1) -or ($select -ne 2))
{   
    #promto user to select option 0,1,2
    Write-Host "Choose and option";$select = Read-Host 

    #Error trap. display error message if user enter incorrect option    
    if (($select -ne) 0 -or ($select -ne 1) -or ($select -ne 2))
            {
                write-host "Incorrect option elected"
            }
}
#If $Select is equal to 0 or 1 or 2 display a message for the selected option.
switch ($select)
{
          #Display welcome message
        0 { 
             Write-Host "Welcome "
          }
          
          #Display Hello message
        1{
             Write-Host "Hello"
         }

          #Display Hi message
        2{
              Write-host " Hi"
         }

}

Rather than checking each variable, it’s a bit more straight forward create an array and see if the value is in the array with contains\notcontains. Also, take it easy on the comments, almost every thing you commented on could be easily read in the code. Use comments sparingly to indicate something that is not obvious in the code or logic:

while ( 0..2 -notcontains $select ) {   
    $select = Read-Host -Prompt 'Choose a number between 0 and 2'
 
    if ( 0..2 -notcontains $select ) {
        write-host "Incorrect option elected, choose a number 0 - 2"
    }
}

switch ( $select ) {
    0 { Write-Host "Welcome" }
    1 { Write-Host "Hello" }
    2 { Write-host "Hi" }
}

Your problem is that this line

if (($select -ne 0) -or ($select -ne 1) -or ($select -ne 2))

will always return True because of the way the -or operator works. If any of the statements are True then the -or operator will return True.

[quote quote=275370]Rather than checking each variable, it’s a bit more straight forward create an array and see if the value is in the array with contains\notcontains. Also, take it easy on the comments, almost every thing you commented on could be easily read in the code. Use comments sparingly to indicate something that is not obvious in the code or logic:

<link rel=“stylesheet” type=“text/css” href=“https://powershell.org/wp-content/plugins/urvanov-syntax-highlighter/themes/powershell-ise/powershell-ise.css”>
<link rel=“stylesheet” type=“text/css” href=“https://powershell.org/wp-content/plugins/urvanov-syntax-highlighter/fonts/liberation-mono.css”>

PowerShell
<textarea class="urvanov-syntax-highlighter-plain print-no" data-settings="dblclick" readonly="" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;">while ( 0..2 -notcontains $select ) { $select = Read-Host -Prompt 'Choose a number between 0 and 2'
if ( 0..2 -notcontains $select ) {
    write-host "Incorrect option elected, choose a number 0 - 2"
}

}

switch ( $select ) {
0 { Write-Host “Welcome” }
1 { Write-Host “Hello” }
2 { Write-host “Hi” }
}</textarea>

1
2
3
4
5
6
7
8
9
10
11
12
13
while ( 0..2 -notcontains $select ) {
$select = Read-Host -Prompt 'Choose a number between 0 and 2'
if ( 0..2 -notcontains $select ) {
write-host "Incorrect option elected, choose a number 0 - 2"
}
}
switch ( $select ) {
0 { Write-Host "Welcome" }
1 { Write-Host "Hello" }
2 { Write-host "Hi" }
}
[/quote]

This worked :)!!!.Thanks a lot. Do you have a possible reason why my code didn’t work. I just need some clarification so I will know better for next time.
sorry about the comments its a probably a bad habit that i use to keep track about that i doing or intend to do.