Results 1 to 12 of 12

Thread: Java Question

  1. #1
    Join Date
    May 2003
    Posts
    398

    Java Question

    hey all,

    I have a question for anyone who knows java. I need to write a for loop that will print out: 3 5 7 9 3 5 7 9. I can't seem to get that pattern no matter what i try. If anyone has any idea please let me know!

    thanks in advance,
    ST

  2. #2
    Join Date
    Nov 2005
    Posts
    330
    how many repetitions of the pattern (3 5 7 9) do you need?

    i = 0
    for i < 2
    print ("3 5 7 9")
    i = i + 1


    This isnt java....cuz I'm not sure of the functions used...but you could use this to get an idea.
    Good Luck!!!!

  3. #3
    Join Date
    Nov 2005
    Posts
    330
    if you want to generate the numbers, you'll need 2 loops!!! one for number of repetitions and the other for generating numbers!!! 2 variables.... one for number of repetitions and the other with an initial value of 3 and inrements of 3 till it is less than 11.

    k = 0
    for k < 2
    i = 3
    for i < 11
    print i
    i = i + 2
    k++

  4. #4
    Join Date
    Sep 2005
    Posts
    1,095
    Quote Originally Posted by randb
    if you want to generate the numbers, you'll need 2 loops!!! one for number of repetitions and the other for generating numbers!!! 2 variables.... one for number of repetitions and the other with an initial value of 3 and inrements of 3 till it is less than 11.

    k = 0
    for k < 2
    i = 3
    for i < 11
    print i
    i = i + 2
    k++
    Two loops aren't necessary if you use the modulus operator. Just take k from 1 to 19, step 2, print k % 10, and insert a statement before the print that increments k by 2 if k % 10 == 1. If you don't want to do it with the step of 2 (I know people who refuse to do this), go k from 1 to 9, increment if k % 5 == 0, and print (2*k + 1) % 10.

    Your way is probably more efficient, though.

  5. #5
    Join Date
    May 2003
    Posts
    398
    thanks for your help all! i ended up doing it in one loop with a mod 8 and adding a whole bunch in and stuff. Probably not the most efficient...but i got then numbers!

    st

  6. #6
    Join Date
    Nov 2005
    Posts
    330
    Quote Originally Posted by snarkophilus
    Two loops aren't necessary if you use the modulus operator. Just take k from 1 to 19, step 2, print k % 10, and insert a statement before the print that increments k by 2 if k % 10 == 1. If you don't want to do it with the step of 2 (I know people who refuse to do this), go k from 1 to 9, increment if k % 5 == 0, and print (2*k + 1) % 10.

    Your way is probably more efficient, though.
    I didnt come across this in my C/C++ classes that I took 2 years ago...lol... I've never used JAVA before... But smaller the code, better it is!!!

  7. #7
    Join Date
    Sep 2005
    Posts
    1,095
    Quote Originally Posted by SpaceTrekkie
    thanks for your help all! i ended up doing it in one loop with a mod 8 and adding a whole bunch in and stuff. Probably not the most efficient...but i got then numbers!

    st
    Using mod 8... very good! I didn't even think of that, but of course that's the way to go.

    for i from 1 to 9
    print ((2*i - 1) % 8) + 2

    Again, the double for loop might be faster... but it might not. It depends upon both your processor and your compiler.

  8. #8
    Join Date
    Jan 2006
    Posts
    260
    Quote Originally Posted by randb
    I didnt come across this in my C/C++ classes that I took 2 years ago...lol... I've never used JAVA before... But smaller the code, better it is!!!
    WRONG. Sometimes an additional IF operator can save a nice pile of CPU cycles in a program

  9. #9
    Join Date
    Jul 2004
    Posts
    436
    Quote Originally Posted by Carnifex
    WRONG. Sometimes an additional IF operator can save a nice pile of CPU cycles in a program
    In obvious cases, sure, but otherwise often no. On modern processors, conditionals tend to trash the instruction pipeline (branch prediction). It is often faster to use "continuous" arithmetics, than trying to save a few instructions with conditionals.

    Like this example. Avoiding the lengthy calculation if x is zero won't make this code faster, even if x is zero most of the time.
    Code:
    float f(int x, float y)
    {
        if(x == 0) {
             return 0.0f;
        }
        return (y / 2 + 5 + x) / 2.5f * x;
    }

  10. #10
    Join Date
    Jul 2004
    Posts
    436
    Quote Originally Posted by snarkophilus
    for i from 1 to 9
    print ((2*i - 1) % 8) + 2
    Perhaps cleaner / more readable:
    Code:
    for(i = 0; i < x; ++i) {
        n = i % 4 * 2 + 3;
    }
    EDIT: maybe this is still better; a matter of taste:
    Code:
    for(i = 0; i < 2 * x; i += 2) {
        n = i % 8 + 3;
    }

  11. #11
    Join Date
    Mar 2004
    Posts
    3,133
    or maybe:

    Code:
    for(i = 0; i < 2 * x; i += 2) {
      n = (i & 7) + 3;
    }

  12. #12
    Join Date
    Nov 2005
    Posts
    330
    Quote Originally Posted by Carnifex
    WRONG. Sometimes an additional IF operator can save a nice pile of CPU cycles in a program
    well...i guess so....I'm not sure, but while writing small programs, it doesn't really make a difference. (One has to type less, if a program is small...so smaller is better )

Similar Threads

  1. Anyone had a problem updating their Java lately?
    By Glom in forum Off-Topic Babbling
    Replies: 11
    Last Post: 2011-Aug-05, 10:24 AM
  2. Two Java Quakes
    By Blob in forum Science and Technology
    Replies: 15
    Last Post: 2006-Jul-21, 03:18 PM
  3. BABB Java Chat
    By Charlie in Dayton in forum Off-Topic Babbling
    Replies: 5
    Last Post: 2004-Nov-18, 01:27 AM
  4. Formulas & Java Applets
    By Tom2Mars in forum Space Exploration
    Replies: 1
    Last Post: 2004-Jul-17, 10:32 PM
  5. JaVa..
    By in forum Against the Mainstream
    Replies: 30
    Last Post: 2003-Aug-11, 03:15 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
  •