Results 1 to 3 of 3

Thread: For loop

  1. #1
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default For loop

    I was doing some search on for loops and i bumped on this tiny code which is confusing to understand. below is the code



    class Ex

    {


    public static void main(String[] args)

    {

    int i;

    for(i=0, System.out.println(i); ; i++)

    {

    }

    }

    }

    Now the above code runs fine. But when i declare the entire variable in inside the for loop like "for(int i=0, System.out.println(i); ; i++)" it throws error " ; expected" .

    Now can any kindly explain me the difference?

    Thank you

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: For loop

    Read paragraph 14.14.1. in the JLS: the for-initialization part can either be a local variable list declaration or a list of expression statements; they can't be combined.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  4. Replies: 5
    Last Post: 04-30-2010, 05:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •