• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using loops in Java

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new to java and I need to loop this code. I want the loop to end when one of these things happen. How can I do this, or what kind of loop should I use? ("blank" or "null" to exit; '#' to change key)

 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this is an loop that will always execute at least once then I suggest that you use a do while loop.
Here is the Oracle Java tutorial on control flow statements
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html
and here is the part about do while and while loops
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

I think that this should help point you in the correct direction.
 
Brett Banich
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pete Letkeman wrote:If this is an loop that will always execute at least once then I suggest that you use a do while loop.
Here is the Oracle Java tutorial on control flow statements
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html
and here is the part about do while and while loops
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

I think that this should help point you in the correct direction.



I appreciate the response. Ill read up on while loops.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brett Banich wrote:I appreciate the response. Ill read up on while loops.


Well that's what we are here for to help each other out.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe you need to use a do while loop but I could be wrong. I hope this helped and solved your problem!
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Be sure you are contributing something new to the thread before posting.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are gonna wanna use a while loop. Here's a decent video on how to use while loops, once you pick it up though while loops are easy.



Here's also a program I did to show how to use them.
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you think this is an appropriate use of a while loop?

Is there any looping going on there? What other language construct could you use instead here that might be better suited?
 
reply
    Bookmark Topic Watch Topic
  • New Topic