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:
loops

Subject: loops

From: Jessica

Date: 28 Apr, 2010 13:01:05

Message: 1 of 5

I am running the loop:

for i=1:10
    b=i+1;
        if Question=='1'
             b=7;
             i=8; %I want to change this!
       end;
end;


I want this loop to run such that if Question==1 and I'm on the 2nd iteration of the loop, the next iteration of the loop with be the 8th time around (that is, i=8). In this manner, the loop would not run through i=3-7.

Thanks!

Subject: loops

From: dpb

Date: 28 Apr, 2010 13:11:36

Message: 2 of 5

Jessica wrote:
> I am running the loop:
>
> for i=1:10
> b=i+1;
> if Question=='1'
> b=7;
> i=8; %I want to change this!
> end;
> end;
>
>
> I want this loop to run such that if Question==1 and I'm on the 2nd
> iteration of the loop, the next iteration of the loop with be the 8th
> time around (that is, i=8). In this manner, the loop would not run
> through i=3-7.

doc continue % you'll need another test as well as 'Question'

or restructure w/ uncounted loop construct

--

Subject: loops

From: Jessica

Date: 28 Apr, 2010 13:18:04

Message: 3 of 5

dpb <none@non.net> wrote in message <hr9c9n$1qn$1@news.eternal-september.org>...
> Jessica wrote:
> > I am running the loop:
> >
> > for i=1:10
> > b=i+1;
> > if Question=='1'
> > b=7;
> > i=8; %I want to change this!
> > end;
> > end;
> >
> >
> > I want this loop to run such that if Question==1 and I'm on the 2nd
> > iteration of the loop, the next iteration of the loop with be the 8th
> > time around (that is, i=8). In this manner, the loop would not run
> > through i=3-7.
>
> doc continue % you'll need another test as well as 'Question'
>
> or restructure w/ uncounted loop construct
>
> --

I'm relatively new to programming. Can you provide a little more detail on going about this? Thanks!

Subject: loops

From: ImageAnalyst

Date: 28 Apr, 2010 13:32:55

Message: 4 of 5

Is Question a number or a character?
Like dpb said,

for k = 1:7
  if Question == 1 && k == 2
    continue; % This will make k=3 the next time
  end
  % rest of loop which gets done if Question not = 1
end

Better to use k rather than i, which is bad practice since it's the
imaginary number.

Subject: loops

From: dpb

Date: 28 Apr, 2010 13:41:59

Message: 5 of 5

Jessica wrote:
> dpb <none@non.net> wrote in message
> <hr9c9n$1qn$1@news.eternal-september.org>...
>> Jessica wrote:
>> > I am running the loop:
>> > > for i=1:10
>> > b=i+1;
>> > if Question=='1'
>> > b=7;
>> > i=8; %I want to change this!
>> > end;
>> > end;
>> > > > I want this loop to run such that if Question==1 and I'm on the
>> 2nd > iteration of the loop, the next iteration of the loop with be
>> the 8th > time around (that is, i=8). In this manner, the loop would
>> not run > through i=3-7.
>>
>> doc continue % you'll need another test as well as 'Question'
>>
>> or restructure w/ uncounted loop construct
>>
>> --
>
> I'm relatively new to programming. Can you provide a little more detail
> on going about this? Thanks!

Nothing ventured, noting gained... :( Why not make a stab at it???

for i=1:10
   b=i+1;
   if Question=='1'
     b=7; % you really want this here???
     if i>1 && i<8, continue,end
   end;
end;

Depending on what the rest of the logic looks like, a while construct w/
explicitly counting the loop might be useful to jump the index...

 >> q='1';
 >> i=1;
 >> i=1;while i<11, disp(i), if i==2 & q=='1',i=8,end,i=i+1;end
      1
      2
i =
      8
      9
     10
 >>

BTW, "i" (and "j" as well) are not good choices for iteration variables
in Matlab even though they're ubiquitous elsewhere. In ML, they are
defined as the imaginary i and j

After the above keyboard exercise...

 >> which i
i is a variable.
 >> clear i
 >> which i
i is a built-in function.
 >>

--

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