Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Terminating a for loop before

Subject: Terminating a for loop before

From: Diego Zegarra

Date: 12 Feb, 2009 03:06:01

Message: 1 of 7

I have a for loop and within the for loop i have an if statement where if something is true then i want to get out of the for loop. I am trying to do it the following way but it is not working,

R=10
div = 0.5
for s=1:R
     ...
     ...
     if (div_sum/numJobs) < div
            cef = 0;
             s = R+1;
     else
            cef = 1;
     end
     ...
end

The thing is suppose that s in the for loop is 4 and it get that cef =0, then I do not want to keep checking for s=5,6,7,8,9,10. I just want to exit the for loop. That is why I put that s=R+1 but it is not working. You can try that with a simple example.

Is there any possible way that this can be done since the most logical way does not work? I know break or return do not work because there would not be a way to put it inside the for loop only.

And by the way div_sum and numJobs or any other variables are all specified in the code, its just that part that is not doing what it is supposed.

Thanks for any help that is given, it is greatly appreciated!

Subject: Terminating a for loop before

From: Matt Fig

Date: 12 Feb, 2009 03:50:03

Message: 2 of 7

You cannot modify a loop index inside a loop. Look at the help for: break.




`czd\ztzd\^cijTjpoqggizH]bc)\z\zko\]"h5j;k^pj"jh`z\joz@`\f`

Subject: Terminating a for loop before

From: Husam Aldahiyat

Date: 12 Feb, 2009 03:57:01

Message: 3 of 7

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gn03mp$kuq$1@fred.mathworks.com>...
> I have a for loop and within the for loop i have an if statement where if something is true then i want to get out of the for loop. I am trying to do it the following way but it is not working,
>
> R=10
> div = 0.5
> for s=1:R
> ...
> ...
> if (div_sum/numJobs) < div
> cef = 0;
> s = R+1;
> else
> cef = 1;
> end
> ...
> end
>
> The thing is suppose that s in the for loop is 4 and it get that cef =0, then I do not want to keep checking for s=5,6,7,8,9,10. I just want to exit the for loop. That is why I put that s=R+1 but it is not working. You can try that with a simple example.
>
> Is there any possible way that this can be done since the most logical way does not work? I know break or return do not work because there would not be a way to put it inside the for loop only.
>
> And by the way div_sum and numJobs or any other variables are all specified in the code, its just that part that is not doing what it is supposed.
>
> Thanks for any help that is given, it is greatly appreciated!

replace s=R+1 with BREAK.

Subject: Terminating a for loop before

From: Diego Zegarra

Date: 12 Feb, 2009 04:02:02

Message: 4 of 7

"Matt Fig" <spamanon@yahoo.com> wrote in message <gn069b$fqt$1@fred.mathworks.com>...
> You cannot modify a loop index inside a loop. Look at the help for: break.
>
>
>
>
> `czd\ztzd\^cijTjpoqggizH]bc)\z\zko\]"h5j;k^pj"jh`z\joz@`\f`

break would only get me out of the if statement but not the for loop...

Subject: Terminating a for loop before

From: Divya satsangi

Date: 12 Feb, 2009 04:02:02

Message: 5 of 7

"Matt Fig" <spamanon@yahoo.com> wrote in message <gn069b$fqt$1@fred.mathworks.com>...
> You cannot modify a loop index inside a loop. Look at the help for: break.
>
>
>
>
> `czd\ztzd\^cijTjpoqggizH]bc)\z\zko\]"h5j;k^pj"jh`z\joz@`\f`
Hi
if you wish to go out of the loop u can use continue statement or a break statement. What actually these statement do u can check it out in matlab help and can use any of these which satisfy ur problem statement

Subject: Terminating a for loop before

From: Matt Fig

Date: 12 Feb, 2009 04:12:02

Message: 6 of 7

"Diego Zegarra" <diegozbb@gmail.com> wrote in message
> break would only get me out of the if statement but not the for loop...

Shall I quote the help for you since you did not take my advice to read it?


>> help break
 BREAK Terminate execution of WHILE or FOR loop.
    BREAK terminates the execution of FOR and WHILE loops.
    In nested loops, BREAK exits from the innermost loop only.

I don't see if-statements mentioned anywhere in that help text.




^^NF@DN$TLNBKMKDNGS@D8NBT,G@SO^^AeMLXl^@@ODHSHx~A^J@G@U^^Ne

Subject: Terminating a for loop before

From: Steven Lord

Date: 12 Feb, 2009 14:45:34

Message: 7 of 7


"Diego Zegarra" <diegozbb@gmail.com> wrote in message
news:gn03mp$kuq$1@fred.mathworks.com...
>I have a for loop and within the for loop i have an if statement where if
>something is true then i want to get out of the for loop. I am trying to do
>it the following way but it is not working,
>
> R=10
> div = 0.5
> for s=1:R
> ...
> ...
> if (div_sum/numJobs) < div
> cef = 0;
> s = R+1;
> else
> cef = 1;
> end
> ...
> end
>
> The thing is suppose that s in the for loop is 4 and it get that cef =0,
> then I do not want to keep checking for s=5,6,7,8,9,10. I just want to
> exit the for loop. That is why I put that s=R+1 but it is not working. You
> can try that with a simple example.

That is expected and correct behavior. You are allowed to modify the loop
variable inside the FOR loop; however, when the loop body ends and you
return to the FOR statement, whatever value the loop variable has is
discarded and the next column of the array that makes up the loop expression
is assigned into the loop variable.

> Is there any possible way that this can be done since the most logical way
> does not work? I know break or return do not work because there would not
> be a way to put it inside the for loop only.

?

for k = 1:5
    if k==5
        break;
    end
    disp(k)
end

BREAK inside an IF statement inside a FOR loop works just fine.

> And by the way div_sum and numJobs or any other variables are all
> specified in the code, its just that part that is not doing what it is
> supposed.

They're defined in the pieces you snipped out and replaced with ellipses
(...), understood.

--
Steve Lord
slord@mathworks.com

Tags for this Thread

No tags are associated with this thread.

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.

Contact us