Accelerating the pace of engineering and science

# Documentation Center

• Trials
• Product Updates

## Functions that Return a Logical Result

### Overview

This table shows some of the MATLAB® operations that return a logical true or false. Most mathematics operations are not supported on logical values.

Function

Operation

Setting value to true or false

logical

Numeric to logical conversion

& (and), | (or), ~ (not), xor, any, all

Logical operations

&&, ||

Short-circuit AND and OR

== (eq), ~= (ne), < (lt), > (gt), <= (le), >= (ge)

Relational operations

All is* functions, cellfun

Test operations

String comparisons

### Examples of Functions that Return a Logical Result

MATLAB functions that test the state of a variable or expression return a logical result:

```A = isstrprop('abc123def', 'alpha')
A =
1     1     1     0     0     0     1     1      1```

Logical functions such as xor return a logical result:

```xor([1 0 'ab' 2.4], [ 0 0 'ab', 0])
ans =
1     0     0     1```

Note however that the bitwise operators do not return a logical:

```X = bitxor(3, 12);
whos X
Name      Size            Bytes  Class     Attributes

X         1x1                 8  double```

String comparison functions also return a logical:

```S = 'D:\matlab\mfiles\test19.m';
strncmp(S, 'D:\matlab', 9)
ans =
1```

Note the difference between the elementwise and short-circuit logical operators. Short-circuit operators, such as && and ||, test only as much of the input expression as necessary. In the second part of this example, it makes no difference that B is undefined because the state of A alone determines that the expression is false:

```A = 0;
A & B
Undefined function or variable 'B'.

A && B
ans =
0```

One way of implementing an infinite loop is to use the while function along with the logical constant true:

```while true
a = [];  b = [];
a = input('Enter username: ', 's');

if ~isempty(a)
b = input('Enter password: ', 's');
end

if ~isempty(b)
disp 'Attempting to log in to account ...'
break
end
end```
Was this topic helpful?