For Programmers: Free Programming Magazines  


Home > Archive > Java Help > June 2007 > Why use "continue" here?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Why use "continue" here?
JT

2007-06-18, 10:11 pm

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

2007-06-18, 10:11 pm


JT
> 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.


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

2007-06-18, 10:11 pm

On Mon, 18 Jun 2007 21:37:44 +0100, JT <jtlinux1@oohay.ca> wrote:

> This piece of code is from the Sun Java Tutorial
>
> class ContinueDemo {
> public static void main(String[] args) {
>
> String searchMe =3D "peter piper picked a peck of pickled =


> peppers";
> int max =3D searchMe.length();
> int numPs =3D 0;
>
> for (int i =3D 0; i < max; i++) {
> //interested only in p's
> if (searchMe.charAt(i) !=3D '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, becau=

se =

> to me, the code would make much more sense if it were written like thi=

s:
>
> class ContinueDemo2 {
> public static void main(String[] args) {
>
> String searchMe =3D "peter piper picked a peck of pickled =


> peppers";
> int max =3D searchMe.length();
> int numPs =3D 0;
>
> for (int i =3D 0; i < max; i++) {
> //interested only in p's
> if (searchMe.charAt(i) =3D=3D '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 t=

he =

> use of continue, I see no point for it.


I agree, I prefer the second form. I never use continue, I always prefe=
r =

to structure the code as in your second example.

On a slightly different note, I also always insist on using braces for a=
ll =

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.

-- =

Daniel Dyer
http//www.uncommons.org
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com