ZK Zhao
ZK Zhao

Reputation: 21553

How to use or in while loop?

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.

Upvotes: 1

Views: 258

Answers (2)

Sam Roberts
Sam Roberts

Reputation: 24127

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.

Upvotes: 4

Digol
Digol

Reputation: 390

Write it this way

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

Upvotes: 5

Related Questions