• Post Reply Bookmark Topic Watch Topic
  • New Topic

while loop  RSS feed

 
Amrit Kaur
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

using a WHILE loop show how you can print out all numbers divisible by 3 between 1 and 50. // hint vary the incrementor?
i am so lost,can i be helped with the syntax of how to show that?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Amrit.

What have you tried?  What exactly are you having a problem with?
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amrit Kaur wrote:i am so lost,can i be helped with the syntax of how to show that?

Hi Amrit. Welcome to coderanch, you can learn a lot about the while loop here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
I also see that you have typed code tags in the original post. Did you forget to paste your attempted code ?

 
Ioanna Katsanou
Ranch Hand
Posts: 127
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add an if loop inside the while loop and check the mod3
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start simple.  First, write a program that uses a while loop to print ALL numbers between 1 and 50.

once that works, and only once that works, start thinking about how to filter which of those numbers you print. 

The key to being a good programmer is learning how to break down one complicated problem into many simpler problems.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:You can add an if loop inside the while loop and check the mod3
There are much better ways to do it.
 
Ioanna Katsanou
Ranch Hand
Posts: 127
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Ioanna Katsanou wrote:You can add an if loop inside the while loop and check the mod3
There are much better ways to do it.


Hey Campbell, you mean with other conditional statements??
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:Hey Campbell, you mean with other conditional statements??

I would point out this from the original post:

// hint vary the incrementor
 
Ioanna Katsanou
Ranch Hand
Posts: 127
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Ioanna Katsanou wrote:Hey Campbell, you mean with other conditional statements??

I would point out this from the original post:

// hint vary the incrementor


Sorry, I didn't notice this.
Cool 
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// hint vary the incrementor

Actually, this statement would confuse me a bit. Rather, I would interpret this as an increment that varies each time. For the given statement, the "value to increment" would be constant.
The hint would better written as : if x is divisible by 3, then x+3 is also divisible by 3
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:. . . Campbell, you mean with other conditional statements??
No, but somebody else has already addressed that question.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!