2,086 views
1 votes
1 votes
class Codechef {

    public static void main (String[] args)      
    {   int k=1;
      System.out.println(++k + k++ + + k);
    }
    }
     I think the steps are like this ( increment postfix has precedence over        
    prefix):
    
    1. k++ returns 1 to use, then increments k, so k now is 2.
       ++k + 1 + + k; // k is 2
    2.unary operator && prefix have same precedence ,so associativity is from                  

      right to left...so
      ++k + 1 + 2;
   
    3 ++k increments k and uses that value (++2) returning 3 3 + 1 + 2; //k   
       is 3
    4.now, go left to right: so, 3 + 1 + 2 = 6
          BUT, according to the book,  the expression is "parsed as  ( (++k) +    

        (k++) ) + (+k) which yields 2 + 2 + 3 = 7

2 Answers

2 votes
2 votes

int k=1;

 System.out.println(++k + k++ + + k); 

Applying operator precedence in Java

(((++k) + (k++)) + (+k)); //++k has the highest priority and hence () put over that, then (k++) and (+k). Arithmetic + has the least priority here and it's associativity is from left to right. 

Precedence just groups the operands to operator and it is not telling the evaluation order of operations. Evaluation order depends on language semantics and varies from language to language. In Java, evaluation order is strictly from left to right where as in C, it depends on the operator and defined sequence points. (The same statement in C produces undefined behaviour). 

++k, returns 2
k++ returns 2
+k returns 3

So, 2 + 2 + 3 = 7.

1 votes
1 votes
When associativity computed, the value is not evaluated (in compiler phases). In compiler phase the associativity is computed based on the grammar parsed in syntax analysis.

Step1: Operating precedence table helps to compute the precedence relation and based on those relations associativity assigned as follows before converting into three address translation.

assume statement in printf consider as : x = ++k+k++++k;

x= (++k) + (k++) + (+k); // this is only possible way to give associativity based on precedence rules and syntax rules.

Note: Do not try to evaluate the expression because still this code is in high level.

Step2: Now convert this code into three address code as follows:

Find number of  pre-increments and post increments:  one preicrement and one post increment.

k= k+1;

t1=k+k;

t2=t1+k; // this instuction and above instuction is similar to t2 = k + k + k where k is already incremented once.

k=k+1 // Post increment once after the given instruction is executed

Step 3: Above instructions are result of 3-Address code. Now evaluate run time using assembly instructions, you will get answer 7 when k=1 initially.

Related questions

1 votes
1 votes
1 answer
2
radha gogia asked Jul 8, 2016
1,192 views
0 / \ 5 7 / \ / \ 6 4 1 3 \ 9Tree given in the form: (node value(left subtree)(right subtree))For tree given above: (0(5(6()())(4()(9()())))(7(1()())(3()())))Input forma...
0 votes
0 votes
1 answer
4
vivek1211 asked Nov 18, 2023
293 views