Home / Questions / What is the value of y after evaluating the expression given below? y+= ++y + y-- + --y; when int y=8
Explanatory Question

What is the value of y after evaluating the expression given below?

y+= ++y + y-- + --y; when int y=8

👁 90 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

   y+= ++y + y-- + --y
⇒ y = y + (++y + y-- + --y)
⇒ y = 8 + (9 + 9 + 7)
⇒ y = 8 + 25
⇒ y = 33

Practice With Code

Let's analyze and implement the Java program to evaluate the given expression:

Expression:


y += ++y + y-- + --y;

Given int y = 8, let's break it down step by step:

  1. ++y → Pre-increment: y becomes 9, and 9 is used.
  2. y-- → Post-decrement: 9 is used, then y becomes 8.
  3. --y → Pre-decrement: y becomes 7, and 7 is used.
  4. Sum: 9 + 9 + 7 = 25
  5. Final Update: y = y + 25y = 8 + 25 = 33

Java Program:


public class ExpressionEvaluation {
    public static void main(String[] args) {
        int y = 8; // Initialize y
        y += ++y + y-- + --y; // Evaluate the expression
        System.out.println("Value of y: " + y); // Output result
    }
}

Output:


Value of y: 33