Results 1 to 5 of 5
  1. #1
    tariqm is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Why do we need "continue" in this piece of code?

    public class ContinueInForLoop {

    public static void main(String[] args) {

    String searchMe = "peter piper picked a peck of pickled peppers";

    int noOfPs = 0;
    int i;
    for(i = 0; i<searchMe.length(); i++){
    if(searchMe.charAt(i) != 'p')
    continue;
    noOfPs++;
    }

    System.out.println("There are " + noOfPs + " P's in " + searchMe);
    }

    }


    Hi all, this an example taken form java official tutorials:
    we can achieve the same output without "continue" key word, e.g

    if(searchMe.charAt(i) == 'p')
    noOfPs++;

    Why bother putting "continue" keyword there?

    Thanks for the help.

    Tariq

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Why do we need "continue" in this piece of code?

    Yes, programmers can not use correct, straight forward logic and make their program more complicated. Happens all the time.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Why do we need "continue" in this piece of code?

    Quote Originally Posted by tariqm View Post
    this an example taken form java official tutorials
    ... from the section about the use of the continue keyword.
    Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Why bother putting "continue" keyword there?
    Wouldn't make much sense to avoid the use of the very keyword the tutorial explains there, would it?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    tariqm is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Why do we need "continue" in this piece of code?

    Quote Originally Posted by DarrylBurke View Post
    ... from the section about the use of the continue keyword.
    Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)


    Wouldn't make much sense to avoid the use of the very keyword the tutorial explains there, would it?

    db
    Thanks for the reply;
    It may not make sense here in this specific example not to use continue keyword, but if we take in
    the account that minimum code is easy to understand and debug, it makes perfect sense not to use
    continue keyword.
    What do you think..........
    may be i am missing some important concept?????


    Tariq

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why do we need "continue" in this piece of code?

    Quote Originally Posted by tariqm View Post
    Thanks for the reply;
    It may not make sense here in this specific example not to use continue keyword, but if we take in
    the account that minimum code is easy to understand and debug, it makes perfect sense not to use
    continue keyword.
    What do you think..........
    may be i am missing some important concept?????
    Using a continue statement unconditionally is silly, because the code following that condition statement can not be reached anymore; if there is no code following the unconditional continue statement, the continue statement is useless; therefore, a continue statement is always used conditionally and there it doesn't do much more than avoid additional indentation cause by an else statement had the continue statement not been used. i.m.h.o. it's a bit too rigid to ban a continue statement; we could ban the while statement for the same (rigid) reason (while (<cond>) <statement> --> for( ;<cond>; ) <statement>)

    A labeled continue statement has serious uses though ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 02-01-2012, 09:37 PM
  2. Program skips "If" code and goes straight to "Else"
    By Logik22 in forum New To Java
    Replies: 12
    Last Post: 01-21-2012, 06:40 PM
  3. Replies: 5
    Last Post: 12-21-2011, 07:44 PM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •