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:
conditional for loop

Subject: conditional for loop

From: Hermano Cappa

Date: 27 Aug, 2010 08:19:08

Message: 1 of 3

Hello,

I have a simple for loop like this:

for i = 1:10
     /* code */
end

But I only want to repeat this /* code */ 10 times if a parameter (let say 'repeat') is true. If the parameter is false the /* code */ must be calculated only one time. I don't want to copy the whole /* code */ outside the for loop and use an if-statement. How can I do this?

Thanks

Subject: conditional for loop

From: Oleg Komarov

Date: 27 Aug, 2010 08:35:35

Message: 2 of 3

"Hermano Cappa" <cappasteven@gmail.com> wrote in message <i57sds$lim$1@fred.mathworks.com>...
> Hello,
>
> I have a simple for loop like this:
>
> for i = 1:10
> /* code */
> end
>
> But I only want to repeat this /* code */ 10 times if a parameter (let say 'repeat') is true. If the parameter is false the /* code */ must be calculated only one time. I don't want to copy the whole /* code */ outside the for loop and use an if-statement. How can I do this?
>
> Thanks

You can break the loop:
for ii = 1:10
    disp(ii)
    if ii == 1
        break
    end
end

Oleg

Subject: conditional for loop

From: Justus Skorps

Date: 27 Aug, 2010 09:57:22

Message: 3 of 3

On 27 Aug., 10:19, "Hermano Cappa" <cappaste...@gmail.com> wrote:
> Hello,
>
> I have a simple for loop like this:
>
> for i = 1:10
> /* code */
> end
>
> But I only want to repeat this /* code */ 10 times if a parameter (let say 'repeat') is true. If the parameter is false the /* code */ must be calculated only one time. I don't want to copy the whole /* code */ outside the for loop and use an if-statement. How can I do this?
>
> Thanks

for i = 1:(1+repeat*9)
      /* code */
end

Tags for 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