Question ?

V

Vasu

Hi !

Following is the snippet of question / answer on Sun Java site of Java
tutorial :

Consider the following code snippet:
int i = 10;
int n = i++%5;

Question: What are the values of i and n after the code is executed?
Answer: i is 11, and n is 0.


Question: What are the final values of i and n if instead of using the
postfix increment operator (i++), you use the prefix version (++i))?
Answer: i is 11, and n is 1.

My question is : in the first answer they have said n is 0, now if we
go by code execution system from top to bottom and left to right, when
compiler hits % operator i has already become 11 (10 + 1 = 11) and
dividing it with 5 we will get 1 remainder, but there answer says n is
0 - why? It should be same as they have shown in answer of 2nd
question.

Can anybody respond to my query.

Thanks in advance.
Vasu
 
J

Joshua Cranmer

Vasu said:
My question is : in the first answer they have said n is 0, now if we
go by code execution system from top to bottom and left to right, when
compiler hits % operator i has already become 11 (10 + 1 = 11) and
dividing it with 5 we will get 1 remainder, but there answer says n is
0 - why? It should be same as they have shown in answer of 2nd
question.

The expression |i++| is equivalent to "take the value of i, then
increment the variable i." So although the value of i is 11 by the time
you which the %, the % operator doesn't see i as its left operand. What
it sees is |i++|, which uses the original value of i.
 
S

Stefan Ram

Vasu said:
when compiler hits % operator i has already become 11 (10 + 1 = 11) and
dividing it with 5 we will get 1 remainder, but there answer says n is
0 - why?

I am seeing the omission of the determiner in noun clauses so
often in this newsgroup, that I would like to ask: Is »when
compiler hits ...« correct English? I always feel annoyed when
reading such a wording, because I believe that only »When /the/
compiler hits ...« is correct. (I also believe that the
determiner sometimes may be omitted, for example, I believe
that »such a wording« might also be written as »such wording«,
but I believe that this does not apply in »when the compiler«.)

In the expression »i++ / 5«, it is /not/ the value of i that
is being divided by 5, but the value of »i++«. While the value
of »i« might be »11« at the time of division, the value of »i++«
is 10 (when i was 10 before the evaluation of »i++«).
 
J

John B. Matthews

I am seeing the omission of the determiner in noun clauses so often
in this newsgroup, that I would like to ask: Is »when compiler hits
...« correct English? I always feel annoyed when reading such a
wording, because I believe that only »When /the/ compiler hits ...«
is correct. (I also believe that the determiner sometimes may be
omitted, for example, I believe that »such a wording« might also be
written as »such wording«, but I believe that this does not apply in
»when the compiler«.)

You are correct. This article expands on the topic and suggests a
possible cause of what you observe:

<http://grammar.ccc.commnet.edu/grammar/determiners/determiners.htm>
 
A

Arne Vajhøj

Stefan said:
I am seeing the omission of the determiner in noun clauses so
often in this newsgroup, that I would like to ask: Is »when
compiler hits ...« correct English? I always feel annoyed when
reading such a wording, because I believe that only »When /the/
compiler hits ...« is correct. (I also believe that the
determiner sometimes may be omitted, for example, I believe
that »such a wording« might also be written as »such wording«,
but I believe that this does not apply in »when the compiler«.)

I am sure that your understanding of finer English syntax is
correct.

But this is an international newsgroup with plenty of participants
that are not native English speaking.

"when compiler hits" is one of those terms, that I think the
native English speaking (or Germans that are exceptionally
good at English) should learn to live with.

Arne
 
M

Mark Space

Arne said:
"when compiler hits" is one of those terms, that I think the
native English speaking (or Germans that are exceptionally
good at English) should learn to live with.

I agree that some tolerance for non-native speakers is needed. At least
it will improve the signal to noise ration in here. Endless pedantic
grammar corrections benefit no one.

OTOH, people should know how to use their shift key properly, and put
spaces after periods, not before. Stuff like that destroys readability.
It's one of my pet peeves.
 
R

Roedy Green

My question is : in the first answer they have said n is 0, now if we
go by code execution system from top to bottom and left to right, when
compiler hits % operator i has already become 11 (10 + 1 = 11) and
dividing it with 5 we will get 1 remainder, but there answer says n is
0 - why? It should be same as they have shown in answer of 2nd
question

see http://mindprod.com/jgloss/increment.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top