SlackerAl Posted December 19, 2017 Share Posted December 19, 2017 I noticed this behaviour and it did not seem very intuitive to me: This script will generate an error @ line 4 Error: "Else" statement with no matching "If" statement Global $iTest = 1 If $iTest = 1 Then MsgBox(0, "Debug", "Test was true") ElseIf $iTest = 0 Then MsgBox(0, "Debug", "Test was false") Else MsgBox(0, "Debug", "Test was naughty") EndIf This script runs without error Global $iTest = 1 If $iTest = 1 Then MsgBox(0, "Debug", "Test was true") ElseIf $iTest = 0 Then MsgBox(0, "Debug", "Test was false") Else MsgBox(0, "Debug", "Test was naughty") EndIf So after an If conditional, Then must be followed by a newline, if the conditional statement is to be followed by an ElseIf. But after an ElseIf conditional, Then does not need to be followed by a newline, even if an Else follows its conditional This script also runs without error: Global $iTest = 1 If $iTest = 1 Then MsgBox(0, "Debug", "Test was true") ElseIf $iTest = 0 Then MsgBox(0, "Debug", "Test was false") ElseIf $iTest = 5 Then MsgBox(0, "Debug", "Test was crazy") Else MsgBox(0, "Debug", "Test was naughty") EndIf But this will cause an error: Global $iTest = 1 If $iTest = 1 Then MsgBox(0, "Debug", "Test was true") ElseIf $iTest = 0 Then MsgBox(0, "Debug", "Test was false") ElseIf $iTest = 5 Then MsgBox(0, "Debug", "Test was crazy") Else MsgBox(0, "Debug", "Test was naughty") EndIf Else needs a new line before its statement. For me it is a little surprising that I must have a newline after the first Then, if I am going to have ElseIf's following it. Perhaps it would be useful to remove the dependence on the newline? Problem solving step 1: Write a simple, self-contained, running, replicator of your problem. Link to comment Share on other sites More sharing options...
Earthshine Posted December 19, 2017 Share Posted December 19, 2017 Well. I never put an if on one line. Just habit and looks better My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
Developers Jos Posted December 19, 2017 Developers Share Posted December 19, 2017 It is pretty strait forward either use a single line If...Then statement or else you can use the multiline - If-ElseIf-Else option. Don't try to mix them. Jos Earthshine 1 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now