f



stop an if loop in a for loop

Hello,

I try to find a way to stop an if loop inside of a for loop
but keep the for loop going. I tried to use "return" and
"break" but they stop the for loop too.

Bellow is a simple script to explain what I mean. Using
"return", it stops the loop when z = 2 but I would like to
find a way to keep the for loop going until z = 3.

i = 1;
for z = 1:3
    i = i+1;
    if i ==3
        return   % Stop the if loop
    else
        display('Cant do it')
    end
end

Thank you for you help

Benoit
0
igneb (42)
1/24/2008 11:27:03 PM
comp.soft-sys.matlab 211264 articles. 26 followers. lunamoonmoon (257) is leader. Post Follow

4 Replies
1172 Views

Similar Articles

[PageSpeed] 34

"Benoit " <igneb@iastate.edu> writes:

> Hello,
>
> I try to find a way to stop an if loop inside of a for loop
> but keep the for loop going. I tried to use "return" and
> "break" but they stop the for loop too.
>
> Bellow is a simple script to explain what I mean. Using
> "return", it stops the loop when z = 2 but I would like to
> find a way to keep the for loop going until z = 3.
>
> i = 1;
> for z = 1:3
>     i = i+1;
>     if i ==3
>         return   % Stop the if loop
>     else
>         display('Cant do it')
>     end
> end

"if" is not a loop.  If you do not want to execute anything, you don't
have to:

if i == 3
  % Do nothing
else
  display('Do something here');
end

For this set of conditions, though, you are better off finding a way to
express the condition without the else.

if i ~= 3
  display('Do something here')
end

-Peter
0
boettcher (2304)
1/25/2008 12:01:55 AM
Okay I will try!

Thank you
0
igneb (42)
1/25/2008 12:13:02 AM
"Benoit " <igneb@iastate.edu> wrote in message <fnb6s7$li7
$1@fred.mathworks.com>...
> Hello,
> 
> I try to find a way to stop an if loop inside of a for 
loop
> but keep the for loop going. I tried to use "return" and
> "break" but they stop the for loop too.
> 
> Bellow is a simple script to explain what I mean. Using
> "return", it stops the loop when z = 2 but I would like 
to
> find a way to keep the for loop going until z = 3.
> 
> i = 1;
> for z = 1:3
>     i = i+1;
>     if i ==3
>         return   % Stop the if loop
>     else
>         display('Cant do it')
>     end
> end
> 
> Thank you for you help
> 
> Benoit

Use the continue statemt. This immediately passes control 
to the next iteration of the loop without stopping the 
loop. For example:

 k = 1;
 for z = 1:3
     k = k+1;
     if k == 3
         continue
     end
     % other stuff in the loop
 end

And, unrelated, you really shouldn't be using i as a 
variable since MATLAB uses i for the imaginary number sqrt
(-1). Same for variable name j.

James Tursa

0
1/25/2008 1:33:02 AM
"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in
message <fnbe8e$b02$1@fred.mathworks.com>...
> "Benoit " <igneb@iastate.edu> wrote in message <fnb6s7$li7
> $1@fred.mathworks.com>...
> > Hello,
> > 
> > I try to find a way to stop an if loop inside of a for 
> loop
> > but keep the for loop going. I tried to use "return" and
> > "break" but they stop the for loop too.
> > 
> > Bellow is a simple script to explain what I mean. Using
> > "return", it stops the loop when z = 2 but I would like 
> to
> > find a way to keep the for loop going until z = 3.
> > 
> > i = 1;
> > for z = 1:3
> >     i = i+1;
> >     if i ==3
> >         return   % Stop the if loop
> >     else
> >         display('Cant do it')
> >     end
> > end
> > 
> > Thank you for you help
> > 
> > Benoit
> 
> Use the continue statemt. This immediately passes control 
> to the next iteration of the loop without stopping the 
> loop. For example:
> 
>  k = 1;
>  for z = 1:3
>      k = k+1;
>      if k == 3
>          continue
>      end
>      % other stuff in the loop
>  end
> 
> And, unrelated, you really shouldn't be using i as a 
> variable since MATLAB uses i for the imaginary number sqrt
> (-1). Same for variable name j.
> 
> James Tursa
> 

Thank you very much, that's exactly what I was looking for!
and I will try to not use i!

Benoit

0
igneb (42)
1/25/2008 3:53:01 AM
Reply: