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

Also if

ValeryOchkov
24-Ruby IV

Also if

Can you show me please, when we must use also if not else if. Must not can!

ACCEPTED SOLUTION

Accepted Solutions

With flag we can work without if, else, else if, also if, otherwise, then etc....

View solution in original post

7 REPLIES 7
LucMeekes
23-Emerald III
(To:ValeryOchkov)

O...K... you say 'also if', but I can also imagine an 'if also'.

for the case of 'also if', i'd say this is when you have a situation where you need a certain condition, but there's another condition that will also do; the equivalent would be an OR in the test of the IF statement:

if a=3 then

  x<=1

also if b=5 then

  x<=1

end if

Is equivalent to:

if (a=3) OR (b=5) then

  x<=1

end if

The 'if also' case occurs when you need a certain condition, but when at the same time another condition also applies, then something else might happen; the equivalent would be a nested IF statement:

if a=3 then

  x<=1

if also b=5 then

  x<=2

end if

is equivalent to:

if (a=3) then

  x<=1

  if (b=5) then

    x<=2

  end if

end if

Success!

Luc

In the sequence

if condition1

     do statement1

else if condition2

     do statement2

else if condition3

     do statement3

else

     do statement4

Mathcad Prime will exit the block of conditional statements as soon as a condition is met. That may not be what is wanted though. In this sequence

if condition1

     do statement1

also if condition2

     do statement2

else if condition3

     do statement3

else

     do statement4

condition2 will be evaluated even if condition1 was true, and if condition2 is also true then statement2 will be executed.

For example:

Mathcad 15 is Mathcad 15!

I never seen also if in one real calculation!

I never seen also if in one real calculation!

It's a shame you marked your answer correct, because it's actually wrong. The first condition is tested and found to be true, and the associated statements are executed. The second condition is also tested, found to be true, and the associated statements are executed. The otherwise is associated with both if statements, and the associated statements will only be executed if neither if statement is true. The second if statement therefore is in fact an "also if". You say you have never seen one in a real calculation, but actually you have written a great many of them yourself! You can turn the "also if" statement into an if statement by putting any other statement between the two if statements, even a dead line of text as a comment:

This splits everything into two separate conditional blocks, and the otherwise is associated only with the second if statement.

This behavior in Mathcad 15 is not desirable:

1) It's not clear what two consecutive if statements do, even to someone reasonably familiar with Mathcad. Is the second if statement independent of the first one, or does it execute as an "else if" or an "also if" statement? Is the otherwise associated with both if statements or only the last one? Most people would guess either that it's independent (after all, it does not say "else if" or "also if", so why assume it is either of them?), or that it's an "else if", because that is common in many programming languages. Either way, they would be wrong, because it's an "also if".

2) If what you actually want is an "else if" statement, as is available in many programming languages, hard luck, because there isn't one! The easiest way to create one is probably to set a flag in each if statement if the condition is true, and then test for that flag with an "and" in all subsequent if statements. That's kind of a pain though, to get what is a very useful (and fast) structure: exit the conditional structure after the first conditional statement is true and the associated statements have been executed.

That's why Prime has "else if" and "also if" statements: to fix the confusing and functionally deficient conditional statements of Mathcad 15 (that's not just speculation; I was involved in the discussion back in the early days of Prime). It's one of the few places where Prime is better than Mathcad 15.

With flag we can work without if, else, else if, also if, otherwise, then etc....

Yes, we can work around it. That doesn't make it good. The fact is, the syntax in Mathcad 15 is not clear and lacks some basic functionality. The workaround you show is even less clear. I could equally well argue that if we were given a "goto" statement then we would no longer need "for" or "while" loops, and we could all go back to the days of spaghetti code.

Announcements

Top Tags