• Post Reply Bookmark Topic Watch Topic
  • New Topic

please explain continue in this program  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this



can you explain what continue does in this
when i run it i get
Found 9 p's in the string.

if i comment out the continue i get
Found 35 p's in the string.

i just dont see or understand how its coming up with this result

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which part do you not understand? What continue does? How an if statement works? How a for statement works? The conditional expression?
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Which part do you not understand? What continue does? How an if statement works? How a for statement works? The conditional expression?


...How commenting out the statement directly after an if behaves when you don't use braces?
 
Wesley Grove
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The continue statement is telling the code to only perform the incrementation of countP if the if statement is true. Otherwise it skips everything after the continue until the next cycle of he for loop. This program isn't the best example of the continue statement, but it shows how it works at least.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan McGuire wrote:
Bear Bibeault wrote:Which part do you not understand? What continue does? How an if statement works? How a for statement works? The conditional expression?


...How commenting out the statement directly after an if behaves when you don't use braces?


And, yeah, never omit braces.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such code is from; that is pretty awful code. If you are trying to count ps, there is a much better way to do it.

I hereby challenge you to work out how to do that with a Stream rather than a loop.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats from the oracle java tutorials...believe it or not...streams ???
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:if i comment out the continue i get
Found 35 p's in the string.


Why don't you just delete that line of code instead of replacing it by a comment? Seems to me all the comment is doing is diverting your attention from the code. Same goes for the other comment, for that matter. Delete that one too so you can just look at the code.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:thats from the oracle java tutorials...believe it or not...streams ???
I found it in the branching statements part of the Java™ Tutorials. Don't worry, they con produce worse code than that to demonstrate continue  Scroll down a bit in the same tutorial and you can see an example.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!