Tuesday, January 10, 2012

JAVA Programming : For Each Loop

JAVA For Each Loop


For each loop or Enhance For loop is a extended version of basic for loop which was introduced in Java 5. For each loop is designed to make iteration over arrays and collections much easier. For each loop also makes the code more readable. In this post we will learn about basics of For each loop using  Arrays. We will discuses more about For Each loop when we learn about Collections and Iterator.

Syntax of For Each Loop:-

        for (type var : arr) {  
        //body-of-loop  
    }

Example of For Each Loop:- This is a very simple example in which we will traverse an array using For Each loop and print all the elements of the array.


public class ForLoop {
  public static void main(String[] args) {
    char[] charArr = {'a','b','c','d','e'};    
    for (char c : charArr) {
      System.out.println(c);
    }
  }
}



To understand the difference between Basic For loop and For Each Loop, here is the same program using For Loop.


public class ForLoop {
  public static void main(String[] args) {
    char[] charArr = {'a','b','c','d','e'};    
    for (int i = 0; i < charArr.length; i++) {
      System.out.println(charArr[i]);
    }
  }
}



Advantages of For Each Loop:-
  • Less number of variables are used
  • Less chance of errors
  • Improve Readability 
Limitation of For Each Loop:- 
  • Cannot traverse multiple collection at once.
  • Iterates only forward by single steps.


12 comments:

  1. thanq....................

    ReplyDelete
    Replies
    1. I am technology Enthusiast. Your blog is really awesome, attractive and impressive. I like the way you think. it is very useful for Java SE & Java EE Learners. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students. or learn thru JavaScript Online Training from India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

      Delete
  2. Awesome article.

    Feel free to surf to my web blog - Go At this site

    ReplyDelete
  3. Good day! Do you use Twitter? I'd like to follow you if that would be okay. I'm absolutely enjoying your blog and look forward to new updates.


    Here is my webpage; www.hssattledt.at

    ReplyDelete
  4. Eveгything is νеry oρen ωith a clеar clarificatіon of thе issues.

    It wаs really infοrmatiνe. Youг sіte іs very hеlpful.
    Thanks for shaгing!

    Herе is my hоmepаge ... stock options training

    ReplyDelete
  5. Amazіng! Тhіs blog looks exactlу liκe my old
    onе! It's on a completely different subject but it has pretty much the same page layout and design. Wonderful choice of colors!

    Here is my blog post - sources

    ReplyDelete
  6. Simply wish to say your article is as astonishing. The clearness in your post is simply cool and i could assume you're an expert on this subject. Fine with your permission let me to grab your feed to keep up to date with forthcoming post. Thanks a million and please keep up the gratifying work.

    my blog; http://menshealthfortodaysman.blogspot.com

    ReplyDelete
  7. I've been browsing online more than 2 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my view, if all website owners and bloggers made good content as you did, the internet will be a lot more useful than ever before.

    Feel free to surf to my blog post - Best Male Enhancement

    ReplyDelete
  8. Рlease let me know if yоu're looking for a writer for your weblog. You have some really good articles and I feel I would be a good asset. If you ever want to take some of the load off, I'd abѕolutely love tο writе sоme articles for youг blog in exchange for
    а link bacκ tο mine. Pleaѕe shoot me an e-mail if intеrеѕted.
    Thankѕ!

    my website ... Discover More Here

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  9. Nice Example http://javademos.blogspot.com

    ReplyDelete