If/else with multiple variables in the condition

Hi guys,

I know that you may help me as I am quite new in programming and I am bit stuck with this.

Below as you can see, there is a sequence of variables that have the results of different checkbox. I would like to open the folder indicated if at least one checkbox is true, otherwise close all the windows (if all the checkbox are false). What I wrote doesn’t work as even if I leave all the tick boxes empty it opens always the folder.

Dialog.addCheckbox("A", false);
Dialog.addCheckbox("B", false);
Dialog.addCheckbox("C", false);
Dialog.addCheckbox("D", false);
Dialog.addCheckbox("E", false);
Dialog.show();  

a=Dialog.getCheckbox();
b=Dialog.getCheckbox();
c=Dialog.getCheckbox();
d=Dialog.getCheckbox();
e=Dialog.getCheckbox();

if (a&&b&&c&&d&&e==false) run("Close All");
else testimagesdir=getDirectory("Choose where Test_Images directory is");

Sorry if it looks bad! How do I do this?

Many thanks for you help!

Mike

There’s a </> button in the post editor that, when clicked, formats the selected text as code (by adding three backticks ``` before and after the selected text). If you want to specify the language for syntax highlighting, you can add it just after the leading backticks (e.g. javascript for IJ1 macros).

I just edited your post to fix the code formatting.

According to the macro language documentation, the == operator has a higher precedence than the && operator, so the above code is equivalent to:

if (a&&b&&c&&d&& (e==false) ) run("Close All");

(You can check this by selecting A,B,C,D but not E.)

You can get what you want by adding parentheses like this:

if ((a&&b&&c&&d&&e)==false) run("Close All");

BTW, you can get this a little easier without the Dialog.* functions, by using script parameters:

#@ Boolean (value=false) a
#@ Boolean (value=false) b
#@ Boolean (value=false) c
#@ Boolean (value=false) d
#@ Boolean (value=false) e

if ((a&&b&&c&&d&&e)==false) print("yes");
else print("no");

Many thanks Jan for your tips.
Unfortunately either when I run this:

if ((a&&b&&c&&d&&e)==false) run("Close All");

or what you suggested:

#@ Boolean (value=false) a
#@ Boolean (value=false) b
#@ Boolean (value=false) c
#@ Boolean (value=false) d
#@ Boolean (value=false) e

if ((a&&b&&c&&d&&e)==false) print("yes");
else print("no");

I don’t get the else statement.

Do you know why?

Cheers!

The way you wrote it , the else statement will be executed only if all the variables are set to true. Probably you want to use the OR condition

if ((a||b||c||d||e)==true) testimagesdir=getDirectory("Choose where Test_Images directory is");
else run("Close All");
1 Like

Many thanks Giovanni! That’s working now!
Cheers!