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: Beginner question about for loops...

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Beginner question about for loops...

    Hi,
    I need to print out a set of numbers that starts at 2, prints out the next 2 numbers, skips the next 2, prints the next 2 and skips the next, etc....... so it should follow the pattern :
    2 3 6 7 10 11 14 15 18 19 22 23 etc....

    code i have so far...
    for (int i =2; i<=255; i++){
    //print out i
    }

    but... i know that this will only print out all the numbers in a row... Any help or suggestions would be great.
    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner question about for loops...

    Do you know how to use an if statement? You could use an if statement to select when to print and when to skip printing. The trick is determining when to do one and when to do the other.
    One way would be to use a counter to see when you had printed two numbers.
    Another would be to look at the relationship of the numbers that are printed.
    I'll leave the logic to you. You need to spend some time with a paper and pencil, thinking about this before you try writing the code.

Similar Threads

  1. please help with if-else statement, loops and beginner stuff
    By javabeg123 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2011, 08:38 PM
  2. Loops & Series Question
    By nuttaay in forum Loops & Control Statements
    Replies: 5
    Last Post: November 29th, 2011, 01:46 PM
  3. Beginner question on where to put an array
    By Diplo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 3rd, 2011, 03:56 PM
  4. Beginner Question
    By jrt224 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2011, 12:56 PM
  5. [SOLVED] Simple question from a beginner
    By jimmylee7706 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 09:57 PM