Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38020

Can't use block of statements as 'incrementer' in for loop?

Path csiph.com!aioe.org!.POSTED!not-for-mail
From Robert Spanjaard <spamtrap@arumes.com>
Newsgroups comp.lang.java.programmer
Subject Can't use block of statements as 'incrementer' in for loop?
Date Wed, 14 Feb 2018 17:01:30 +0100
Organization Aioe.org NNTP Server
Lines 51
Message-ID <p61mgq$10r6$1@gioia.aioe.org> (permalink)
NNTP-Posting-Host bL5gwq6hANUN7rOnAkKsyA.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0
X-Notice Filtered by postfilter v. 0.8.3
X-Mozilla-News-Host news://news.aioe.org:119
Content-Language en-US
Xref csiph.com comp.lang.java.programmer:38020

Show key headers only | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

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

csiph-web