www.webdeveloper.com
Results 1 to 5 of 5

Thread: for loops

  1. #1
    Join Date
    Oct 2004
    Posts
    14

    for loops

    A for loop is usually written as
    for(i =0; i<10; i++)
    However, instead of i++ I want to go up in increments of 20000, and my program wont compile when I write i + 20000, does anyone know how you do this??
    Thanks very much!

  2. #2
    Join Date
    May 2003
    Location
    O!
    Posts
    200
    Try using i+=20000.

    I haven't used this in Java, but it is valid in C. In C, you can use any valid expression to alter the value of i. I would think that the same is true in Java.

    In your example i + 20000 doesn't change i whereas i+=20000 or i = i + 20000 do.

  3. #3
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665

    Re: for loops

    Originally posted by kateryan13
    A for loop is usually written as
    for(i =0; i<10; i++)
    However, instead of i++ I want to go up in increments of 20000, and my program wont compile when I write i + 20000, does anyone know how you do this??
    Thanks very much!
    PHP Code:

    public class ForCounter 

        public static 
    void main(final String[] args
        { 
            for (
    int i 01000000i++) 
            {
                
    i+=20000;
                
    System.out.println(i); 
            } 
        } 

    Like this? There's a problem. It keeps summing "i" every iteration by 1.

    Hrmm..

    Kind regards,
    Jaime Bueza
    Last edited by BuezaWebDev; 11-29-2004 at 07:41 PM.

  4. #4
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Nar, like this:
    Code:
    public class ForCounter 
    { 
        public static void main(final String[] args) 
        { 
            for (int i = 0; i < 1000000; i+=20000) 
                System.out.println(i); 
        } 
    }
    Regards.

  5. #5
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    lol, that's weird. I swear, I compiled it like that too and it gave me an error. :|

    Hmm... Thanks for the quick reply. I learned something as well as the guy who asked the original question!

    Kind regards,
    Jaime Bueza

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles