Lecture 6 Boolean Logic and Logical Operations
Lecture 6 Boolean Logic and Logical Operations
(Chapter 4—Riggs)
Upcoming assignments:
Assignment 4 (Functions): due tomorrow, 11:59 PM
Assignment 5 (Boolean Logic & Logical Operations): due February 07, 11:59 PM
Statement 2
Code A Code B
Repetitive
Statement 3 Code
Logic statements are either true or false and can be used to control which code is executed.
Logic Flow Control
(Chapter 4—Riggs)
Outline of structures:
• if Statements
Selection Statements
• if else Statements
• while Loops
Repetitive Statements
• for Loops
Logic “switches” are needed in almost every program; e.g., pipe flow
These switches enable the sequence of calculations to change depending on the simulated conditions.
Logic Components for Flow Control
(Logical and Relational Operators)
Notes:
Care should be taken when using the == logic component because round-off error can prevent the
statement from working as intended!
The combinational functions (e.g., AND and OR) are used to combine two or more logic statements into a
more complex switch.
Boolean data types may have one of two values: true or false.
Examples:
Primarily associated with conditional statements that control the flow of data within
programs.
Logical Variables (Booleans), Cont’d.
MATLAB has a “logical data type” that has two possible values: true and
false.
whos : list current variables and sizes. Logical type saves memory.
Comparison/Relational Operators
Equals == 5 == 6 False
Some Rules:
Relational operators are used as arithmetic operators within an expression (control data flow
within a program).
When two numbers are compared, the result is 1 if comparison is true and 0 if it is false.
If two arrays are compared (arrays of same size only), the comparison is done element-by-
element.
If a scalar is compared with an array, the scalar is compared to every element of the array.
The arithmetic operations (+,-,*,/,\) have precedence over relational operators. Parentheses ()
can be used to alter order of precedence.
Comparison/Relational Operators
Examples:
Relational operators may also be used to compare a scalar value with an array.
z = a > b
1 0
0 1
Relation operators can also be used to compare two arrays of the same size.
z = a >= b
1 0
1 1
Order of precedence:
3+4<16/2 3+(4<16)/2
ans = 1 ans = 3.5000
Logical Operators
Operator Operation
& Logical AND
&& Logical AND with shortcut evaluation
| Logical Inclusive OR
|| Logical Inclusive OR with shortcut evaluation
~ Logical NOT
Logical operators are operators with one or two logical operands that yield a logical
result.
Try this
General Framework: x LC y
x > 1 ; x < 6 ; x >= 1 ; (x > 1) & (x < 6); (x < 1) | (x > 6)
Logical Operators
General Framework: x LC y
x y x&y x|y ~x
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0
Use the & AND if it is necessary to ensure that both operands are evaluated, or if the comparison is
between arrays. Otherwise, use && AND.
Use the | OR if it is necessary to ensure that both operands are evaluated, or if comparison is between
arrays. Otherwise, use || OR.
Logical Operators
Some Rules:
Logical operators have numbers as operands. A nonzero number is true, and a zero
number is false.
If two arrays are the operands (arrays of same size only), the comparison is done element-
by-element.
If a scalar is one operand and an array is the other, the operation is done between the scalar
and each element of the array.
The NOT operation has one operand. When used with a scalar, the outcome is scalar 1 or
0. When used with an array, the outcome is an array of same size with 1s for nonzero
numbers and 0s for zero numbers.
Logical Operators
Examples:
3 & 7
ans = 1 [3 and 7 are both true (nonzero), so outcome is 1]
a = 5|0
a = 1 [1 is assigned to a since at least one number is true]
~25
ans = 0 [25 is true and the opposite is false]
T = 25*((12&0)+(~0)+(0|5))
T = 50
x = 9; y = 6;
NOTE the difference between = and == statements!
z = (x == y)
z=0 A logical scalar.
• When comparing two floating point numbers for equality, should always test to see if
their difference is less than some small number!
x = 0.0707106781186548
For example: y = 0.0707106781186547
x = 500/(5000*sqrt(2)); y = 600/(6000*sqrt(2));
z = (x == y)
z=0 Gives incorrect result (answer should be true!)
Correct comparison:
Note:
Not all mathematical operations are defined for logical variables:
E.g.,
Always use the values true/false for logical values—avoid using real 1/0 for logical
values.
Never test two floating point numbers for equality—always test the absolute of their
difference to a small number.
Review: Relational/Logical Operators
Evaluate the following expressions (write down with pen and paper, not in MATLAB):
z = ~(a > b)
z = c <= d
z = a*b > c
z = a*(b > c)
if Statements
Basic Form:
if (x > 5)
fx = 1;
end;
Can have more complex logical expressions:
if (x > 5)
fx = 1; Executable code 1
else Logic determines which is executed.
fx = 0; Executable code 2
end
if x < 0
Note: The elseif statement can be used f = 0;
for nested logical statements! elseif x <= 4
f = x;
else
f = 4;
end