Results 1 to 3 of 3
  1. #1
    davidq23 is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default Incrementing by more than 1

    Hello how can you increment a variable in a for loop by more than 1?
    For example I want a for loop like this:
    Java Code:
    for (int i=0; i<20; i++){
    System.out.println(i);
    }
    where i is incremented by 2

    Thanks!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    i+=2, or the long form i=i+2

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    One of the pitfalls of learning how to use for loops. N00bs think that i++ is the only expression that they can use.

Similar Threads

  1. Help with incrementing numbers in a GUI
    By jdg951 in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 03:23 AM
  2. incrementing array elements
    By porchrat in forum New To Java
    Replies: 2
    Last Post: 10-06-2010, 02:37 PM
  3. incrementing elements in an array
    By JavaNubb in forum New To Java
    Replies: 7
    Last Post: 08-20-2010, 03:15 AM
  4. Incrementing and loops
    By Rubidoux in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 02:04 PM
  5. Incrementing jbutton in for loop
    By RobertF in forum New To Java
    Replies: 3
    Last Post: 03-12-2009, 11:41 AM

Posting Permissions

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