Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38020 > unrolled thread
| Started by | Robert Spanjaard <spamtrap@arumes.com> |
|---|---|
| First post | 2018-02-14 17:01 +0100 |
| Last post | 2018-02-15 04:49 -0800 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
Can't use block of statements as 'incrementer' in for loop? Robert Spanjaard <spamtrap@arumes.com> - 2018-02-14 17:01 +0100
Re: Can't use block of statements as 'incrementer' in for loop? Arne Vajhøj <arne@vajhoej.dk> - 2018-02-14 11:13 -0500
Re: Can't use block of statements as 'incrementer' in for loop? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-02-14 11:34 -0500
Re: Can't use block of statements as 'incrementer' in for loop? Eric Douglas <e.d.programmer@gmail.com> - 2018-02-14 10:57 -0800
Re: Can't use block of statements as 'incrementer' in for loop? Robert Spanjaard <spamtrap@arumes.com> - 2018-02-15 12:05 +0100
Re: Can't use block of statements as 'incrementer' in for loop? Eric Douglas <e.d.programmer@gmail.com> - 2018-02-15 04:49 -0800
| From | Robert Spanjaard <spamtrap@arumes.com> |
|---|---|
| Date | 2018-02-14 17:01 +0100 |
| Subject | Can't use block of statements as 'incrementer' in for loop? |
| Message-ID | <p61mgq$10r6$1@gioia.aioe.org> |
When you create a for loop, you provide an initialisation statement, a
condition, and a continue statement. Usually this continue statement is an
incrementer or decrementer, but basically it can be any statement.
For instance, if you change Oracle's own tutorial example (*1)...
class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
}
}
...with this...
class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; System.out.println("haha")){
System.out.println("Count is: " + i);
i++;
}
}
}
... it still works.
Now, the same tutorial says "A block is a group of zero or more statements
between balanced braces and can be used anywhere a single statement is
allowed." (*2)
But when I replace the single haha-printing statement with any kind of
block, the compiler produces an error message:
ForDemo.java:3: error: illegal start of expression
for (int i=1; i<11; { i++; i++; }) {
^
I must say I tried this construction purely out of curiosity, remembering
Oracle's statement about blocks being allowed anywhere instead of a single
statement, so I don't have a practical use case for it. But still I'd like
to know why it doesn't work. So, can anyone of you shed some light on this
issue? Google search doesn't really help in this case.
*1
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
*2
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html
--
Robert Spanjaard
[toc] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2018-02-14 11:13 -0500 |
| Message-ID | <p61n7e$123l$1@gioia.aioe.org> |
| In reply to | #38020 |
On 2/14/2018 11:01 AM, Robert Spanjaard wrote:
> When you create a for loop, you provide an initialisation statement, a
> condition, and a continue statement. Usually this continue statement is
> an incrementer or decrementer, but basically it can be any statement.
>
> For instance, if you change Oracle's own tutorial example (*1)...
>
> class ForDemo {
> public static void main(String[] args){
> for(int i=1; i<11; i++){
> System.out.println("Count is: " + i);
> }
> }
> }
>
> ...with this...
>
> class ForDemo {
> public static void main(String[] args){
> for(int i=1; i<11; System.out.println("haha")){
> System.out.println("Count is: " + i);
> i++;
> }
> }
> }
>
> ... it still works.
>
> Now, the same tutorial says "A block is a group of zero or more
> statements between balanced braces and can be used anywhere a single
> statement is allowed." (*2)
>
> But when I replace the single haha-printing statement with any kind of
> block, the compiler produces an error message:
>
> ForDemo.java:3: error: illegal start of expression
> for (int i=1; i<11; { i++; i++; }) {
> ^
>
> I must say I tried this construction purely out of curiosity,
> remembering Oracle's statement about blocks being allowed anywhere
> instead of a single statement, so I don't have a practical use case for
> it. But still I'd like to know why it doesn't work. So, can anyone of
> you shed some light on this issue? Google search doesn't really help in
> this case.
I think the problem is that it is not a general statement.
JLS 14.4.1 says that it is a comma separated list of StatementExpression.
JLS 14.8 says that StatementExpression is one of:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression
Arne
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2018-02-14 11:34 -0500 |
| Message-ID | <p61ofi$e29$1@dont-email.me> |
| In reply to | #38020 |
On 2/14/2018 11:01 AM, Robert Spanjaard wrote:
> When you create a for loop, you provide an initialisation statement, a
> condition, and a continue statement. Usually this continue statement is
> an incrementer or decrementer, but basically it can be any statement.
"Expression," not "statement."
> [...]
> Now, the same tutorial says "A block is a group of zero or more
> statements between balanced braces and can be used anywhere a single
> statement is allowed." (*2)
>
> But when I replace the single haha-printing statement with any kind of
> block, the compiler produces an error message:
>
> ForDemo.java:3: error: illegal start of expression
> for (int i=1; i<11; { i++; i++; }) {
> ^
Right. The brace-enclosed stuff is a block, hence a statement,
but the `for' requires an expression -- not a statement -- in the
third spot.
--
esosman@comcast-dot-net.invalid
One thousand seventy-one days to go.
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2018-02-14 10:57 -0800 |
| Message-ID | <c4080c02-1eca-4401-9bcc-9c33572d02ef@googlegroups.com> |
| In reply to | #38020 |
On Wednesday, February 14, 2018 at 11:01:48 AM UTC-5, Robert Spanjaard wrote:
> https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html
>
> --
> Robert Spanjaard
That documentation says "... types of expressions can be made into a statement by terminating the expression with a semicolon"
as you see in the docs Arne refers to the 'ForUpdate' section may include multiple expressions separated by comma.
https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.14.1
So try like this:
public static void main(final String[] args) {
ArrayList<Integer> a1;
for (int i = 1, j; i < 11; j = 2, i++, System.out.println("Hello World!"), a1 = new ArrayList<>(), a1.add(j)) {
System.out.println("Count is: " + i);
}
}
[toc] | [prev] | [next] | [standalone]
| From | Robert Spanjaard <spamtrap@arumes.com> |
|---|---|
| Date | 2018-02-15 12:05 +0100 |
| Message-ID | <p63pgv$336$1@gioia.aioe.org> |
| In reply to | #38023 |
On 14-02-18 19:57, Eric Douglas wrote:
> On Wednesday, February 14, 2018 at 11:01:48 AM UTC-5, Robert Spanjaard wrote:
>> https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html
>
> That documentation says "... types of expressions can be made into a statement by terminating the expression with a semicolon"
>
> as you see in the docs Arne refers to the 'ForUpdate' section may include multiple expressions separated by comma.
> https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.14.1
>
> So try like this:
> public static void main(final String[] args) {
> ArrayList<Integer> a1;
> for (int i = 1, j; i < 11; j = 2, i++, System.out.println("Hello World!"), a1 = new ArrayList<>(), a1.add(j)) {
> System.out.println("Count is: " + i);
> }
> }
>
Yes, but that same doc lists System.out.println as an example of a statement.
But OK, I think Arne and you got me on the right track. Thanks.
--
Robert Spanjaard
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2018-02-15 04:49 -0800 |
| Message-ID | <1fc76053-569a-4a48-bfba-f5871e14dc1b@googlegroups.com> |
| In reply to | #38024 |
On Thursday, February 15, 2018 at 6:05:22 AM UTC-5, Robert Spanjaard wrote: > > Yes, but that same doc lists System.out.println as an example of a statement. > > But OK, I think Arne and you got me on the right track. Thanks. > > -- > Robert Spanjaard It is an expression. Put a semicolon on the end then it's a statement.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web