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 8586553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:20:13+00:00 2026-06-11T22:20:13+00:00

I am very curious to learn why the below code does not run in

  • 0

I am very curious to learn why the below code does not run in a continuous loop. And I’m also looking for some ways to achieve what I want to achieve–which is resetting a loop inside of the loop. I need to do this because I need to account for each element in a container. The reason why this is because I might start off in the middle, and need to loop back around to check the others / and need to recheck other information too. So on with my little test example:

for ( int i = 0; i != 10; i++ ) {
    std::cout << std::endl << "TEST: " << i << std::endl;
    if ( i++ == 10 ) {
        i = 0;
    } else {
        i--;
    }
}

Is there any particular reason why the above does not work? I am very interested in knowing why, so I can learn how everything works. This also leads into a much bigger problem I am facing. Which is the below code. I am using MSVC++ 2010 Express. Also, this is one thread, so other data is not accessing it. It is an unordered_map using STL. its size if 2 (i checked).

for (game_player_client_map::const_iterator it = gpc_map_ptr->begin(); it != gpc_map_ptr->end(); ++it) {
    if ( it++ == gpc_map_ptr->end() ) {
        cout << endl << "IT == gpc_map_ptr->end()" << endl;
        it = gpc_map_ptr->begin();
    } else {
        it--;
    }
}

I appreciate any feedback SO has to offer, and any new things to learn 🙂 If further information is needed I will provide. Thank you for your time.

  • 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-06-11T22:20:14+00:00Added an answer on June 11, 2026 at 10:20 pm

    Because the condition is checked before the body of the loop is entered. When i == 10, the loop is broken, before your code can execute at the time that i++ would evaluate to 10.

    Remember that postincrement increments the variable and returns the old value. So if i is 9, i++ evaluates to 9 also, but the next time you use i, it will be 10.

    If you want the variable to be incremented and use the new value in an expression, use preincrement:

    if (++i == 10) // changes i to i + 1 and checks if the new value of i is 10
    

    You could completely ditch the increment however, and just use i + 1. That way you don’t have to de-increment i in the else block.

    Your misunderstanding of postincrement is probably also the source of the bug in the second block of code you posted. You can change it to preincrement, or if it is a random-access iterator, you can do the same thing as mentioned above and check if it + 1 == gpc_map_ptr->end() and not have to de-increment it in the else block.

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

Sidebar

Related Questions

I'm very curious looking for an explanation why the following code isn't allowed in
I can't find if it is or not and am very curious - if
I am very curious to know how to implement the Ctrl + K (code
I'm very curious, why stability is or is not important in sorting algorithms?
We're having a very curious problem with hsqldb. We are running some tests which
I'm very curious - why does UITextInputDelegate have all its methods required? Why can't
This is mostly a theoretical question I'm just very curious about. (I'm not trying
I just learned about comet pattern(Long polling) and very curious to implement a simple
I've always been curious: how can I perform arithmetic operations on very long decimals--for
Very simple + silly question: Does clojure provide multi maps? I currently have something

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.