cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

If loop with a range variable

ksk_10594232
9-Granite

If loop with a range variable

Hi

 

How do I program an if-loop with a range variable, and not a scalar? Is that possible?

 

1 ACCEPTED SOLUTION

Accepted Solutions

I found a solution to get around the problem. Thanks anyways!

View solution in original post

11 REPLIES 11

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:

Werner_E_1-1695651361000.png

 

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.

Werner_E_0-1695665056201.png

Or you do completely without any "if" and use the "min" function, which is also available in Prime Express

Werner_E_1-1695665654487.png

 

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? 

 

ksk_10594232_0-1695990315847.png

 

You again turn the calculation into a function and call it vectorized with a vector (not a range!) as argument.

Werner_E_0-1695991343597.png

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:

Werner_E_1-1695991657170.png

 

 

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

Top Tags