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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:35:20+00:00 2026-05-15T22:35:20+00:00

or, Declaring multiple variables in a for loop ist verboten ?! My original code

  • 0

or, “Declaring multiple variables in a for loop ist verboten” ?!

My original code was

 for( int i = 1, int i2 = 1; 
      i2 < mid;
      i++, i2 = i * i ) {

I wanted to loop through the first so-many squares, and wanted both the number and its square, and the stop condition depended on the square. This code seems to be the cleanest expression of intent, but it’s invalid. I can think of a dozen ways to work around this, so I’m not looking for the best alternative, but for a deeper understanding of why this is invalid. A bit of language lawyering, if you will.

I’m old enough to remember when you had to declare all your variables at the start of the function, so I appreciate
the

for( int i = 0; ....

syntax. Reading around it looks like you can only have one type declaration in the first section of a for() statement. So you can do

for( int i=0, j=0; ...

or even the slightly baroque

for( int i=0, *j=&i; ...

but not the to-me-sensible

for( int i=0, double x=0.0; ...

Does anyone know why? Is this a limitation of for()? Or a restriction on comma lists, like “the first element of a comma list may declare a type, but not the other? Are the following uses of commas distinct syntactical elements of C++?

(A)

for( int i=0, j=0; ...

(B)

int i = 0, j = 0;

(C)

 int z;
 z = 1, 3, 4;

Any gurus out there?

====================================================

Based on the good responses I’ve gotten, I think I can sharpen the question:

In a for statement

for( X; Y; Z;) {..... }

what are X, Y and Z?

My question was about C++, but I don’t have a great C++ refrence. In my C reference (Harbison and Steele 4th ed, 1995), they are all three expressions, and my gcc requires C99 mode to use for( int i = 0;

In Stroustrup, sec 6.3, the for statement syntax is given as

for( for-init-statement; condition; expression ) statements

So C++ has a special syntactic statement dedicated to the first clause in for(), and we can assume they have special rules beyond those for an expression. Does this sound valid?

  • 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-15T22:35:20+00:00Added an answer on May 15, 2026 at 10:35 pm

    int i = 1, double i2 = 0; is not a valid declaration statement, so it cannot be used inside the for statement. If the statement can’t stand alone outside the for, then it can’t be used inside the for statement.

    Edit:
    Regarding your questions about comma operators, options ‘A’ and ‘B’ are identical and are both valid. Option ‘C’ is also valid, but will probably not do what you would expect. z will be assigned 1, and the statements 3 and 4 don’t actually do anything (your compiler will probably warn you about “statements with no effect” and optimize them away).

    Update:
    To address the questions in your edit, here is how the C++ spec (Sec 6.5) defines for:

    for ( for-init-statement condition(opt) ; expression(opt) ) statement
    

    It further defines for-init-statement as either expression-statement or simple-declaration. Both condition and expression are optional.

    The for-init-statement can be anything that is a valid expression-statement (such as i = 0;) or simple-declaration (such as int i = 0;). The statement int i = 1, double i2 = 0; is not a valid simple-declaration according to the spec, so it is not valid to use with for. For reference, a simple-declaration is defined (in Section 7) as:

    attribute-specifier(opt) decl-specifier-seq(opt) init-declarator-list(opt) ;
    

    where decl-specifier-seq would be the data type plus keywords like static or extern and init-declarator-list would be a comma-separated list of declarators and their optional initializers. Attempting to put more than one data type in the same simple-declaration essentially places a decl-specifier-seq where the compiler expects a init-declarator-list. Seeing this element out of place causes the compiler to treat the line as ill-formed.

    The spec also notes that the for loop is equivalent to:

    {
        for-init-statement
        while ( condition ) {
            statement
            expression ;
        }
    }
    

    where condition defaults to “true” if it is omitted. Thinking about this “expanded” form may be helpful in determining whether a given syntax may be used with a for loop.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could POST to your server, which in a script,… May 16, 2026 at 11:51 am
  • Editorial Team
    Editorial Team added an answer http://github.com/mikenz/twitter-text-php i found the php version for the twitter text… May 16, 2026 at 11:51 am
  • Editorial Team
    Editorial Team added an answer Something like this: UPDATE TABLE SET ... FROM Table1, Table2… May 16, 2026 at 11:51 am

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

While declaring a simple UIAlertView, with the following code, I get the above mentionned
Cheers, I ran into this chunk of code in Programming Game AI by Example:
What are the performance hit or implications, if any, of: If I have multiple
I have a snippet of code, to calculate the width of some child items
I would like the below function to be more flexible and accept multiple callbacks
When declaring a class that inherits from a specific class: class C(dict): added_attribute =
I am declaring a Spring bean for a Java class that is used as
I am wondering at the difference between declaring a variable as volatile and always
In theory it seems that books sugest declaring ADT in C as: struct some_structure;
May I use variable to declaring cursors?? I want to creating dynamic cursor, how

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.