Presentation is loading. Please wait.

Presentation is loading. Please wait.

Expressions.

Similar presentations


Presentation on theme: "Expressions."— Presentation transcript:

1 Expressions

2 1. Arithmetic Expression:
- It is composed of operands and arithmetic operations ( + , - , *, /, MOD). - Its result is a numeric value (e.g gives 7) - Operands may be numbers and/or identifiers that have numeric values e.g. x – y gives 4, if x is 6 and y is 2 x / gives 6, if x 12 T MOD 2 gives 0 if T is any even number, and 1 if T is any odd number

3 2. Logical Expression: - It is called also Boolean expression.
- It is composed from operands and operators. - Operands are identifiers that have logical values - Its result is a logical value (true or false) (see later). - Operators are logical: AND , OR, NOT e.g. X AND Y a AND b OR c where X, Y, a, b, c are defined logical

4 3. Relational Expression:
- It is composed from operands and operators. - Operands may be numbers and/or identifiers that have numeric values - Its result is a logical value (true or false). - Operators are relational operators: <, >, =, ≠, ≤, ≥ e.g. (a < b) gives true, if value of a is less than value of b false, if value of a is not less than value of b (x ≠ y) also gives true or false according to the values of x and y

5 NOTES A relational expression may contain arithmetic sub-expressions,
e.g. ( ) < (12 * 4 ) 2) A logical expression may contain relational and arithmetic sub-expressions, e.g. x AND y AND ( a > b ) (2 + t ) < (6 * w ) AND ( p = q )

6 Expressions are evaluated according to the precedence rule.
Operator Precedence Expressions are evaluated according to the precedence rule. Precedence Rule: - Each operator has its own precedence that indicates the order of evaluation. - If the expression has operators of the same precedence, then the evaluation starts from left of expression to the right.

7 Precedence Description Operator Higher parentheses ( unary plus, unary minus, Not +, – , NOT *, /, MOD Binary plus, binary minus + , - <, <=, >, >= Equal, not equal = , != AND OR Lower Assignment

8 Examples Find the value of the following expression: (1) 5 + 8 * 2 / 4
(1) * / 4 16 4 (This is the final result)

9 ( ) / 12 3 9 (this is the final result)

10 Evaluating Logical Expressions
The truth table AND table AND True False True True False False False False

11 (2) OR table OR True False True True True False True False
(3) NOT table NOT True False False True

12 Examples on Logical Expressions
If x = True, y = False, z = False, find the value of the expression x AND y OR z x AND y OR z False False (the final result)

13 (2) If a = 3, b = 5, x = true, y = false, find the value of the expression: ( a < b ) AND y OR x
True (the final result)


Download ppt "Expressions."

Similar presentations


Ads by Google