1. ## 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!

ST

2. 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. 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. 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. 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. 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. 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. 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. 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. Originally Posted by snarkophilus
for i from 1 to 9
print ((2*i - 1) % 8) + 2
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. or maybe:

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

12. 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 )

#### Posting Permissions

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