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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Nested if Statements

BTram
1-Visitor

Nested if Statements

I need to do some nested if statements for conditional programming and I cannot figure out for the life of me how to nest if-statements using the programming toolbar. I am in Matcad 14 and the "add line" button just adds if-else if statements, I need to nest. All the "add line" option does is just add another line on the same logic level. Is there a difference between if and conditional if?

I posted a flowchart of the basic program logic. (Ignore the red lines, that is just my spell checker)

flowchart.png

ACCEPTED SOLUTION

Accepted Solutions
RichardJ
19-Tanzanite
(To:BTram)

When you enter the if there are two placeholders, one before the if (where the statement to be executed goes) and one after the if (where the condition goes). The cursor should be on the on before the if, but if it isn't then select that placeholder. Add a line using the programming toolbar, and the structure of the if statement will change so that you have a block of statements below the if. Now you can add another if as one of those statements. As long as you always have more then one line in the statement block the structure of the nested if is clear. It's not actually necessary to have more than one line, but the structure is impossible to figure out if you don't (as you have discovered!).

View solution in original post

17 REPLIES 17
RichardJ
19-Tanzanite
(To:BTram)

It's very hard to figure out the structure of nested if statements if they only have one line in the statement block. After adding an if, add a second line to the block. Then add the next if, and so on.

BTram
1-Visitor
(To:RichardJ)

So I can't do something like this?

nestedif.png

I found this through google in a Mathcad primer PDF. The problem is I physically can't figure out how the keystokes to get just "if .......". The only if statement I can find is through the programming logic which is "......... if .............", and even then I can't figure out how to nest them. Thanks.

RichardJ
19-Tanzanite
(To:BTram)

When you enter the if there are two placeholders, one before the if (where the statement to be executed goes) and one after the if (where the condition goes). The cursor should be on the on before the if, but if it isn't then select that placeholder. Add a line using the programming toolbar, and the structure of the if statement will change so that you have a block of statements below the if. Now you can add another if as one of those statements. As long as you always have more then one line in the statement block the structure of the nested if is clear. It's not actually necessary to have more than one line, but the structure is impossible to figure out if you don't (as you have discovered!).

BTram
1-Visitor
(To:RichardJ)

Thanks! I'll mess around with it but it was the keystrokes that were getting me not the program logic!

EDIT: Got it working, I posted a screen shot if anyone else runs into a similar problem. To add extra lines click on the box before the if statement and the otherwise statement. The black lines act like brackets in text based programming

nestedif.png

Message was edited by: Brad Trammell (mathcad file with solution)

That function could be cleaned up a lot.

Can you post the worksheet?

Mike

Sure, ignore the other stuff around it. I can't imagine the way I did it is the best but it is functional for the time being.

Sure, ignore the other stuff around it. I can't imagine the way I did it is the best but it is functional for the time being.

There seems to be something fundamentally wrong with your function.

  • What happens if Cr is 0.01?

The function falls over.

Mike

Gotcha another poster pointed out the same thing. The chances of me hitting 0.01 exactly were close to zero so brute force debugging fail

Gotcha another poster pointed out the same thing. The chances of me hitting 0.01 exactly were close to zero so brute force debugging fail

Remember if there any chance at all it's good practice to program for it.

Mike

RichardJ
19-Tanzanite
(To:BTram)

One observation: the program does not cover the case for Cr=0.01

BTram
1-Visitor
(To:RichardJ)

does the last line not get read by the program?

1-exp(-NTUtotal) if Cr < 0.01

should I change that to an otherwise?

RichardJ
19-Tanzanite
(To:BTram)

If Cr>0.01 the first block is executed, if Cr<0.01 the second block is executed. If Cr=0.01 neither block is executed. How you deal with this depends on what you want. If you want the first block to execute when Cr=0.01 change the greater than to a greater then or equal to. If you want the second block to execute when Cr=0.01 you can either change the less than to a less than or equal to, or change it to an otherwise.

BTram
1-Visitor
(To:RichardJ)

ahhh, you are correct. I misread your statement I need to change that to a less than or equal to.

I have just restructed. Looks a little more traqnsparent now (IMO)

Clipboard01.jpg

Mike

Good call, one less if statement cleans it up. Thanks for the help!

Brad Trammell wrote:

Good call, one less if statement cleans it up. Thanks for the help!

No problem at all.

Remember try and make the programs easy to follow. When I write anything, I aim to keep it as simple as possible, so if a non-Mathcad user wants to check my work, it's still possible.

Mike

Yes.

1-exp(-NTU(1-Cr)) / 1-Cr*(exp(-NTU*(1-Cr)))

converges to

NTU/(1+NTU)

as Cr approaches 1. I failed to see that in the original logic (but it is a good thing it is true or i would have a discontinuity in my solution)

Announcements

Top Tags