wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Continue Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Continue Statement" Watch "Continue Statement" New topic
Author

Continue Statement

Aaron Parker
Greenhorn

Joined: Jan 31, 2004
Posts: 27
My understanding of the continue statement is that if it's used in a do...while loop, it should jump to the bottom of the loop and execute the next statment. So if I use a continue in the inner of two loops, and the outer loop's(do...while) end is the statement after the inner loop's end, the while of the do...while should evaluate it's condition. I'm not getting that to work. Here's my code. I'm probably missing something simple, but have been banging my head against the table with this problem for a while.
public class LoopInvestmentDialog {
static int termNum;
static char term;
static String termString;
static Integer termInt;
static String principalCode = "x";
static double principal = 0;
static double newPrincipal = 0;
static double interest = 0;
static double balance = 0;
static boolean quit = false;

public static void main(String[] args) throws Exception {
do {
while (principalCode.charAt(0) != 'A' &&
principalCode.charAt(0) != 'B' &&
principalCode.charAt(0) != 'C' &&
principalCode != "quit") {
principalCode = JOptionPane.showInputDialog
("Please enter A) for $1,000 B) for $5,000 C) for $10,000 or quit) for quit");
quit = principalCode.equalsIgnoreCase("quit");
}
switch (principalCode.charAt(0)) {
case 'A':
principal = 1000.00;
break;
case 'B':
principal = 5000.00;
break;
case 'C':
principal = 10000.00;
break;
default:
break;
}
if (quit == true) {
continue;
/* shouldn't this jump it out to where the outer loop evaluates?
*/
}
System.out.print("Enter number of years 1-9: ");
term = (char) System.in.read();
System.in.read();
System.in.read();
String strVal = String.valueOf(term);
termNum = Integer.parseInt(strVal);
for (int counter = 1; counter <= termNum; ++counter) {
interest = principal * .08 * 100;
interest = Math.round(interest);
balance = principal * 1.08 * 100;
balance = Math.round(balance);
interest = interest / 100;
balance = balance / 100;
System.out.println("Interest for this year is $" + interest +
". This year's balance is $" + balance + ".");
principalCode = "x";
principal = balance;
}
System.out.println(quit);
} while (quit == false);
System.exit(0);
}
}
Please, please, please help me!
Aaron Parker
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Aaron Parker:
it should jump to the bottom of the loop and execute the next statment.

"continue", when used in a do-while loop, will cause the "while" condition to be re-tested and, if it's true, the loop to start over at the beginning. Any code between the continue and the while will be skipped.
But in your code, the "continue" is inside the inner loop, so what it does is restart the inner loop from the top; it has no effect on the outer loop.


[Jess in Action][AskingGoodQuestions]
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
For a complete education on flow control using break and continue, cut and paste the following code into a file called Foo.java, compile it and run it.

Observe the output.
Replace "continue;" with "continue inner;". Run it and observe the output.
Replace "continue inner;" with "continue outer;". Run it and observe the output.
Now repeat this entire process, but everywhere you were using "continue" before, replace with "break".
You'll find that unlabeled breaks and continues only break/continue the innermost loop. To break/continue loops outside the innermost loop, you must use labels.
sev
[ March 01, 2004: Message edited by: sever oon ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Continue Statement