can anybody explain me in detail about the associativity of operators plz....

2
Contributors
1
2
Views
8 Years
Discussion Span
Last Post by TotoTitus

I'm not sure if this is the place for associativy discussion - this is more a math thing :D
So, here it goes:
Let's analyze the addition (+) operation.
2+3=5. We all know that.
2+3+5 = 10. Again, we all know that.
I'll put some redundant parentheses:
(2+3)+5=10
but also
2+(3+5)=10
When adding numbers, this is extremely obvious, that's why i called the parentheses ' redundant '. It's obvious that it doesn't matter what i add first 2 and 3 or 3 and 5.
Same for multiplication:
2*3*4 = 24
(2*3)*4 = 24
2*(3*4) = 24.
We call addition and multiplication associative operations, because the order in which we operate the numbers doesn't matter
Let's take another operation, for instance, division:
8/4 = 2.
8/4/2 = 1. First we divide 8 by 4 ( and obtain 2) , and then we divide the resulting 2 by 2, and thus we get 1. (We execute operations from left to right)
But if we change the order of operations:
8/(4/2) = 4. First, because of the parentheses, we divide 4 by 2 (and get 2). THEN only, we divide 8 by 2 and get 4.
Same with substraction:
8-4-2 = 2 . First, we substract 4 from 8 (obtaining 4), and then we substract 2 from the previously obtained 4 -> bingo we obtain 2
But in the following situation:
8-(4-2) = 6. Because of the parentheses, we first substract 2 from 4 (=2), and then substract the previously obtained 2 from 8 -> 6.
So, we call substraction and division non-associative operations because the order in which we deal with the operations matters.

Edit: There's more:
Even non-associative operations can have a form of associativity.
8-4-2 is not equal to 8 - (4 -2 )
BUT
8-4-2 IS equal to (8- 4) -2, because we process the expression from left to right.
Such operations ( division included), in which forcing the evaluation to left-to-right using parentheses yields the same result as when parentheses are not used, are called left-associative operations.

Let's take another operation: Power.
2^3^2 = 512.
BUT
2^3^2 = 2^(3^2) = 2^9 = 64.
BUT
(2^3)^ 2 = 64 which is NOT equal to 2^3^2

In this example, putting parentheses to the left will change the result, whereas putting parentheses at the right of the expression will not change the result, because power is processed from RIGHT to LEFT.
Such operations, where forcing the evaluation of the expression from RIGHT to LEFT yields the same result as if parentheses were not used, are called
right-associative operations.
An operation is associative when it is both left-associative, and right-associative (<=> order of evaluating doesn't matter), otherwise it is unassociative.

Edited by TotoTitus: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.