Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    65
    Rep Power
    0

    Default My for loop is skipping even numbers somehow

    Hi. I was expecting my for loop to print out "1 2 3 4 5 6", but it skipped the even numbers:

    My for loop is skipping even numbers somehow-skippingevennumbers.png

    Why does it do that? Here's my code:

    Java Code:
    package randomExperiments;
    
    import java.util.Scanner;
    import java.lang.Double;
    import java.text.NumberFormat;
    import java.lang.Integer;
    
    public class RandomExperiments {
    	
    	public static void main(String[] args){
    		
    		for(int i = 1; i < 6; i++)
    		{
    			System.out.println(i);
    			i++;
    		}
    	}
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,983
    Rep Power
    8

    Default Re: My for loop is skipping even numbers somehow

    The increment is already in the for-loop. You should not repeat that INSIDE the loop.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Skipping code on second run through?
    By Qwertyness_ in forum New To Java
    Replies: 12
    Last Post: 03-11-2013, 02:11 PM
  2. Print Numbers with a loop from an array
    By ejdrijin in forum New To Java
    Replies: 1
    Last Post: 05-30-2012, 07:46 PM
  3. Adding squared numbers in a loop
    By kurap3ka in forum New To Java
    Replies: 5
    Last Post: 02-10-2011, 08:13 AM
  4. Skipping Scanner
    By chari_autumn in forum New To Java
    Replies: 2
    Last Post: 01-04-2011, 06:29 AM
  5. skipping input
    By windie_86 in forum New To Java
    Replies: 1
    Last Post: 02-12-2008, 07:09 PM

Posting Permissions

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