If-then-else syntax issue

Post your questions and help other users.

Moderator: Martin

Post Reply
Econdoc
Posts: 153
Joined: 28 May 2016 20:06

If-then-else syntax issue

Post by Econdoc » 19 Sep 2016 13:25

I am having some confusion about if () { } else { } structures.
Sometimes it is very awkward to set up an “if” condition where the false condition is of interest, not the true.
The first snippet below works correctly, but is awkward to create (and awkward to interpret three months after coding it!)
SNIPPET 1:
If (ischecked(“ON”) == false)
{
Do Something
}


I think that the snippet below will be clearer to interpret three months after coding, but it does not appear to work.
SNIPPET 2:
If (ischecked(“ON”))
{
}
Else
{
Do something
}

The empty first set of braces creates an error. [Expression expected [82]]. The snippet works if some nonsense is inserted, such as “a=1”.

Am I missing something? It is common in other programming languages to have empty expressions inside an if-then-else structure.

User avatar
MURTUMA
Posts: 697
Joined: 05 Mar 2013 22:43

Re: If-then-else syntax issue

Post by MURTUMA » 19 Sep 2016 19:10

Try this:
If NOT (ischecked(“ON”))
{do your thing};

Econdoc
Posts: 153
Joined: 28 May 2016 20:06

Re: If-then-else syntax issue

Post by Econdoc » 20 Sep 2016 00:13

Nice idea, but, sadly, it does not work! :oops:
It generates an error.

Just to be clear, the work-around that I wrote works. My issue was why the { do Nothing} creates an error. No reason for that to happen that I can think of.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: If-then-else syntax issue

Post by Martin » 20 Sep 2016 19:13

Hi,

The NOT respectively ! belongs within the parenthesis. Following should work:

Code: Select all

if (!isChecked("ON"))
{
...
}
or

Code: Select all

if (NOT isChecked("ON"))
{
...
}
This limitation of the empty block comes from the fact that each block in Automagic has to evaluate to a value (last evaluated value of the block). I could artificially let an empty block evaluate to false or something like that but I'm not sure if this could lead to other problems.

You could also write:

Code: Select all

if (...)
{
  false;
}
else
{
 do something...
}
Regards,
Martin

User avatar
MURTUMA
Posts: 697
Joined: 05 Mar 2013 22:43

Re: If-then-else syntax issue

Post by MURTUMA » 21 Sep 2016 09:21

Martin wrote:if (NOT isChecked("ON"))
{
...
}
Yes,this was what I meant. I couldn't find the right syntax from the documentation so I wrote how I remembered it. Sorry about that.

Post Reply