0

I really don't understand why this wont work...

    for (int i = 1, int k = 2; i<=4000000; i++) {

    }

While this will...

    for (int i = 1; i<=4000000; i++) {

    }

I need to initialize two variables but when I try the first code it wont compile, the code looks correct??

1
  • Basically I was using declaring each variable as an int individually which wont complile... Commented Feb 8, 2014 at 14:27

4 Answers 4

3

If you want to initialize multiple variables, then they need to be of the same type, and you have to use just one declaration, so it would look like this:

for (int i = 1, k = 2; i<=4000000; i++ ) {

}

I wrote a bit about the syntax of for loops in response to Types permitted in for loop variable declarations? This isn't quite the same question, but the answer makes sense here, too I think.

In general, you can have a look at the Java language specification for the for statement. You can declare and initialize any type of variable in a for loop, and can even declare multiple variables, so long as they're all the same type. The relevant productions in the grammar are:

BasicForStatement:
    for ( ForInitopt ; Expressionopt ; ForUpdateopt ) Statement

ForInit:
    StatementExpressionList
    LocalVariableDeclaration

LocalVariableDeclaration:
    VariableModifiersopt Type VariableDeclarators

VariableDeclarators:
    VariableDeclarator
    VariableDeclarators , VariableDeclarator

This means that you can do any of the following, e.g.,

for ( ; … ; … )                         // no variable declaration at all
for ( int i; … ; … )                    // variable declaration with no initial value
for ( int i=0; … ; … )                  // variable declaration with initial value
for ( int i=0, j=1; … ; … )             // multiple variables
for ( final Iterator<T> it = …; … ; … ) // final variable

The fourth case is the one that you're concerned with at the moment.

As an aside, the ForInit can also be a StatementExpressionList, which means that instead of declaring and initializing variables, you can also just execute some statements. E.g, you could do this (but this isn't a particularly useful example):

for ( System.out.println( "beginning loop" ; … ; … )
Sign up to request clarification or add additional context in comments.

Comments

1

Change as for (int i = 1, k = 2; i<=4000000; i++) {

From Doc

The general form of the for statement can be expressed as follows:

for (initialization; termination; increment) {
    statement(s)
}
  • The initialization expression initializes the loop; it's executed once, as the loop begins.
  • When the termination expression evaluates to false, the loop terminates.
  • The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

You can initialize as int i=1,k=2;

Comments

0
for (int i = 1, k = 2; i<=4000000; i++) {

Comments

-1

2 consecutive initializations should be semi-colon separated like

int a; int b;

In the above case you've have separated them using a comma and so it gives a compilation error.
Regards.

1 Comment

No. Your answer isn't correct for the initialiser of an if block - see the other answers. Somebody has flagged your answer as either 'not an answer', or 'very low quality', and I am reviewing that flag in a review queue. You are attempting to answer the question, so even though I think your answer is wrong, I won't recommend it is deleted in my role as a reviewer, because a downvote is the right course of action here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.