Solved

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

Posted on 2007-04-05
5
839 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
5 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 250 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 100 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

809 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