Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do you use for/in loops on Java?

    Can someone just explain how to use for/in loops thoroughly and use some examples? New to programming.

    Thank You!

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Did you want to know how to do it using JavaScript (as per the forum you posted in) or Java (as per your title) - two totally different languages.

    In JavaScript you use it to iterate over all the enumerable properties and methods on an object.

    So if you have defined an object with three properties:

    Code:
    var x = {a:1, b:2, c:3};
    then you can use a for-in loop to process those properties.

    Code:
    for (i in x) {console.log(i);}
    which would log the values 1, 2, and 3 respectively (of course you'd substitute what you really want to do with x.a, x.b and x.c (or whetever your object and properties are called) for the console.log call inside the loop.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by mothdragon123 View Post
    Can someone just explain how to use for/in loops thoroughly and use some examples? New to programming.

    Thank You!
    Google is your friend!

    Exploring JavaScript for-in loops | JavaScript, JavaScript...
    Loop through array in JavaScript - Stack Overflow

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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