Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Help with loop logic

Ranch Hand
Posts: 71
basic java code kind of forgotten need some hel

this will show the following:

NO ODD NUMBERS
5 1 3 5
10
15

but what i want is :
NO ODD NUMBERS
5 1 3 5
10 1 3 5 7 9
15 1 3 5 7 9 11 13 15

thanks guys hope to hear from you
o

Winston Gutkowski
Bartender
Posts: 10527
64
Abhradeep Banerjee wrote:thanks guys hope to hear from you

Question: what doesdo?

Answer that, and I think you'll work out what your problem is.

Winston

Ranch Hand
Posts: 71
while (z<5) {
z=z+2;
...
}

do?

Answer that, and I think you'll work out what your problem is.

i know it will keep displaying numbers up to 5 and z increments by 2.

Jan Hoppmann
Ranch Hand
Posts: 147
Abhradeep Banerjee wrote:i know it will keep displaying numbers up to 5 and z increments by 2.

Right. But you don't want 5 to be your upper boundary, do you?
And what value does z have in your second and third loop?

Ranch Hand
Posts: 71
only for the first line

2nd line will be 9
3rd will be 15

Jan Hoppmann
Ranch Hand
Posts: 147
So you don't want the odd numbers that are less than 5, but rather less than x, right? That should point you in the right direction

Greg Brannon
Bartender
Posts: 563
Please write meaningful thread titles. I changed it to one I think describes the topic better.

Ranch Hand
Posts: 71
Jan Hoppmann wrote:So you don't want the odd numbers that are less than 5, but rather less than x, right? That should point you in the right direction

kind of confused now

fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
My suggestion is always to put in tons of System.out.println() statements to see what your code is doing. Specifically, I would put some in here to see what x and z are at the beginning of each loop.

print out the values each time you think they should change. Print out the values just before any test-condition..i.e. just before each 'while' statement.

I don't think 'z' is what you think it is everywhere...

Ranch Hand
Posts: 71
z is the odd numbers

Junilu Lacar
Bartender
Posts: 7764
62
step through the loops with paper and pencil, note down the values each time through the loop. You'll see what Fred is trying to tell you.

Jan Hoppmann
Ranch Hand
Posts: 147
Abhradeep Banerjee wrote:z is the odd numbers

No. z is exactly one odd number at any given moment, this value is its state. You should check this state at the start and / or end of every loop, simply with System.out.println(z);. You should do the same for x as well.

Winston Gutkowski
Bartender
Posts: 10527
64
Abhradeep Banerjee wrote:z is the odd numbers

No it isn't (at least not based on what you wrote).

My advice: Stop guessing, and listen to the advice you've been given (especially fred's). Do some debugging and find out what is going on, because you aren't going to learn anything by simply firing off questions.

Winston

Ranch Hand
Posts: 71
maybe this will make it abit clearer of what i understand

Ranch Hand
Posts: 71
webpage

Winston Gutkowski
Bartender
Posts: 10527
64
Abhradeep Banerjee wrote:maybe this will make it abit clearer of what i understand...

Yes, but what you understand doesn't produce what you want, so the question you need to answer is: Why not?

You have been given plenty of advice, but you seem to want someone else to do your work for you.

ShowSomeEffort (←click) or explain what it is you don't understand. It's a requirement of this website.

Winston

Ranch Hand
Posts: 71
managed to get it working by using the if statement inside:

Winston Gutkowski
Bartender
Posts: 10527
64
Abhradeep Banerjee wrote:managed to get it working by using the if statement inside:

OK. Well done. Now I'd suggest that you look back over the advice that you've been given and see if you can do even better.
(Hint: you only need one 'if' statement)

Winston

Ranch Hand
Posts: 71
So you don't want the odd numbers that are less than 5, but rather less than x, right? That should point you in the right direction <=== just this statement i dont understand what he means

Stuart A. Burkett
Ranch Hand
Posts: 679
Abhradeep Banerjee wrote: So you don't want the odd numbers that are less than 5, but rather less than x, right? That should point you in the right direction <=== just this statement i dont understand what he means

You start off with x set to 5 and print all the odd numbers less than 5
You then set x to 10 and print all the odd numbers less than 10
You then set x to 15 and print all the odd numbers less than 15

Do you see a pattern ?

Winston Gutkowski
Bartender
Posts: 10527
64
Abhradeep Banerjee wrote: So you don't want the odd numbers that are less than 5, but rather less than x, right? That should point you in the right direction <=== just this statement i dont understand what he means

Look at your latest code, and specifically at your 3 if statements. What is "the same" about them? They are basically doing the same thing.

Now look at Jan's statement again, and see if it makes more sense.

Winston

 too slow.