Results 1 to 5 of 5
- 04-08-2012, 02:11 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
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
- 04-08-2012, 02:25 PM #2
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.
- 04-08-2012, 02:43 PM #3
Re: Why do we need "continue" in this piece of code?
... 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?Why bother putting "continue" keyword there?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-08-2012, 04:19 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
Re: Why do we need "continue" in this piece of code?
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
- 04-08-2012, 05:47 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,391
- Blog Entries
- 7
- Rep Power
- 17
Re: Why do we need "continue" in this piece of code?
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
loop "play again" in an 8 ball game , loops but wont let me answer my "out.print"
By IareSmart in forum New To JavaReplies: 1Last Post: 02-01-2012, 08:37 PM -
Program skips "If" code and goes straight to "Else"
By Logik22 in forum New To JavaReplies: 12Last Post: 01-21-2012, 05:40 PM -
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 06:44 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks