Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CH/S4CIT(A)/Sept. 2005. OPERATORS AND EXPRESSIONS Operators 4 types of operators in Pascal: i. arithmetic operators ii. relational operators iii. logical operators iv. string operators Arithmetic operators operation operator operand type(s) result type example positive unary + integer, real same type +x positive numbers negative unary - integer, real same type -x negative numbers addition + integer, real same type x+y add y to x subtraction - integer, real same type x-y subtract to x multiplication * integer, real same type x*y multiply x by y integer division DIV integer integer x DIV y division / integer, real real x/y integer modulo MOD integer integer x MOD y quotient after integer division of x by y divide x by y remainder after integer division of x by y e.g. algebra expression Pascal expression x + 3y x + 3 * y x - y z x - y / z xy z x * y / z (x + y) z (x + y) / z x(-y) x * (-y) z xy meaning (ref.) exp(exp(z*ln(y))*ln(x)) For integer powers x3 : x*x*x OPERATORS AND EXPRESSIONS page 1 CH/S4CIT(A)/Sept. 2005. For real powers xy : exp(y*ln(x)) which in mathematics notation e y ln x where e 2.718 and 1n means log e . Sample statements: Area := (Base * Altitude) / 2; FirstRoot := (-B + exp(ln(B*B - 4*A*C)/2))/(2*A); r := 123; s := 7; t := r mod s; (*then t = 4*) u := r div s; (*then u = 17*) Relational Operators Relational operators are used to compare two values. The result of comparison is either ‘True’ or ‘False’. operator operand type(s) result type example meaning = any type but file Boolean x=y <> any type but file Boolean x<>y x is not equal to y > any simple type Boolean x>y x is greater than y < any simple type Boolean x<y x is greater than y >= any simple type Boolean x>=y x is greater than or equal to y <= any simple type Boolean x<=y x is less than or equal to y x is equal to y e.g. if x = 3, y = 7.5, a = 120, b = 6 (x + 2*y ) < (a / b) OPERATORS AND EXPRESSIONS gives a value page 2 CH/S4CIT(A)/Sept. 2005. Logical operators Logical operators are used in logical operations. They operate on Boolean data or expression and return value of either “True” or “False”. Three Logical Operators: operator operand type result type meaning NOT Boolean Boolean logical complement AND Boolean Boolean logical and OR Boolean Boolean logical or Results of each operator (Truth Table) x y NOT X NOT y x AND y x OR y T T F F T T T F F T F T F T T F F T F F T T F F e.g. if A = 3, B = 2, C = 1 NOT ((B*B - 4*A*C)) < 0 A > 2 OR A < 0 B < 10 AND B > 0 returns false returns true returns true String Operators Only one string operator: concatenation, i.e. ‘add’ two strings, the second string is added to the end of the first string. e.g. Str1 := 'Con' + 'cate' + 'nate'; Str1 is assigned ‘Concatenate’. Strings may be compared using relational operators. String comparisons are performed by taking one character at a time from the leftmost character of each string. The ASCII values of the characters from the two strings are compared. ** ASCII - American Standard Code for Information Interchange. All operation must be done with strict restriction on type. Example: If the variable declaration is as follows: var a, b c, d e : integer; : real; : string; Then the following statements are wrong in syntax: a e c c := := := := c + b; 12 + 43; e * 5; a div b; OPERATORS AND EXPRESSIONS page 3 CH/S4CIT(A)/Sept. 2005. ** Expression An expression may be simply a constant, a variable, or a combination of constants, variables and functions with operators. e.g. force * distance * cos(angle) Only a single value can be returned from an expression no matter how complicated the expression is. *** Order of Precedence of Operators Operators in expression are evaluated in order of their precedence. For operators of equal precedence, they will be evaluated as they are encountered, reading from left to right. precedence operators operator class ( ) Parenthesis 2 NOT Unary 3 *, /, DIV, MOD, AND Multiplying 4 +, -, OR Adding =, <, >, < >, < =, > =, IN Relational 1 (highest) 5 (lowest) OPERATORS AND EXPRESSIONS page 4