What can be done in parentheses of the Java for loop?

My question is about a Java instruction for, for example.

for (int i = 0; i < 10; ++i) {/* stuff */}

What I do not understand is exactly how much code / which code I can put in parentheses (i.e. where I have int i = 0; i < 10; ++iin my example) - I do not quite understand the language used to describe it here:

http://java.sun.com/docs/books/jls/third_edition/html/statements.html#24588

Basically my question comes down to a request for a bit translation in a specification that looks like this:

ForInit: StatementExpressionList LocalVariableDeclaration

EDIT: Wow. I suppose the real answer is "learn to read and understand the notation used in JLS - it is used for some reason." Thanks for all the answers.

+5
source share
13 answers

Both StatementExpressionListand LocalVariableDeclarationare defined elsewhere on the page. I will copy them here:

StatementExpressionList:
        StatementExpression
        StatementExpressionList , StatementExpression

StatementExpression:
        Assignment
        PreIncrementExpression
        PreDecrementExpression
        PostIncrementExpression
        PostDecrementExpression
        MethodInvocation
        ClassInstanceCreationExpression

and

LocalVariableDeclaration:
        VariableModifiers Type VariableDeclarators

VariableDeclarators:
        VariableDeclarator
        VariableDeclarators , VariableDeclarator

VariableDeclarator:
        VariableDeclaratorId
        VariableDeclaratorId = VariableInitializer

VariableDeclaratorId:
        Identifier
        VariableDeclaratorId [ ]

VariableInitializer:
        Expression
        ArrayInitializer

There is no longer any sense in following grammar; I hope it will be easy enough to read as it is.

This means that you can have any number StatementExpressions, separated by commas or LocalVariableDeclarationin a section ForInit. And it LocalVariableDeclarationcan consist of any number of pairs " variable = value", separated by commas, which are preceded by their type.

So this is legal:

for (int i = 0, j = 0, k = 0;;) { }

because " int i = 0, j = 0, k = 0" is valid LocalVariableDeclaration. And this is legal:

int i = 0;
String str = "Hello";
for (str = "hi", i++, ++i, sayHello(), new MyClass();;) { }

because all these random things in the initializer qualify as StatementExpressions.

And since it is StatementExpressionListallowed in terms of updating the for loop, this is also true:

int i = 0;
String str = "Hello";
for (;;str = "hi", i++, ++i, sayHello(), new MyClass()) { }

?

+16

: , .

for (int i = 0; i < n; i++)

. - ( n, n-1, n + 1? It > 0, >= 0,...?)

, . - , , .

+6

, FOR:

for (startCondition; endCondition; increment)

startCondition, , ; i

int i = 0

endCondition , . , , = 15,

i <= 15

i != 15

, ,

i == true

, , . 1,

i++

startCondition endCondition, , FOR.

+3

StatementExpressionList

, :

int x = 0; 
int y = 0;
int z = 0;


for ( x++, y++; x+ y < z; z++) {
// whatever
}

x ++, y ++ - . , .

LocalVariableDeclaration

int j = 0, i = 0, k = 0;

for ( int j = 0, i = 0, k = 0; i+j+k < 10; i++, j++, k++ ){
// whatever
}
+3

for () , . - i < max, hasMore(), (x > y) == bool - myclient.getMapOfIntsToBools().get(i). , , .

, , : , , ( ). , , .

:

if (A; B; C){
  ...
}

:

A
while (B){
  ...
  C
}

A C , , ( ). B -, .

+2

for(stmt a,stmt b, stmt,c.... ; stmt 1; alpha, beta, omega,...){}

stmt: statement;

a, b, c ..

for(int a=0,b=0;;){}

a, b 0;

for(a=i,b=a+1;;){}

a b

 for(Iterator it= my_list.iterator();;){}

1, 2, 3 .. ,

-

 for (;a++;)

for (;a<b;)

for(;it.hasNext();)

while.

:)

 for(int a=0;;a++)

alpha, omega.., -

for(;;a){} 

,

for(;;a++){}
for(;;a=b;a++){}

.

foreach,

for (A a:getAList())

, . .

+1

BasicForStatement:         for (ForInitopt; Expressionopt; ForUpdateopt)

for ForInit, Expression ForUpdate,

ForInit:         StatementExpressionList         LocalVariableDeclaration A forInit - , (. )

ForUpdate:         StatementExpressionList

A forUpdate - , ... :       
      PreIncrementExpression       PreDecrementExpression       PostIncrementExpression       PostDecrementExpression       MethodInvocation       ClassInstanceCreationExpression

StatementExpressionList:         StatementExpression         StatementExpressionList, StatementExpression

:

,       .

; . S U, T S U , S T T U. , .

, , , .

, java ( ), ...

+1
for(startCondition(s); endCondition(s); Action) {}

startCondition (s) endCondition (s) booleans (true false, 1 0 ), "" .

0

; , , ,

0

for (initFor; endCondition; update)

startCondition - , ( ). LocalVariableDeclaration, , (, int x = 0), "" for ( for).

endCondition . , ( )

update - , , .

0

, . : , . , break. , Iterator.

EDIT: , . .

0

for , .

for , . , ( ), , for (, int = 1, j = 2). for ( , ).

for - , . , for.

for , , - .

0

You may find that for each notation it is often simpler and more understandable. I would suggest using for each cycle whenever it is appropriate. eg.

for(Element element: arrayOfElement) { /* stuff */ }
// or
for(Element element: iterableOfElement) { /* stuff */ }

The collection, set and list is Iterable, but you can make your own.

0
source

All Articles