Operator Notation Operators Are Special Symbols That Allow Us to Perform Certain Actions on the Operands. by Using These Operato
Total Page:16
File Type:pdf, Size:1020Kb
Operator Notation Operators are special symbols that allow us to perform certain actions on the operands. By using these operators we can write expressions that can act on one, two, or three operands to perform a specific task. We have three different styles of operator notations based on where the operator is placed relative to its operands. This discussion focuses on operation notation for binary operators (i.e., operators that require two operands). Infix Notation In this type of notation, the operator is written in between the two operands. For example, the addition of two numbers, 3 and 4, can be written as follows in the infix notation: 3 + 4 The infix notation is the most commonly used notation in mathematics. A more complex example, of parenthesized infix notation, is shown below: ((1*2) + (3*4)) – ((5-6) +(7/8)) Here the operators are evaluated from left to right, with the operations within parentheses being evaluated first, followed by multiplication, division, addition, and subtraction. Prefix Notation This is also known as Polish notation. In this type of notation, the operators are written before their operands. The operators are evaluated from left to right and they operate upon their nearest two right operands. For example, the addition of two numbers 3 and 4 can be written as follows in the prefix notation: + 3 4 Here the operator “+” is written before 3 and 4 and acts on its immediate two operands on the right (i.e., 3 and 4). A more complex example in prefix notation is shown below: - + * 1 2 * 3 4 + - 5 6 / 8 8 The above example involves multiple operands in the beginning. This changes the order in which computations are done. As you are going from left to right, the “*” operator will work with the numbers 1 and 2 with a result of 2 replacing “* 1 2”. Then the “*” operator will work with the numbers 3 and 4 with a result of 12 replacing “* 3 4”. At this point the expression looks as follows: - + 2 12 + - 5 6 / 8 8 http://www.saylor.org/courses/cs101/#4.1.1 The Saylor Foundation Saylor.org Page 1 of 3 Next the “-” operator acts on 5 and 6 to result in a -1, and this is followed by “/” acting on 8 and 8 to result in a 1. At this point the expression looks as follows: - + 2 12 + -1 1 We have gone through a round of left to right evaluation, and now the second round starts, as we still have operators to be resolved. Now the “+” operator will act on 2 and 12, resulting in 14, and the other “+” operator will act on -1 and 1, resulting in 0. At this point the expression looks as follows: - 14 0 Finally, the last round results in 14 as the answer for the evaluated expression. Postfix Notation This is also known as reverse Polish notation. In this notation, the operators are written after their operands. As with prefix notation, the operands are evaluated from left to right. An operator acts on its immediate two preceding operands. For example, the addition of the numbers 3 and 4 can be written as follows in postfix notation: 3 4 + Here the operator “+” is written after numbers (3 and 4) that need to be added. A more complex example of postfix notation is shown below: 1 2* 3 4* + 5 6 - 8 8 / + - As you are going from left to right, the “*” operator will work on the numbers 1 and 2, with a result of 2 replacing “* 1 2”. Then the “*” operator will work on the numbers 3 and 4, with a result of 12 replacing “* 3 4”. At this point the expression looks as follows: 2 12 + 5 6 - 8 8 / + - Next the “-” operator acts on 5 and 6, resulting in a -1, and this is followed by “/” acting on 8 and 8, resulting in a 1. At this point the expression looks as follows: 2 12 + -1 1 + - Now the “+” operator will act on 2 and 12, resulting in 14, and the other “+” operator will act on -1 and 1, resulting in 0. At this point the expression looks as follows: 14 0 - Finally, the last round results in 14 as the answer for the evaluated expression. Summary http://www.saylor.org/courses/cs101/#4.1.1 The Saylor Foundation Saylor.org Page 2 of 3 As the infix notation is commonly used in mathematics and is practiced from very early on, this style is most intuitive and easy to understand. Most modern programming languages, including Java, use infix notation for writing mathematical expressions. However, prefix is often used for operators that take a single operand, such as logical negation (!) and function calls. The postfix notation is similar to the infix, with operators following the operands rather than preceding them. The use of postfix notation is not common in any of the programming languages. http://www.saylor.org/courses/cs101/#4.1.1 The Saylor Foundation Saylor.org Page 3 of 3 .