Message Boards Message Boards

0
|
2747 Views
|
10 Replies
|
6 Total Likes
View groups...
Share
Share this post:

How to conditionally execute several lines of code?

Posted 2 years ago

I have a few lines of code in a cell which I only want to execute when a certain condition is true - I need something like this

A line of code here;
Another  line of code;
if myCondition == true then;
    first line of conditional code;
    ...
    last line of condtional code;
end if;
more lines of code follow;

Is this possible? I can only find reference to If[condtion,result if true,result if false] and I don't see how I could use that for what I want.

POSTED BY: Paul Newton
10 Replies
Posted 1 year ago

The issue here is the semantics of Equal. In the cancelled case, the expression fileName == $Canceled is comparing a symbol with a symbol (the same symbol) and so it can resolve to True. In the other case, it's comparing a symbol with a string, and that is indeterminate (according to the designed semantics of Equal anyway). So that If cannot be resolved because the condition cannot be resolved to either True or False.

There are several possible fixes. One is to use the 4-argument form of If:

If[
 fileName == $Canceled,
 fileName = "Canceled",
 fileName = "File has been found",
 fileName = "File has been found"]

In this form, the last argument is the one that gets executed if the condition doesn't resolve to either True or False.

Another fix is to use SameQ rather than Equal.

If[
 fileName === $Canceled,
 fileName = "Canceled",
 fileName = "File has been found"]

SameQ checks for structural identity, and so it always resolves to True/False.

You could also use Switch or Which instead of If.

POSTED BY: Eric Rimbey
Posted 1 year ago

That's great Eric. I'll go with SameQ (another one new to me) as it seems to be the neatest.

POSTED BY: Paul Newton
Posted 1 year ago

I must have lost the plot somewhere along the line ... I have this

fileName = SystemDialogInput["FileOpen",pFilename, WindowTitle -> StringJoin["Select the ",pFileDescrip," containing dissociation constants"]];
         If[fileName == $Canceled,fileName = "Canceled", fileName = "File has been found"];

When the dialog shows, if I click on "Cancel" or close the dialog without selecting a file, fileName is returned as "Canceled", as it should, BUT if I select a file and click on "Open" fileName is returned as the name and path of the file instead of "File has been found"/

POSTED BY: Paul Newton
Posted 2 years ago

Thank you - I can see that being very useful

POSTED BY: Paul Newton
Posted 2 years ago

Not sure whether the confusion was about how to combine multiple statements together or how to use If without an "else" branch. The answer I gave above explains the latter, but assumed an understanding of CompoundExpression which is how to achieve the former.

The semi-colon is just syntactic sugar for CompoundExpression. The comma is how to separate arguments. So, in an If expression, commas separate the branches and semi-colons will "chunk" pieces together into a single branch.

POSTED BY: Eric Rimbey
Posted 2 years ago

It (my confusion) was the former - how to combine multiple statements together. It just looks a bit alien when you are used to the if then .... else ... end if construct as in, for example, Visual Basic

POSTED BY: Paul Newton
Posted 2 years ago

I apologise - this turns out to be much easier than I realised:

If[ myCondition == true,
    first line of conditional code;
    ...
    last line of condtional code;]
POSTED BY: Paul Newton
Posted 2 years ago

Yes, you can use If for this.

If[
 True,
 Print["true 1"];
 Print["true 2"];
 Print["true 3"];,
 Print["false 1"];
 Print["false 2"];]

...should do the three "true" prints.

If[
 False,
 Print["true 1"];
 Print["true 2"];
 Print["true 3"];,
 Print["false 1"];
 Print["false 2"];]

...should do 2 "false" prints.

If you don't want the false branch at all you can do...

If[
 condition,
 TrueStuff[],
 Null]

The Null can be implicit:

If[
 condition,
 TrueStuff[],]

And though it's undocumented, you can leave out the non-true branch(es) entirely:

If[
 condition,
 TrueStuff[]]
POSTED BY: Eric Rimbey
Posted 2 years ago

As you can see, I managed to work that out before I saw your post - our posts obviously crossed in the mail!

POSTED BY: Paul Newton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract