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

Thread: How to Use different Java loops

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Post How to Use different Java loops

    Here is a quick example showing you how to use different loop types in Java.

    For loop

    public class JavaLoops {
     
        /**
         * JavaProgrammingForums.com 
         */    
        public static void main(String[] args) {
     
            // For loop example
            for(int a = 0; a < 10; a++){
                System.out.println("This is a for loop");
            }        
        }
    }
    Example output:

     

    This is a for loop
    This is a for loop
    This is a for loop
    This is a for loop
    This is a for loop
    This is a for loop
    This is a for loop
    This is a for loop
    This is a for loop
    This is a for loop


    For Each loop

    public class JavaLoops {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
     
            String[] myString = {"Java", "Programming", "Forums", ".com"};
     
            // For each loop
            for(String s : myString){
                System.out.print(s);
            }
        }
    }
    Example output:

     

    JavaProgrammingForums.com


    Do While loop

    public class JavaLoops {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
     
            // Do While loop
            int count = 1;
     
            do {
                System.out.println("Count is: " + count);
                count++;
            } while (count <= 10);
        }
    }
    Example output:

     

    Count is: 1
    Count is: 2
    Count is: 3
    Count is: 4
    Count is: 5
    Count is: 6
    Count is: 7
    Count is: 8
    Count is: 9
    Count is: 10


    While loop

    public class JavaLoops {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
     
            // While loop
            while(true){
                System.out.println("I'm looping forever");
            }        
        }
    }
    Example output. Note that this loop will keep going forever.

     

    I'm looping forever
    I'm looping forever
    I'm looping forever
    I'm looping forever
    I'm looping forever
    I'm looping forever
    I'm looping forever
    I'm looping forever
    I'm looping forever
    I'm looping forever
    ....


    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to Use different Java loops

    For Each loop on a Map

    public class JavaLoops {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
            final Map<String, String> stringMap = new HashMap<String, String>();
            stringMap.put("key1", "value1");
            stringMap.put("key2", "value2");
            stringMap.put("key3", "value3");
     
            for(final Map.Entry<String, String> entry : stringMap.entrySet()) {
                System.out.println(entry.getKey() + " = " + entry.getValue());
            }
        }
    }


    Example output

     

    key1 = value1
    key2 = value2
    key3 = value3




    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    Freaky Chris (August 28th, 2009)

Similar Threads

  1. Breaking out of multiple loops
    By helloworld922 in forum Loops & Control Statements
    Replies: 4
    Last Post: July 1st, 2009, 02:52 PM
  2. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM
  3. [SOLVED] Fixing of bug in number guessing game
    By big_c in forum Loops & Control Statements
    Replies: 6
    Last Post: April 16th, 2009, 02:15 AM

Tags for this Thread