Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6097383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:02:13+00:00 2026-05-23T13:02:13+00:00

In this question,I’m asking how the following snippets work, as it involves weird use

  • 0

In this question,I’m asking how the following snippets work, as it involves weird use of variable:

while (+(+i--)!=0)
{
    i-=i++;
}
console.log(i);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:02:13+00:00Added an answer on May 23, 2026 at 1:02 pm

    Interesting problem… you’ve tagged it Java, JavaScript and C — note that while these languages have the same syntax, this question involves very subtle semantics that may (I’m not sure) differ across languages.

    Let’s break it down:

    +(+i--)
    

    The -- postfix decrement operator is most tightly bound. So this is equivalent to +(+(i--)). That is therefore equivalent to the value of +(+i) (that is, i), but it also decrements i after taking the value. It compares the value with 0 to see if the loop should continue. Thus, while (+(+i--)!=0) is equivalent to the following:

    while (i-- != 0)
    

    Note that it also performs i-- at the end of the loop.

    Inside the loop, I believe you have some undefined behaviour, at least in C, because you are referencing i on the right, and also updating i on the left — I believe that C doesn’t define which order to do that in. So your results will probably vary from compiler to compiler. Java, at least, is consistent, so I’ll give the Java answer. i-=i++ is equivalent i = i - i++, which is equivalent to to reading all the values out of the expression, computing the result of the expression, applying the post-increment, and then assigning the result back. That is:

    int t = i - i;    // Calculate the result of the expression "i - i++"
    i++;              // Post-increment i
    i = t;            // Store the result back
    

    Clearly, this is the same as writing i = 0. So the body of the loop sets i to 0.

    Thus, the loop executes just one time, setting i to 0. Then, it decrements i one more time on the next while loop, but fails the check because i (before decrementing) == 0.

    Hence, the final answer is -1, no matter what the initial value for i is.

    To put this all together and write an equivalent program:

    while (i-- != 0)
    {
        int t = i - i;
        i++;
        i = t;
    }
    console.log(i);
    

    When I tried it in Java and JavaScript, that’s what I got. For GCC (C compiler), it gives -1 only when i starts out as 0. If i starts out as anything else, it goes into an infinite loop.

    That’s because in GCC (not necessarily all C compilers), i-=i++ has a different meaning: it does the store back to i first, then does the post-increment. Therefore, it is equivalent to this:

    int t = i - i;    // Calculate the result of the expression "i - i++"
    i = t;            // Store the result back
    i++;              // Post-increment i
    

    That’s equivalent to writing i = 1. Therefore, on the first iteration, it sets i to 1, and then on the loop, it checks whether i == 0, and it isn’t, so it goes around again, always setting i to 1. This will never terminate, but for the special case where i starts out as 0; then it will always terminate the loop and decrement i (so you get -1).

    Another C compiler may choose to act like Java instead. This shows that you should never write code which both assigns and postincrements the same variable: you never know what will happen!

    Edit: I tried to be too clever using that for loop; that wasn’t equivalent. Turned back into a while loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is the other side of the question asking, How do I calculate
This question borders between the world of the audio designer and the programmer. While
This question is related to this question. The following code compiles fine VC9 compiler
This question was inspired by one I asked almost a year ago - any-orms-that-work-with-ms-access-for-prototyping
This question is more out of curiosity than a real problem. Consider the following
This question is kind of an add-on to this question In C#, a switch
This question and answer shows how to send a file as a byte array
This question comes on the heels of the question asked here . The email
This question would probably apply equally as well to other languages with C-like multi-line
This question is a follow up to my previous question about getting the HTML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.