Question ?

Discussion in 'Java' started by Vasu, Oct 18, 2008.

  1. Vasu

    Vasu Guest

    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
     
    Vasu, Oct 18, 2008
    #1
    1. Advertising

  2. Vasu wrote:
    > 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.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Oct 18, 2008
    #2
    1. Advertising

  3. Vasu

    Stefan Ram Guest

    Vasu <> writes:
    >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++«).
     
    Stefan Ram, Oct 18, 2008
    #3
  4. In article <-berlin.de>,
    -berlin.de (Stefan Ram) wrote:

    > 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>

    --
    John B. Matthews
    trashgod at gmail dot com
    http://home.roadrunner.com/~jbmatthews/
     
    John B. Matthews, Oct 18, 2008
    #4
  5. Vasu

    Arne Vajhøj Guest

    Stefan Ram wrote:
    > Vasu <> writes:
    >> 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«.)


    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
     
    Arne Vajhøj, Oct 18, 2008
    #5
  6. Vasu

    Mark Space Guest

    Arne Vajhøj wrote:

    > "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.
     
    Mark Space, Oct 18, 2008
    #6
  7. Vasu

    Roedy Green Guest

    On Sat, 18 Oct 2008 07:37:27 -0700 (PDT), Vasu
    <> wrote, quoted or indirectly quoted
    someone who 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


    see http://mindprod.com/jgloss/increment.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The Canadian national animal should be changed from the beaver to the ostrich.
    Canadians elected a party that denies global warming so they too could pretend it presents no danger.
     
    Roedy Green, Oct 19, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    597
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    662
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    950
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    426
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    854
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page