Thread Subject: How to jump in a for loop?

Subject: How to jump in a for loop?

From: Prediger

Date: 9 Feb, 2010 11:45:47

Message: 1 of 3

Dear All,

I have a for cycle (such as: for j=31:-1:1) and I have a condition within the loop. If the condition is true I'd like to jump in the loop and leave out some part of the cycle. For example j runs from 31-20 than the condition becomes true therefore j runs from 15 towards the end or jumps back to 24.
I can't figure out how to modify the value of j within the cycle.

Thx

Prediger

Subject: How to jump in a for loop?

From: John D'Errico

Date: 9 Feb, 2010 12:14:03

Message: 2 of 3

Prediger <prediger@freemail.hu> wrote in message <249999763.136201.1265715977098.JavaMail.root@gallium.mathforum.org>...
> Dear All,
>
> I have a for cycle (such as: for j=31:-1:1) and I have a condition within the loop. If the condition is true I'd like to jump in the loop and leave out some part of the cycle. For example j runs from 31-20 than the condition becomes true therefore j runs from 15 towards the end or jumps back to 24.
> I can't figure out how to modify the value of j within the cycle.

You cannot change the loop counter inside the
loop, or the limits on the for.

So use a while loop instead, where YOU increment
the loop counter.

Or, use an if statement inside the loop, only
processing those iterations where the loop counter
is known to be appropriate.

Or you can use the if to break out of the loop if
you are known to be done, or, use a continue
to skip to the next iteration.

HTH,
John

Subject: How to jump in a for loop?

From: Prediger

Date: 10 Feb, 2010 11:43:30

Message: 3 of 3

Thanks John!

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com