Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Одиноков / МиИПиС_асп_13г / MATLAB_R2013a_Выбор.doc
Скачиваний:
33
Добавлен:
15.04.2015
Размер:
4.03 Mб
Скачать

Short-Circuit Operators

The following operators perform AND and OR operations on logical expressions containing scalar values. They are short-circuit operators in that they evaluate their second operand only when the result is not fully determined by the first operand.

Operator

Description

&&

Returns logical 1 (true) if both inputs evaluate to true, and logical 0 (false) if they do not.

||

Returns logical 1 (true) if either input, or both, evaluate to true, and logical 0 (false) if they do not.

The statement shown here performs an AND of two logical terms, A and B:

A && B

If A equals zero, then the entire expression will evaluate to logical 0 (false), regardless of the value of B. Under these circumstances, there is no need to evaluate B because the result is already known. In this case, MATLAB short-circuits the statement by evaluating only the first term.

A similar case is when you OR two terms and the first term is true. Again, regardless of the value of B, the statement will evaluate to true. There is no need to evaluate the second term, and MATLAB does not do so.

Advantage of Short-Circuiting.  You can use the short-circuit operators to evaluate an expression only when certain conditions are satisfied. For example, you want to execute a function only if the function file resides on the current MATLAB path.

Short-circuiting keeps the following code from generating an error when the file, myfun.m, cannot be found:

comp = (exist('myfun.m') == 2) && (myfun(x) >= y) (для истинности вызажения exist('name') == 2 one of the following is true:

■ name exists on your MATLAB® searchpath as a file with extension .m.

■ name is the name of an ordinary file on your MATLAB search path.

■ name is the full pathname to any file.)

Similarly, this statement avoids attempting to divide by zero:

x = (b ~= 0) && (a/b > 18.5)

You can also use the && and || operators in if and while statements to take advantage of their short-circuiting behavior:

if (nargin >= 3) && (ischar(varargin{3})) (здесь nargin - number of function input arguments; ischar - determine whether item is character array; varargin - variable-length input argument list)

  • R2013a>MATLAB>Language Fundamentals>Operators and Elementary Operations>Logical Operations

  • Operator Precedence (см. R2013a>MATLAB>Language Fundamentals>Operators and Elementary Operations>Arithmetic)

You can build expressions that use any combination of arithmetic, relational, and logical operators. Precedence levels determine the order in which MATLAB evaluates an expression. Within each precedence level, operators have equal precedence and are evaluated from left to right. The precedence rules for MATLAB operators are shown in this list, ordered from highest precedence level to lowest precedence level:

  1. Parentheses ()

  2. Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^)

  3. Unary plus (+), unary minus (-), logical negation (~)

  4. Multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/), matrix left division (\)

  5. Addition (+), subtraction (-)

  6. Colon operator (:)

  7. Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=)

  8. Element-wise AND (&)

  9. Element-wise OR (|)

  10. Short-circuit AND (&&)

  11. Short-circuit OR (||)