• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Help with loop logic

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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




 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 151
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
only for the first line

2nd line will be 9
3rd will be 15
 
Jan Hoppmann
Ranch Hand
Posts: 151
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please write meaningful thread titles. I changed it to one I think describes the topic better.
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
z is the odd numbers
 
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 151
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
maybe this will make it abit clearer of what i understand

 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
webpage
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
managed to get it working by using the if statement inside:

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic