Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Continue Statement  RSS feed

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;
case 'B':
principal = 5000.00;
case 'C':
principal = 10000.00;
if (quit == true) {
/* shouldn't this jump it out to where the outer loop evaluates?
System.out.print("Enter number of years 1-9: ");
term = (char);;;
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;
} while (quit == false);
Please, please, please help me!
Aaron Parker
author and iconoclast
Posts: 24217
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.
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, 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.
[ March 01, 2004: Message edited by: sever oon ]
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!