Welcome › Forums › General PowerShell Q&A › While Loop with multiple conditions displaying incorrect message
- This topic has 3 replies, 3 voices, and was last updated 1 month, 3 weeks ago by
Participant.
-
AuthorPosts
-
-
November 29, 2020 at 7:28 pm #275148
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.PowerShell12345678910111213141516171819202122232425262728293031323334#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 breakswhile (($select -ne 0) -or ($select -ne 1) -or ($select -ne 2)){#promto user to select option 0,1,2Write-Host "Choose and option";$select = Read-Host#Error trap. display error message if user enter incorrect optionif (($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 message0 {Write-Host "Welcome "}#Display Hello message1{Write-Host "Hello"}#Display Hi message2{Write-host " Hi"}}-
This topic was modified 1 month, 3 weeks ago by
Mighty34.
-
This topic was modified 1 month, 3 weeks ago by
-
November 30, 2020 at 12:00 pm #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:
PowerShell12345678910111213while ( 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" }} -
November 30, 2020 at 12:25 pm #275385
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.
-
November 30, 2020 at 12:50 pm #275388
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>12345678910111213while ( 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” }}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.
-
-
AuthorPosts
- You must be logged in to reply to this topic.