Download objects
Transcript
68 - 02: Variables and Data Types mod as and shl shr Modulo (the remainder an of integer division) (in C this is %) Allows a type-checked conversion at runtime (covered in Chapter 8) Boolean or bitwise and (in C this is either && or &) Bitwise left shift (in C this is <<) Bitwise right shift (in C this is >>) Unary Operators (Highest Precedence) @ not Memory address of a variable or function (returns a pointer, in C this is &) Boolean or bitwise not (in C this is !) Different from many other programming languages, the and and or operators have higher precedence than comparison ones. So if you write: a < b and c < d the compiler will do the and operation first, generally resulting in a compiler error. If you want to test both comparisons, you should enclose each of the < expressions in parentheses: (a < b) and (c < d) For math operations, instead, the common rules apply, with multiplication and division taking precedence over addition and subtraction. The first two expressions below are equivalent, while the third is different: 10 + 2 * 5 10 + (2 * 5) (10 + 2) * 5 // result is 20 // result is 20 // result is 60 Some of the operators have different meanings when used with different data types. For example, the + operator can be used to add two numbers, concatenate two strings, make the union of two sets, and even add an offset to a pointer (if the specific pointer type has pointer math enabled): 10 + 2 + 11 10.3 + 3.4 'Hello' + ' ' + world' However, you cannot add two characters, as is possible in C. An unusual operator is div. In Object Pascal, you can divide any two numbers (real or integers) with the / operator, and you'll invariably get a real-number result. If you need to divide two integers and want an integer result, use the div operator instead. Here are two sample assignments (this code will become clearer as we cover data types in the next chapter): realValue := 123 / 12; integerValue := 123 div 12; Marco Cantù, Object Pascal Handbook