• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Continue Statement

 
Aaron Parker
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic