Skip to main content
15-Moonstone
April 20, 2019
Question

If Exist or If Not Exist

  • April 20, 2019
  • 4 replies
  • 3378 views

I am writing a program and would like to use an if statement  with the following format

 

if X exist

if X does not exist

 

Running Prime 5. I did not see this as an option in the help files.

Mathcad 15 does not appear to have the feature either

 

David

4 replies

tietjee15-MoonstoneAuthor
15-Moonstone
April 21, 2019

Solved my own question.  It is not as clean as other programming languages.  Attached is my solution, which will work for my application.

23-Emerald I
April 21, 2019

Mathcad has a (non-programming, so it works even in Express) if statement like the one in EXCEL:

 

if(boolean test, result if true, result if false)

24-Ruby IV
April 21, 2019

Try please the try operator!

25-Diamond I
April 21, 2019

As you already noticed there is unfortunately no way to check if a variable is already defined.
You would have to write a program which saves the worksheet and scans the xml-file in the saved archive (a mcdx-file is just a renamed zip archive) for the existence of a variable definition.

Also Valerys suggestion does not help as Prime would reject the call to the function if you provide an undefined variable as argument and the function so would be never called.

But if just checking if a (defined) variable has the "value" NaN is enough, your solution can be simplified a little bit by omitting the local variable y. Furthermore you could follow Freds advice to use the if-function instead of the programming if-statement. This has the benefit that the routine will also work in the limited Prime Express. The definition will be shorter that way, but also a little bit less clear.

B.png

tietjee15-MoonstoneAuthor
15-Moonstone
April 21, 2019

Thank you everybody for the support.  I have made the change to the code snippet,

 

The decision is made in a program which is represented by "Variable Not..."and "Variable..."  Each one will replaced by several lines of code.

Again,  Thank you