How to use or in while loop?

262 Views Asked by At

I really have no idea how to use this

  while  flag==1 or n<3000 
         n=n+1
  end

And it reports:

??? Error using ==> or
Not enough input arguments.

This problem is so basic, but I can't find any examples. I search for matlab or, but or got ommited in google. I'm sorry I really got no idea about its syntax.

2

There are 2 best solutions below

1
On BEST ANSWER

Write it this way

   while  (flag==1) || (n<3000 )
         n=n+1;
  end
0
On

The straightforward answer is that you should write

while  (flag==1) || (n<3000)
    n=n+1;
end

instead. || is for "or", and && is for "and".

Why did you get the error message you were seeing? Well, although it's not often used directly, there is actually a MATLAB function or, and || is just shorthand for or. But to call or directly, you have to use it as a function. So

while or(flag==1, n<3000)
    n=n+1;
end

would work as well. When you call it as flag==1 or n<3000, it doesn't have input arguments and errors as you saw.