Skip to main content
1-Visitor
January 6, 2016
Question

Exiting out of For Loops

  • January 6, 2016
  • 2 replies
  • 2715 views

In the attached Mathcad 15 document I have a for loop that defines certain cases. In this example the equality is satisfied for case 5 and case 6 but the loop is returning the last equality that is satisfied and I need to have it start the loop at case 1 and exit the loop after the first equality is satisfied. Any help is greatly appreciated. Thanks.

2 replies

23-Emerald IV
January 6, 2016

See if it helps to add a break in the if statements, like this:

Success!

Luc

Heavenly1-VisitorAuthor
1-Visitor
January 6, 2016

Luc,

How do I define an if statement without a variable definition in front of it?

Dave

23-Emerald IV
January 6, 2016

Use the if function?

if ( {condition} , {statement if true} , {statement if false} )

Or is it that you wonder how the constructions I showed above are made...?

Put your cursor on the statement / placeholder in front of the if

  and add a program line, either from the menu or using the "]" key.

Success!
Luc

25-Diamond I
January 6, 2016

You can't use return, as Valery suggested, as this will end the whole calculation and you won't get the vector you need.

Using break seems to cancel the for-loop completely

So I guess its "continue" which you have to use.

Another option would be to nest the if's using "otherwise".

The way you described your problem in your question could be interpreted as if you just one one single value to be returned - cancelling the whole routine after the first match weas found. If thats the case,  "break" and "return" should do the job and I would favor using return.

But in your sheet you create a vector of values, so I assumed that this is what you are after.

R

1-Visitor
January 6, 2016

You can use "return" if you remove the loop from the program and set up the problem a little differently: