• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Continue Statement

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic