?
Solved

x = a++ + y++; what is x?

Posted on 2007-04-05
5
Medium Priority
?
844 Views
Last Modified: 2011-10-03
int x, a=6, y=7;
x = a++ + y++;

x will be 13,
but I think ++ has high precendence than + ,
so a++, y++ will be exercuted before +,
so I think x should be 15.

please let me know what is wrong in my think.

0
Comment
Question by:keepworking
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 1000 total points
ID: 18860370
The post-increment operator, ++, placed after a variable, returns the value first, then increments the variable. Thus the plus operator, +, gets the values 6 and 7.
0
 
LVL 8

Assisted Solution

by:mnrz
mnrz earned 400 total points
ID: 18860445
because ++ occured after the variable so it has not preceding over the +
it means after the action increment the value
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18860730
Yes, just look at it as:

x = a ++ + y ++ ;

becomes

x = ( a ++ ) + ( y ++ ) ;

So a ++ and y ++ are evaluated first (as the current values of a and y respectively) and then added to be assigned to x. The values of a and y are then incremented after the statement.
0
 

Author Comment

by:keepworking
ID: 18860783
mayankeagle:

it seems yu added the comment during I was closing it, so I missed yours, I am sorry. how to add points to you now?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18862234
You don't need to - its ok.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question