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

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

A small note about for loop

AlvaroDíaz
9-Granite

A small note about for loop

Is usual not define the increment in the loop. This can carry some problems.

Alvaro.
10 REPLIES 10

Yes, Mathcad is very poor about handling end cases. In particular, it does not handle for loops that should execute zero times. In your example, the output loop should run only to n-1 to avoid a null inner loop.

Other similar issues are zero length vectors or substrings.
__________________
� � � � Tom Gutman

On 10/16/2009 4:10:06 PM, Tom_Gutman wrote:
>Yes, Mathcad is very poor
>about handling end cases. In
>particular, it does not handle
>for loops that should execute
>zero times.__________________
>� � � � Tom Gutman

We have the while loop for this case.

Val
http://twt.mpei.ac.ru/ochkov/v_ochkov.htm

I found the for loop behavior odd but predictable, at least as far as my limited testing goes. And Val's comment about the while loop is a good one to keep in mind...The for loop could be preceded by a test for bounds, but I think the while loop would be more intuitive. Both might benefit from a comment in the source about why that control statement was chosen, because someone not familiar with the problem (peculiar behavior) of Mathcad might change the source for their own use and be surprised when it didn't work as expected.

I once had to comment some PL/I code because it couldn't do A=B+C correctly! I had to use A=B, A=A+C. IBM acknowledge the bug, but their time twixt compiler releases was long enough to warrant the code mod and the comment.

Rich

http://www.downeastengineering.com/

...



jmG

The while loop can be used. But it requires additional separate statements to do the initialization and stepping. Sometimes a surrounding if (on the for statement) is necessary. Usually I find it best to simply arrange for that end case not to occur. The example starting this thread is typical, and is simply solved by using better loop limits (specifically, the limit on the outer loop).
__________________
� � � � Tom Gutman

The cycle rule is simple.
If I know how many time I will do the cycle body (2, 3, 4...) I use for cycle.
If I do not know how many time I will do the cycle body (0, 1, 2, 3, 4...) I use while (cycle in general form):
while 1
.
.
break (return ..) if ..
.
.
break (return ..) if ..
.
.

Val
http://twt.mpei.ac.ru/ochkov/v_ochkov.htm

In general, from a software engineering viewpoint (my background is largely in the area of software engineering at this point), I disagree with the casual use of break and goto type statements. In this case, I could see wrapping the 'for' loop with an 'if' test. [Actually, I prefer that PTC corrects Mathcad's aberrant behavior, and include an increment or step value option.] Most of the uses of 'break' that I have seen are easily rewritten to eliminate them, with greater clarity as the result. The goal here is to provide greater clarity for software verification and validation (which involves code reviews), and to ease the task of software maintenance in the future. When a Mathcad document or a program in [C++, PL/I, Pascal, etc.] is around for a decade or more, it is likely that someone other than the author must maintain the source. Whoever is tasked with that maintenance has to figure out what the author had in mind. Minimizing the use of 'break' statements, in general, helps to achieve that goal. Some languages, such as C/C++, require a 'break' statement in switch or case statements. I haven't done enough programming in Mathcad to offer a strong opinion against the regular use of 'break' in Mathcad programming loops, but I'd instinctively do my best to avoid it.

Rich
http://www.downeastengineering.com/

Thanks for the interest and comments, which clarify the options to make program techniques more robusts and clear in Mathcad. Some few notes.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
In Mathematica.

For[start, test, incr, body] executes start , then repeatedly evaluates body and incr until test fails to give True.

All parameters are mandatory. Don't looks pretty in text mode, the only one that I have to kernell access, and nor also in frontend, but works following "strick" rules.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
In Maple:

First syntax:

|for name | | from expr | | by expr | | to expr | | while expr | do statement sequence od;


Statment
> p := 0:
> for i from a to b do
> p := p+1;
> od: p;

gives 5 for a=1, b=5 and 0 for a=5, b=0

Also notice the nice inclusion of the while condition in the same line that for, very useful.

Second syntax:

|for name| |in expr| |while expr| do statement sequence od;

This looks actually more similar that what mathcad actually interpret the for loop. The expr is actually a maple object. Mathcad objects are ... very few: numbers, ranges or matrices.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
In MuPad.

Mathematica is only for completness, Maple for historical reasons, but given that MuPad is the Mathcad's kernel, could be more interesting. There are a lot of syntaxis

for i from start to stop do
body
end_for

for i from start to stop step stepwidth do
body
end_for

_for(i, start, stop, stepwidth, body)

for i from start downto stop do
body
end_for

for i from start downto stop step stepwidth do
body
end_for

_for_down(i, start, stop, stepwidth, body)

for x in object do
body
end_for

_for_in(x, object, body)

The underscores versions corresponds to the maple option to call this constructors as `for`( ... ). Note the inclution of the downto keyword (this is only a "to" with step negative).

There are not the beatifull while inline option.

The object constructor looks that is the same in Maple, and probably is the concept in Mathcad 14 to the for loop.

The above example in MuPad gives the same results as Maple answers, which diverges from the Mathcad considerations. I think that better to modify Mathcad answers could be the inclution of the some other avaible syntax.

Regards. Alvaro.

>I think that better to modify Mathcad answers could be the inclusion of the some other available syntax.< [Alvaro].
_________________________

The Mathcad programming structure does not come out of sparse odd/unrelated examples. There are lot of applied examples in circulation, then try some and comment/suggest ... for instance the TCF [Thiele Continued Fraction] just passed this week, give it a go in Maple/MuPad/Mathematica so that readers can evaluate your points.

You can even try the next attachment which is the "Spline Demo".

jmG

>I think that better to modify Mathcad answers could be the inclusion of the some other available syntax.< [Alvaro].
___________________________

Keep downloading what comes in the collab and try to figure out your better way. The Mathcad programming structure is a very powerful +, that if you have suggestion to dismantle ... better try to be more convincing with working examples than suggestive w/o examples. Try the B�zier in the "Spline Demo" just posted above.

jmG
Announcements

Top Tags