Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X
Hi
How do I program an if-loop with a range variable, and not a scalar? Is that possible?
Solved! Go to Solution.
I found a solution to get around the problem. Thanks anyways!
The "if" statement is a branch, not a loop!
Its unclear to me what you mean and how this would have to do with a range variable.
Maybe you can give a couple of examples of what you are looking for.
If you are looking for multiple branching like the "switch" statement in C - Prime does not provide a comparable statement.
But depending on the specific problem its often possible to find a simpler solution compared to a cascade of if's and elseif's.
I have attached an example which I hope will clearify my question 🙂
And what kind of data structure do you expect as the output?
You have to be aware that ranges are not vectors! Ranges should only be used on three occasions:
To index the elements of a vector or matrix
In a for-loop inside a program
As abscissa values when you plot a function
In all other cases you should use vectors.
Maybe this is what you are looking for:
Prime 9 file attached
I found a solution to get around the problem. Thanks anyways!
Hi,
Here is solution that does not use programmed if statements suitable for Express version of Prime.
Cheers
Terry
If you are looking for a version for Express, it would be easier to use the if-function instead of the programmed if-statement once x.test is defined as a vector and not a range.
Or you do completely without any "if" and use the "min" function, which is also available in Prime Express
Using functions has various advantages over manipulating vectors throughout (which is a way of working many are used from using a spreadsheet program).
Thanks ! 🙂
How can I use the same approach for an else/if statement as shown in the attached file?
You again turn the calculation into a function and call it vectorized with a vector (not a range!) as argument.
Or are you looking for a function which also works in Prime Express where programming is not available?
The you would use nested if-functions, similar to what you have to do in any spreadsheet program like Calc ode Excel - but this tends to look cluttered and messy:
I understand, but I don't know how to write the above calculation into a function.
My bad, I noticed that to wrote above 😄 Thank you