File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help with loop logic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with loop logic" Watch "Help with loop logic" New topic
Author

Help with loop logic

Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
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

Joined: Mar 17, 2011
Posts: 7059
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
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

Joined: Jul 19, 2010
Posts: 142

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?


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
only for the first line

2nd line will be 9
3rd will be 15
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

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

Joined: Oct 24, 2010
Posts: 557
Please write meaningful thread titles. I changed it to one I think describes the topic better.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
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

Joined: Oct 02, 2003
Posts: 10916
    
  12

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
z is the odd numbers
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

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

Joined: Mar 17, 2011
Posts: 7059
    
  16

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
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
maybe this will make it abit clearer of what i understand

Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
webpage
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7059
    
  16

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
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
Posts: 71
managed to get it working by using the if statement inside:

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7059
    
  16

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
Abhradeep Banerjee
Ranch Hand

Joined: Dec 09, 2009
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

Joined: May 30, 2012
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

Joined: Mar 17, 2011
Posts: 7059
    
  16

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

[Edit] too slow.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with loop logic
 
Similar Threads
How close am I in getting this program right?
Help: traverse through one array and store in another array.
A for loop problem
"assert i%2==0 : i--;"question required
two possible answers Q18 ch 4 page 282 from KB