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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Struggling to write if/else statements with variable ranges

AS_10119570
1-Newbie

Struggling to write if/else statements with variable ranges

 

 

I am looking to write some code for a structural connection design template. One of the things that needs to be checked is plate geometry. More specifically the distance from the centerline of a bolt hole to the edge of the plate the hole is in. The table below outlines the general parameters I am trying to meet. 

AS_10119570_0-1703082611432.png

lets call the bolt diameter "d" and the min edge distance "x",

for 0.5 <= d <= 1

if x >= d + 0.25 then return "meets spec". if not then return "violates spec"

 

for 1 < d <= 1.25

if x >= d + 0.375 then return "meets spec". if not then return "violates spec"

 

for d > 1.25

if x >= d * 1.25 then return "meets spec". if not then return "violates spec"

 

Basically, I want to put in various values for "d" and "x" and have it tell me whether or not I meet the spec per the rules above. I feel like this should be rather simple to code in but I have no coding experience so I am struggling hard with it.

 

AS_10119570_0-1703087835526.png

Above is a snippet of the code I have tried to write for it (note that "x" = "lev" in the actual code

1 ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:AS_10119570)

The other construct, is also supported by Prime 9.

To code an IF you can use the programmatic IF, that you used in you program.

Terry used the IF function which is defined as:

 if ( <boolean> , <value if true> , <value if false> )

Example:

b := if ( x > 0 , 5 , 3 )

Assign to b the value 5 if x >0, and 3 otherwise.

 

To your program:

Your first test check if d is within 0.5 to 1 and if that's the case, then you have other tests, such as whether d is between 1 and 1.25. Well, the program only gets to that test if d is between 0.5 and 1, so the test for d between 1 and 1.25 always returns false.

The else statement at the end returns false for all values of d outside the range from 0.5 to 1...

 

Success
Luc

View solution in original post

5 REPLIES 5

Capture.JPG

Hello, thanks for the help, I am using mathcad prime 9.0.0.0 so the visual of the syntax is different:

AS_10119570_0-1703089047913.png

I have tried to match what you had coded, it seems to work for values of 0.5 < d < 1 but anything over 1 it doesnt work as seen in my snippet where it should say "meets spec" when I input a value of 1.5 for d and 1.875 for lev

MartinHanak_0-1703093251461.png

 


Martin Hanák
LucMeekes
23-Emerald III
(To:MartinHanak)

For a single range, you don't need an AND construct,

 

Instead of :      0.5 < d AND d < 1

you can use:   0.5 < d < 1

 

Success!
Luc

LucMeekes
23-Emerald III
(To:AS_10119570)

The other construct, is also supported by Prime 9.

To code an IF you can use the programmatic IF, that you used in you program.

Terry used the IF function which is defined as:

 if ( <boolean> , <value if true> , <value if false> )

Example:

b := if ( x > 0 , 5 , 3 )

Assign to b the value 5 if x >0, and 3 otherwise.

 

To your program:

Your first test check if d is within 0.5 to 1 and if that's the case, then you have other tests, such as whether d is between 1 and 1.25. Well, the program only gets to that test if d is between 0.5 and 1, so the test for d between 1 and 1.25 always returns false.

The else statement at the end returns false for all values of d outside the range from 0.5 to 1...

 

Success
Luc

Top Tags