Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2007

Tip: Looking for answers? Try searching our database.

Why use "continue" here?

Thread view: 
JT - 18 Jun 2007 21:37 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.