logotransparent.png

Legion of Learners

www.lol-101.com

  • Home

  • Classrooms

  • Courses

  • About Us

    • Executive Team
    • Board Members
  • Resources

  • More

    Use tab to navigate through the menu items.
    To see this working, head to your live site.
    • Categories
    • All Posts
    • My Posts
    William Li
    Jan 27

    1/26 Enhanced For Loops

    in Introduction to Java

    Today, we learned about:


    - Enhanced for loops

    These allow you to traverse an array without using an increment variable.

    For example:

    for (int i : myIntArr) {
        System.out.println(i); //prints out each int in the array in order
    }

    this would be equivalent to:

    for (int i = 0; i < myIntArr.length, i++) {
        System.out.println(myIntArr[i]);
    }

    note that in the first example, "i" is set to the actual value at that index in the array, while in the second example using a normal for loop i is the index at which an int is stored.

    The enhanced for loop only gets data, so you can't use it to set elements in an array. Nor can you traverse the array in any other order.


    We then practiced using enhanced for loops in some practice problems.

    1 comment
    0
    William Li
    Jan 27

    Here's the homework: https://docs.google.com/forms/d/e/1FAIpQLSfngsy3FoTryUv8K1QysFH15t3IaGX808KxP6lTUQO0rd1-uA/viewform?usp=sf_link

    0
    1 comments

    Questions? Email us at legionoflearners@gmail.com or join our WeChat group!

    • lol-101dotcom

    ©2020 Legion of Learners