Logic Flow Control
(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
For today’s class:
Create a new sub-folder, named Lecture6, in your ME2543 folder.
Open MATLAB and change the path to the Lecture6 folder.
Be sure to save all exercises done in class (we will be using .m files).
Logic Flow Control
(Chapter 4—Riggs)
Three ways to execute code:
Sequential Code Selection Code Repetitive Code
Statement 1 Logic Logic
Statement Statement
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
Friction factor f depends on
value of Reynolds # Re.
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.
General Framework: x LC y (e.g., x > 1 & x < 6 x<1|x>6)
Logical Variables (Booleans)
Boolean data types may have one of two values: true or false.
Examples:
Intended to represent the truth values of logic and Boolean algebra.
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.
• The logical value of true is represented by the value 1.
• The logical value of false is represented by the value 0.
Note: There is a memory difference in using these data types in
MATLAB
Name Size Bytes Class
ME2543_good = true;
ME2543_good 1x1 1 logical
ME2543_bad = false;
ME2543_bad 1x1 1 logical
ME2543_1 = 1;
ME2543_1 1x1 8 double
ME2543_0 = 0;
ME2543_0 1x1 8 double
whos
whos : list current variables and sizes. Logical type saves memory.
Comparison/Relational Operators
Operator MATLAB Example Value
symbol
Equals == 5 == 6 False
Not equal to ~= 5 ~= 6 True
Is greater than > 5>6 False
Is less than < 5<6 True
Is greater than or equal to >= 5 >= 5 True
Is less than or equal to <= 6 <= 5 False
Comparison/Relational Operators
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 scalars are compared, the result is a scalar 1 or 0.
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
Truth Table for Logic Operators: Think of 1 as true and 0 as false!
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
Good programming practice:
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.
Can be used with scalars and arrays.
If two scalars are the operands, the result is a scalar 1 or 0.
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 3 0 11 0 15]; y = [2 0 13 -11 0 4];
x&y
ans = 1 0 0 1 0 1
Relational/Logical Operators
Relational operators produce a single logical value.
x = 9; y = 6;
NOTE the difference between = and == statements!
z = (x == y)
z=0 A logical scalar.
Relational operators have equal precedence (evaluated left to right).
z = (5 > 3) & (8 < 10) z1 = 5 > 3; z2 = 8 < 10;
z=1 OR z = (z1 & z2)
z=1
When used on arrays, relational operators are applied element-by-element and
produce a logical array:
x = [6, 3, 9]; y = [14, 2, 9];
z = (x < y)
z=1 0 0 A logical array.
Relational/Logical Operators
Equality testing ( == ) can lead to unexpected results due to round-off errors.
• 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:
abs(x) – abs(y) < 1.0e-14 If this condition is true, then consider
the numbers numerically equal.
Relational/Logical Operators
Note:
Not all mathematical operations are defined for logical variables:
E.g.,
x = (5>3 & 8<10);
y = sin(x);
Undefined function ‘sin’ for input arguments of type ‘logical’.
Good Programming Practices
Relational Operations
Always use the values true/false for logical values—avoid using real 1/0 for logical
values.
Always add appropriate parentheses to complex logical expressions, even when
they are not required, to make the expressions easier to read.
Always use == to test for equality of integer or logical variables.
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 = a > c && b > c
z = c <= d
z = a*b > c
z = a*(b > c)
if Statements
Basic Form:
if ‘logic statement’ (If the logic statement is TRUE, then the
executable code executable code is executed.)
end
e.g., consider the function:
if (x > 5)
fx = 1;
end;
Can have more complex logical expressions:
𝑓 ( 𝑥 ) =1 if 𝑥>5 and 𝑥<10
if x > 5 & x < 10
fx = 1;
end
if else
Statements
For the if statement, only one set of commands is executed if the logic statement is
TRUE.
For the if else statement, the logic statement determines which of two separate codes
are executed:
e.g., in the previous example:
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