leuce

Correct syntax of "If NOT"

17 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Alternative for the nested IFs   :)

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

If NOT StringRegExp($selectoption, '^[1234]$') Then _ 
    Exit MsgBox (0, "Can't read option", "Option should be 1, 2, 3 or 4", 0)
MsgBox (0, "", "it worked !", 0)

 

Share this post


Link to post
Share on other sites
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 


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

iamtheky,
Your regex example doesn't work for me
Looks like a typo, but if you meant 1\z etc then it allows 51 so it's less precise than  '^[1234]$'  ;)

Share this post


Link to post
Share on other sites

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

1 person likes this

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

Share this post


Link to post
Share on other sites

Did you say 'problems' ?

Quote

Some people, when confronted with a problem, think
“I know, I'll use regular expressions.”
Now they have two problems.

:)

3 people like this

Share this post


Link to post
Share on other sites

mikell,

You made my night!


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
19 hours ago, Jos said:

Are you sure about that? Strait from the helpfile:

Jos 

Missed that, sorry.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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


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

Share this post


Link to post
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