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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:29:52+00:00 2026-05-16T03:29:52+00:00

Possible Duplicate: In C++ why can’t I write a for() loop like this: for(

  • 0

Possible Duplicate:
In C++ why can’t I write a for() loop like this: for( int i = 1, double i2 = 0; …

A C developer would write this:

int myIndex;
for (myIndex=0;myIndex<10;++myIndex) ...

A C++ developer would write this to prevent the loop variable from leaking outside the loop:

for (int myIndex=0;myIndex<10;++myIndex) ...

However, if you have 2 loop variables, you cannot do this anymore. The following doesn’t compile:

for (int myIndex=0,MyElement *ptr=Pool->First;ptr;++myIndex,ptr=ptr->next) ...

The comma operator does not allow two variables to be defined this way, so we have to write it like this:

int myIndex;
MyElement *ptr;
for (myIndex=0,ptr=Pool->First;ptr;++myIndex,ptr=ptr->next) ...

Which defeats the advantage of having real loop-local variables.

A solution could be to put the whole construction between braces, like this:

{
int myIndex;
MyElement *ptr;
for (myIndex=0,ptr=Pool->First;ptr;++myIndex,ptr=ptr->next) ...
}

But this is hardly more elegant.

Isn’t there a better way of doing this in C++ (or C++0x)?

  • 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-16T03:29:52+00:00Added an answer on May 16, 2026 at 3:29 am

    You just have to understand the first statement is a declaration (and that comma is not the comma operator). It’s not any harder to do:

    for (int i, double d; ...)
    

    Than it is:

    int i, double d;
    

    Because for (init cond; expr) statement gets expanded to:

    {
        init
        while (cond)
        {
            statement
            expr;
        }
    }
    

    A trick is to make that init statement a struct definition and instance, like:

    for (struct { int myIndex; MyElement* ptr;} data = {0, Pool->First};
        data.ptr;
        ++data.myIndex, data.ptr = data.ptr->next)
        {
            // blah...
        }
    

    Which becomes the same as:

    {
        struct
        {
            int myIndex;
            MyElement* ptr;
        } data = {0, Pool->First};
    
        while (data.ptr)
        {
            {
                // blah...
            }
            ++data.myIndex, data.ptr = data.ptr->next;
        }
    }
    

    But I find that pretty ugly. In practice, I’d just split it up like you have. If scope is really a problem, which it probably isn’t, throw the extra braces around there.

    I don’t think there’s much to improve here without a bit of boilerplate code.

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

Sidebar

Ask A Question

Stats

  • Questions 523k
  • Answers 523k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If the field is fixed width storing NULL takes the… May 16, 2026 at 9:36 pm
  • Editorial Team
    Editorial Team added an answer The OP changed their question, so here is my new… May 16, 2026 at 9:36 pm
  • Editorial Team
    Editorial Team added an answer The WPF layout system doesn't use absolute positioning, unless you're… May 16, 2026 at 9:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Possible Duplicate: Why can’t I catch a generic exception in C#? I have been
Possible Duplicate: What’s the reason I can’t create generic array types in Java? HashSet<Integer>[]
Possible Duplicate: Should I use uint in C# for values that can’t be negative?
Possible Duplicate: How can I configure Vim to compile C code using Borland’s compiler
Possible Duplicate: Which sorting algorithm is used by STL’s list::sort()? Which sorting algorithm can
Possible Duplicate: What’s your choice for your next ASP.NET project: WebForms or MVC? Can
Possible Duplicate: Where does ‘Hello world’ come from? Hello world! is the most commonly
Possible Duplicates: implementing a compiler in “itself” Bootstrapping a language How can you write
Possible Duplicate: Browser’s Default CSS Is there a list of the default html elements
Possible Duplicate: Can you call Directory.GetFiles() with multiple filters? How do you filter on

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.