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 1 of 1

Thread: For loop tricks

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

    Default For loop tricks

    Here are some for-loop tricks you can do. Some of them are quite useful, and some of them are bad programming practice, but perfectly legal to the compiler.

    Here's the general layout for a for loop:
    for (init;conditions;increment)

    The for loop works like this:

    a. Run init
    b. Check the condition. If true, run the for-loop code. Else, stop
    c. Run increment. Repeat b & c until stop

    1. You don't need to have anything in any of init,conditions, or increment spots, or you can have any combination of used/unused spots. Note that if the condition block is empty, it will automatically assume true.

    for(;;)
     
    for (int i = 0;true;)

    2. You can put valid single statement in any of the slots. Note that the conditions slot MUST evaluate to a boolean, or be empty. Code Blocks are not allowed.

    for (System.out.println("Initializing");true; System.out.println("Incrementing"))
     
    for (int i = 0, j = 0; i < 5 && j < 5; i++)

    3. You can create a "for-each" loop on object types that implement the Iterator interface. Note however, if you do choose to use this method, you have absolutely no access to the location of that object within the original collection (mostly when working with arrays, or collections implemented with some sort of indices system)

    int[] a = new int[5];
    for (int x :a){}
    Last edited by helloworld922; October 5th, 2009 at 12:47 AM.


Similar Threads

  1. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM
  2. [SOLVED] loop , passed and failed
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: August 25th, 2009, 07:00 AM
  3. JAVA for loop
    By tazjaime in forum Loops & Control Statements
    Replies: 2
    Last Post: August 18th, 2009, 07:43 PM
  4. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM
  5. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM