Jump to content

Syntax thoughts: If Then ElseIf


 Share

Recommended Posts

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

  • Developers

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

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

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...