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
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
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!!!!
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.Originally Posted by randb
Your way is probably more efficient, though.![]()
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
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!!!Originally Posted by snarkophilus
Using mod 8... very good! I didn't even think of that, but of course that's the way to go.Originally Posted by SpaceTrekkie
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.
WRONG. Sometimes an additional IF operator can save a nice pile of CPU cycles in a programOriginally Posted by randb
![]()
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.Originally Posted by Carnifex
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; }
Perhaps cleaner / more readable:Originally Posted by snarkophilus
EDIT: maybe this is still better; a matter of taste:Code:for(i = 0; i < x; ++i) { n = i % 4 * 2 + 3; }
Code:for(i = 0; i < 2 * x; i += 2) { n = i % 8 + 3; }
or maybe:
Code:for(i = 0; i < 2 * x; i += 2) { n = (i & 7) + 3; }
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 betterOriginally Posted by Carnifex
)