Page 1 of 1

If-then-else syntax issue

Posted: 19 Sep 2016 13:25
by Econdoc
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.

Re: If-then-else syntax issue

Posted: 19 Sep 2016 19:10
by MURTUMA
Try this:
If NOT (ischecked(“ON”))
{do your thing};

Re: If-then-else syntax issue

Posted: 20 Sep 2016 00:13
by Econdoc
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.

Re: If-then-else syntax issue

Posted: 20 Sep 2016 19:13
by Martin
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

Re: If-then-else syntax issue

Posted: 21 Sep 2016 09:21
by MURTUMA
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.