This piece of code is from the Sun Java Tutorial
class ContinueDemo {
public static void main(String[] args) {
String searchMe = "peter piper picked a peck of pickled peppers";
int max = searchMe.length();
int numPs = 0;
for (int i = 0; i < max; i++) {
//interested only in p's
if (searchMe.charAt(i) != 'p')
continue;
//process p's
numPs++;
}
System.out.println("Found " + numPs + " p's in the string.");
}
}
I'm wondering if there is any real point to using continue in the spot
where they do, or if they are just trying to illustrate a point, because
to me, the code would make much more sense if it were written like this:
class ContinueDemo2 {
public static void main(String[] args) {
String searchMe = "peter piper picked a peck of pickled peppers";
int max = searchMe.length();
int numPs = 0;
for (int i = 0; i < max; i++) {
//interested only in p's
if (searchMe.charAt(i) == 'p')
//process p's
numPs++;
}
System.out.println("Found " + numPs + " p's in the string.");
}
}
The result is 9 both ways, so unless this is just an illustration of the
use of continue, I see no point for it.
Jeff Higgins - 18 Jun 2007 22:07 GMT
JT
> This piece of code is from the Sun Java Tutorial
>
[quoted text clipped - 40 lines]
> The result is 9 both ways, so unless this is just an illustration of the
> use of continue, I see no point for it.
class test {
public static void main(String[] args) {
String searchMe =
"peter piper picked a peck of pickled peppers";
System.out.println("Found " + (searchMe.length() -
searchMe.replaceAll("p", "").length()) +
" p's in the string.");
}
}
Daniel Dyer - 18 Jun 2007 22:20 GMT
> This piece of code is from the Sun Java Tutorial
>
[quoted text clipped - 42 lines]
> The result is 9 both ways, so unless this is just an illustration of the
> use of continue, I see no point for it.
I agree, I prefer the second form. I never use continue, I always prefer
to structure the code as in your second example.
On a slightly different note, I also always insist on using braces for all
blocks (if, else, for, while, etc.), whether they are strictly required or
not. Without the identation of the conditional, the second example is
confusing.
Dan.

Signature
Daniel Dyer
http//www.uncommons.org