aspose file tools*
The moose likes Beginning Java and the fly likes how do i loop not using a for statement? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how do i loop not using a for statement?" Watch "how do i loop not using a for statement?" New topic
Author

how do i loop not using a for statement?

yeng xiong
Greenhorn

Joined: Mar 11, 2010
Posts: 2


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

Joined: Oct 28, 2009
Posts: 33
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

Joined: Sep 14, 2009
Posts: 1660
    
  11

I would use a switch / case:



When you do things right, people won't be sure you've done anything at all.
yeng xiong
Greenhorn

Joined: Mar 11, 2010
Posts: 2
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

Joined: Sep 14, 2009
Posts: 1660
    
  11

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

Joined: Oct 13, 2005
Posts: 38393
    
  23
== true? It's not 1st April, Janeice.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

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

Joined: Oct 13, 2005
Posts: 38393
    
  23
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
 
Consider Paul's rocket mass heater.
 
subject: how do i loop not using a for statement?