Jump to content
leuce

Correct syntax of "If NOT"

Recommended Posts

leuce

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
j0kky

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
leuce

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
mikell

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
Jos
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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
iamtheky

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
water

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 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
mikell

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
iamtheky

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

  • Like 1

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

Share this post


Link to post
Share on other sites
mikell

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.

:)

  • Like 3

Share this post


Link to post
Share on other sites
kylomas

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
leuce
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
leuce
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
iamtheky

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

×