• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do i loop not using a for statement?

 
yeng xiong
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


After this, i want to loop it back up into the first statement again. How do i do that?
Thanks a bunch!
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several ways to iterate in Java. At the basic level, there are 2 that you need to be familiar with (aside from the for-next):

1. the while loop. In this structure, the looping condition is evaluated before the loop is entered. The loop may never be entered, depending on the condition.

2. the do-while loop. In this structure, the looping condition is evaluated at the end of the loop. The loop will always be executed at least once, because the condition is evaluated post-loop.

in your example, clearly the do-while is appropriate. Take a look at your text...you should have something along the lines of:

 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use a switch / case:

 
yeng xiong
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i implement:

System.out.println("Shop is not setup yet!");

into it every time when a number other than 1 is pressed?

note: i want to stay as basic as possible, within, method, iteration, arrays, recursive, do-while loops, for loops, etc...
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should set something as a boolean to decide whether or not the shop is set up... and place it into an if/else

perhaps:


or
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== true? It's not 1st April, Janeice.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1807
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was afraid to put



... because I didn't want anyone who might already be confused to think shopSetUp might be a numeric value, like you can do in C++

But I suppose



might be better. I didn't mean to be misleading, as the matter of fact I wanted to unconfuse...
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you write "if (shopSetUp)". It is a boolean, and that is how you write booleans. If it really is a boolean rather than a Boolean, you can't write .equals after it. Not if the compiler is awake, anyway
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic