Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: What can be legally inside FOR statement?

  1. #1
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default What can be legally inside FOR statement?

    Hi guys

    I have a question for how many ways to use FOR statement?
    I thought FOR is only used in starting a loop, like below two forms:
    for (initialization; termination; increment) or For each loop(Type item : lstItem)
    But today I saw FOR is used in this statement:
    for (Vertex vertex = target; vertex != null; vertex = vertex.previous)
    This format is not loop at all, what is it? I am wondering if there is anything else that can be put inside () after FOR?
    Can any explain this?

    Many thanks
    Last edited by ice; January 11th, 2011 at 09:41 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What can be legally inside FOR statement?

    A for loop must have this syntax (using regex notation where ? means it's optional):
    for(expression?; condition?; expression?)
    statement

    where expression is any valid single expression
    condition is any expression which evaluates to true or false. Note that if no condition is given, it is automatically evaluated to be true.
    and statement is any single statement (either a block statement or a line statement)

    // all of these are valid. However, whether you should be using for loops like this is debatable
    for(int i = 0; i < 100; ++i)
    {}
     
    StringBuilder b = new StringBuilder();
    for(Scanner reader = new Scanner(System.in()); reader.hasNextLine(); b.append(reader.nextLine()));
     
    for(;;) // infinite loop which does nothing
    {}
     
    for(;false;); // does nothing. Note that the compiler will likely complain about "unreachable code"

    There are also for-each loops. These have a slightly different syntax.
    for(type var_name : {Iterable<type>_var | type[]})
    statement
    where type is any valid Java type
    var_name is the name of the variable to use inside the for-each loop
    Iterable<type>_var is any expression which evaluates to an Iterable of type type
    type[] is any expression which evaluates to an array of type type
    statement has the same definition as a regular for-loop

    valid constructs of for-each loops:
    int ints[] = {1,2,3};
    ArrayList<String> strings = new ArrayList<String>();
    for(int i : ints)
    {}
     
    for(String s : strings) // ArrayList implements Iterable
    {}
    Last edited by helloworld922; January 11th, 2011 at 08:53 PM.

  3. #3
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: What can be legally inside FOR statement?

    I see....Thanks a lot for the explanation.
    so
    for(expression?; condition?; expression?)
    statement
    is still a kind of loop, but doesn't statement need to be wrapped inside a pair of {}? (otherwise how could it know which statement is the thing to do during the above loop?)

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What can be legally inside FOR statement?

    No, a statement can be a line statement or a block statement.

    This is perfectly fine code and will print out 0 to 5. However, it is generally considered good practice to wrap statements after control structures (if/else, while, do/while, switch, for) with {}

    for(int i = 0; i <= 5; ++i)
         System.out.println(i);

  5. #5
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: What can be legally inside FOR statement?

    ok, what about there are many lines of statements under for(int i = 0; i <= 5; ++i), EG.:
    for(int i = 0; i <= 5; ++i)
    System.out.println(i); // statements A
    ........................; //statements B
    ................; //statements  C
    ............; // statements D
    will those four statements from A to B be executed during the loop?

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What can be legally inside FOR statement?

    No, the parser will take only 1 statement (statement A). The other statements are considered outside the for-loop scope, and thus are not executed as part of the for loop.

    That's be similar to doing something like this:

    for(int i = 0; i <= 5; ++i)
    {
        System.out.println(i);
    }// the whole block is statement A
    {
        System.out.println("this is statement B");
    }// the whole block is statement B
    {
        System.out.println("this is statement C");
    }// the whole block is statement C

    Here, again only statement A (which happens to be a block) gets executed in the for loop.

    A semi-formal definition of statement:
    statement = (expression ";") | ("{" statement* "}")

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    ice (January 18th, 2011)

  8. #7
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: What can be legally inside FOR statement?

    "Necessity is the mother of Invention" or " Need is mother of Inveiton".

    So u can create varied different ways of using for loop or any other loop.

    No. of our frnds explained you various ways of using for loop.
    But the initial thing is


    for(expression?; condition?; expression?)
    {
    statements...
    }

    and i will appreciate if u will do R&d on this and found some other ways too.

    Now i m adding one for you.

    We can also use for like:

    for(int i=0 ; i<100;i++);
    System.out.println(i);

    Please run and feel difference between the above and below code

    for(int i=0 ; i<100;i++)
    System.out.println(i);



    Best of luck
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  9. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What can be legally inside FOR statement?

    for(int i=0 ; i<100;i++);
    System.out.println(i);
    This is the same as:
    for(int i=0 ; i<100;i++)
        ;
    System.out.println(i);
    An empty semi-colon counts as a statement. So the for-loop statement is the empty statement, and System.out.println() executes after the for loop (though it would fail because i was declared in the for-loop scope and invalid outside the for-loop). It's considered an empty statement (because it does nothing). Though I did miss that in my definition of statement.

    new definition which allows for empty statements:
    statement = (expression? ";") | ("{" statement* "}")

    So, following the for-loop construct (added quotes to clarify definition a bit):
    "for" "(" expression? ";" condition? ";" expression? ")"
    statement

Similar Threads

  1. painting Image on JPanel inside a JScrollPane
    By becca23 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2010, 07:28 PM
  2. suggestionbox inside modal panel
    By smackdown90 in forum Web Frameworks
    Replies: 1
    Last Post: April 8th, 2010, 01:16 PM
  3. [SOLVED] Passing arrayList inside class
    By KrisTheSavage in forum Collections and Generics
    Replies: 1
    Last Post: March 27th, 2010, 12:45 PM
  4. centering a label inside a rectangle
    By Brain_Child in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2009, 09:08 AM
  5. count components inside a JPanel
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 2nd, 2009, 03:17 PM