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

  • Home
  • SEARCH
  • 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 1060729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:22:16+00:00 2026-05-16T18:22:16+00:00

In a function, I have several consecutive for loops with the same code but

  • 0

In a function, I have several consecutive for loops with the same code but different initial values for the control variable. The initial values are obtained from inputs to the function. Ie,

void thisFunction( class A a){
  //some other code

  for (int i = a.x; i != 0; --i){
    code
  }

  for (int i = a.y; i != 0; --i){
    code
  }

  for (int i = a.z; i != 0; --i){
    code
  }

  //some other code
}

Is there any way to condense all the for loops into one loop so that when I make changes to the code within the loop, I won’t have to change it for all three loops? An alternative is to write anotherFunction() with the initial values as input, but I need access to local variables in thisFunction().

  void anotherFunction(int in){
      for (int i = in; i != 0; --i){
        code
      }
  }

So is there another way to condense the loops?

Thank you.

  • 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-16T18:22:18+00:00Added an answer on May 16, 2026 at 6:22 pm

    EDIT: In most cases you should avoid doing this, and rather follow MSalter‘s answer. Just because you can, doesn’t mean you should.

    I am not sure how good an idea this is, but without any more context, a simple solution could be:

    int starts[3] = { a.x, a.y, a.z };
    for ( int var = 0; var < 3; ++var ) {
       for ( int i = starts[var]; i != 0; --i ) {
          // code
       }
    }
    

    Note that the values of the conditions are obtained once at the beginning of the function, that means that if the object a changes throughout the first loop, that change will not be visible in the later control loops. If you need that, the solution can be modified to store pointers.

    EDIT: There is a comment suggesting the use of the size of the array. I did not add that here not to modify, but anyway, a better way of getting the array size is:

    template<typename T, unsigned int N>
    inline unsigned int array_size( T (&)[N] ) {
       return N;
    }
    //...
    for ( int var = 0; var < array_size(starts); ++i ) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a function to calculate several different values: int ASPfile::get_dimensions(int* Lat, int* Lon,
i have several classes that have the same function, the are called at different
I have several objects of different kinds (different function names, different signatures) and I
I have several jQuery function like function setOne(); setTwo(); setThree(); and a variable var
I have several blocks of code that follow this pattern: // Dummy function defs.
I have several widgets bound to the same function call. How do I tell
I have a function which takes several boolean template arguments: template<bool par1, bool par2,
I have a function that updates several tables. As an example, let's say it
I have a function which evaluates terms of a polynomial in several variables. The
I have been fruitlessly trying for several hours to make a function that filter

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.