Boolean Algebra, Logic Gates, and Conditional Statements in Programming
Boolean Algebra and Logic Gates
- Boolean Operators: Operators applied to binary data (ones and zeros) that perform operations like comparisons of true (1) and false (0) conditions.
- Binary Numbering System:
- Base-2 system; represents numbers with two symbols: 0 and 1.
- Decimal (base-10) uses ten symbols (0-9).
- Example: Decimal 147 = 128 + 16 + 2 + 1 = Binary 10010011.
- A byte consists of 8 digits in binary.
Logic Truth Tables
OR Gate: Output is TRUE (1) if at least one input is TRUE.
- Truth table:
- A B | A + B
- 0 0 | 0
- 0 1 | 1
- 1 0 | 1
- 1 1 | 1
AND Gate: Output is TRUE (1) if both inputs are TRUE.
- Truth table:
- A B | A . B
- 0 0 | 0
- 0 1 | 0
- 1 0 | 0
- 1 1 | 1
NOT Gate (Inverter): Inverts the input value.
- Truth table:
- A | NOT A
- 0 | 1
- 1 | 0
NOR Gate: Combines NOT and OR; outputs the inverted result of OR.
- Truth table:
- A B | A + B
- 0 0 | 1
- 0 1 | 0
- 1 0 | 0
- 1 1 | 0
NAND Gate: Combines NOT and AND; outputs the inverted result of AND.
- Truth table:
- A B | A . B
- 0 0 | 1
- 0 1 | 1
- 1 0 | 1
- 1 1 | 0
XOR Gate: Outputs TRUE if one input is TRUE and the other is FALSE.
- Truth table:
- A B | A ⊕ B
- 0 0 | 0
- 0 1 | 1
- 1 0 | 1
- 1 1 | 0
Logical Operators in MATLAB
- Basic Logical Operators:
~: NOT&: AND|: OR
- Short-circuit Operators:
&&: Short-circuit AND||: Short-circuit OR
- Examples:
f = B & C;(AND)f = A | B;(OR)f = ~A;(NOT)
Relational Operators
- Used to compare MATLAB variables:
- Examples:
x > y(Is x greater than y?)x == y(Is x equal to y?)y <= x(Is y less-than-or-equal-to x?)
- Returns logical 1 (true) or 0 (false).
- Example Comparisons:
z = (x == y);z = (x > y);z = (x < y);
Conditional Statements
If-Statement
- Control structure determining if code should execute based on conditions.
- Syntax:
matlab if condition statement; end
- Syntax:
- Combined with short-circuit operators for multiple conditions.
If-Else Statement
- Extends if-statements to branch into two code paths.
- Syntax:
matlab if condition statement1; else statement2; end
- Syntax:
If-Elseif Statement
- Allows branching to multiple conditions via elseif.
- Syntax:
matlab if condition1 statement1; elseif condition2 statement2; else statement3; end
- Syntax:
Switch-Case Statements
- Simplifies branching decisions based on a single variable.
- Syntax:
matlab switch variable case value1 statement1; case value2 statement2; otherwise statement3; end - Example of calculating force, mass or acceleration based on user input.
(These notes capture fundamental concepts of Boolean algebra, logic gates, logical and relational operators in MATLAB, as well as conditional statements for programming control structures.)