1 / 9

Computer Programming and Problem Solving Logic and Relational Operators

Logical Values. Some types of formal logic are based on ?truth statements"Either the statement is true, or it is falseThe ?logical" data type in MATLAB captures this ideaKnown as a ?Boolean" data type in most other languagesRepresents base-2 in numeric systemsCurrently forms the foundation for

siusan
Download Presentation

Computer Programming and Problem Solving Logic and Relational Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. CS 101 – 010 Lecturer: Daniel J. Boston GITC 2315-C wednesday friday 1:00-2:30pm 2:30-4pm Computer Programming and Problem Solving Logic and Relational Operators

    2. Logical Values Some types of formal logic are based on “truth statements” Either the statement is true, or it is false The “logical” data type in MATLAB captures this idea Known as a “Boolean” data type in most other languages Represents base-2 in numeric systems Currently forms the foundation for all digital circuits MATLAB allows logical data types to be dealt with as integers True is 1 -- Non-zero numbers are true False is 0 -- Zero is false

    3. Relational Operators Relational operators test the Relationship between two operands (which can be expressions) They are a “Truth Statement”, and resolve to either True (1) or False (0)

    4. Relational Operators Relational operators can be used on arrays The arrays must be the same size OR One of the operands must be a scalar Relational operators can be used on strings Both strings must be the same length Results in a vector of truth values Each character is pair-wise compared

    5. Relational Operators Be careful! “==“ is an equality test, but “=“ is the assignment operator The equality test is strict equality; number precision errors can produce wrong answers from logically sound comparisons Try it out: a = 0; b = sin(pi); a == b Instead use: abs(a – b) < 1.0E-14

    6. Logic Operators Logic Operators can combine “truth statements” together in helpful ways

    7. Logic Operators – Truth Table

    8. Evaluation order Parenthesis are evaluated inner to outermost Unary Operators are evaluated (^, -, then ~) Arithmetic operations are evaluated Relational operators are evaluated from left to right Logical AND (& and &&) are evaluated from left to right Logical OR (exclusive and inclusive) are evaluated from left to right

    9. Logical Functions Some functions also resolve to “true” or “false” ischar(a) -- true if a is a character array (a string) isempty(a) -- true if a is an empty array isinf(a) – true if a is infinite (Inf) isnan(a) – true if a is not a number (NaN) isnumeric(a) – true if a is a numeric array logical(a) – element-by-element true if the sub-element is non-zero, false otherwise

More Related