Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
Hello! I am new to Mathcad Prime. I would like to ask You for a help. My problem is as below:
The task is to write the function using "also if" condition. I don't understand why, when I assign to variable "x" sth different than text string i get no result (should be 3 in my opinion). The function works perfectly with strings, i mean if I assigna to "x" string shorter than 5 i get result 1, when I assign value larger than 5 I get 2. Just that statement "else" doesn't want to work. Thank You for all advices in advance!
Please attach your worksheet - choose "use advanced editor" at the upper right to do so.
What is the error message?
The problem seems to be that you use y as selector but you change that value in the conditional branch. You may try using a different variable for the return value or replace "y <-- 1" for "return 1", etc.
Error message: "This value must be a string". In the exercise is written to use local variable "y" for these 3 conditions.
So the problem is the "also if" part. It is (look at the help files) evaluated regardless of the result of the forgoing if (in contrary to else if). So the routine tries to evaluate strlen(x) even if x is no string and that throws the error.
Are you sure you are forced to use "also if"? I see no need and no use for an "also if" here.
I would do it that way:
Well maybe there is a bug in tutorial question.In the Note below the task is written that alsoif is evaluated when the if statement is true. On the other hand in help (conditional operators) this happens regardless of whether the preceding if or alsoif statements are zero (false) or nonzero (true). Thank you for answers!
Now I see where this task comes from.
While its not wrong what is written here, it should have been worded differently as its incomplete.
To the author it obviously goes without saying that any "also if" is evaluated when the foregoing "if" is false and so they stated only that it will (also) be evaluated when that "if" is true - thus highlighting the difference to "else if". The Prime help/reference is correctly formulated in that respect.
Nevertheless I think its a very badly chosen task for an "also if". I see no reasonable way to throw in an "also if".
Your logic was correct and if strlen(x) would return a 0 or a NaN in case of a non-string argument, it would work, but unfortunately strlen throws an error.
I guess the authors of that tutorial didn't solve their tasks themselves and were not aware of the strlen()-error. But maybe they really thought that "also if" does not evaluate when the foregoing "if" is false.
Find attached your sheet with your routines untouched, I simple added a strlen() routine which does not throw an error in case of an improper arguement but return NaN ("Not a Number").