Jump to content

Correct syntax of "If NOT"


leuce
 Share

Recommended Posts

Hello everyone

In this script:

$selectoption = InputBox ("Select option", "Type 1, 2, 4 or 4", "5")

If NOT $selectoption = "1" Then
If NOT $selectoption = "2" Then
If NOT $selectoption = "3" Then
If NOT $selectoption = "4" Then
MsgBox (0, "Can't read option", "Option should be 1, 2, 3 or 4", 0)
Exit
EndIf
EndIf
EndIf
EndIf

MsgBox (0, "", "Grrr, it didn't work!", 0)

I expect the script to exit at the first MsgBox, if the $selectoption is anything other than 1, 2, 3 or 4 (e.g. if it is "5" or "hello").  But no, the script gives me the second MsgBox.  What am I doing wrong?

Thanks

Samuel

 

Edited by leuce
Link to comment
Share on other sites

you lost parentesis, the correct syntax is:

If Not (variable1 = variable2) Then

Instead, if you write:

If Not variable1 = variable2 Then

is equal to:

If (Not variable1) = variable2 Then

Edited by j0kky
Link to comment
Share on other sites

  • Developers
1 hour ago, leuce said:

Thanks, I never knew that I had to use brackts.  It doesn't say so in the help files. :-(

Are you sure about that? Strait from the helpfile:

Quote

Remarks

This version of the If statement is used to execute a single statement without the overhead of an EndIf.
The expression can contain the boolean operators of And, Or, and Not as well as the logical operators <, <=, >, >=, =, ==, and <> grouped with parentheses as needed.

Related

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Why do you use an invalid value as default for the InputBox?

$selectoption = Number(InputBox ("Select option", "Type 1, 2, 3 or 4"))
If $selectoption < 1 Or $selectoption > 4 Then MsgBox (0, "Invalid selection", "Option should be 1, 2, 3 or 4")

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

number like that leaves edge cases for days because of the way it behaves with mixed strings, I would go very literal if you only have 4 options

 

$selectoption = number(InputBox ("Select option", "Type 1, 2, 3 or 4" , "3hello"))
If $selectoption < 1 Or $selectoption > 4 Then MsgBox (0, "Invalid selection", "Option should be 1, 2, 3 or 4")
   
$selectoption = InputBox ("Select option", "Type 1, 2, 3 or 4" , "3hello")
If stringregexp($selectoption , "1/z|2/z|3/z|4/z") = 0 Then MsgBox (0, "Invalid selection", "Option should be 1, 2, 3 or 4")

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Enhanced version ;)

$sSelectOption = InputBox("Select option", "Type 1, 2, 3 or 4")
$iSelectOption = Number($sSelectOption)
If StringLen($sSelectOption) <> 1 Or ($iSelectOption < 1 Or $iSelectOption > 4) Then MsgBox(0, "Invalid selection", "Option should be 1, 2, 3 or 4")

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

"Pointing out other people's problems with something that has lots of problems itself".  Is my new slogan.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

19 hours ago, water said:

Why do you use an invalid value as default for the InputBox?

$selectoption = Number(InputBox ("Select option", "Type 1, 2, 3 or 4"))
If $selectoption < 1 Or $selectoption > 4 Then MsgBox (0, "Invalid selection", "Option should be 1, 2, 3 or 4")

 

For testing purposes.  The final script will have a valid default value.

Link to comment
Share on other sites

combobox would prevent all the additional fun we can have making inputbox do tricks.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...