• Post Reply Bookmark Topic Watch Topic
  • New Topic

please explain how it outputs 11 33 99  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Circles {
4. public static void main(String[] args) {
5. int[] ia = {1,3,5,7,9};
6. for(int x : ia) {
7. for(int j = 0; j < 3; j++) {
8. if(x > 4 && x < 8) continue;
9. System.out.print(" " + x);
10. if(j == 1) break;
11. continue;
12. }
13. continue;
14. }
15. }
16. }
when the second CONTINUE is executed ,will J be still increment ? because "continue " will stop the current iteration and moves to next iteration?
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when talking about code, it it CRITICAL you tell people EXACTLY what happens. the output of the program is NOT "11 33 99".

The actual output is " 1 1 3 3 9 9"

you should probably read our FAQ on TellTheDetails

I would suggest you try stepping through the code, line by line, on paper, keeping track of all the variables. Then, if you have a specific question, come back and ask.
 
Sheriff
Posts: 4930
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred, pay attention at condition in a inner "for" loop.
In general, it's dangerous do not go through the all array under your assumption that array is sorted.

In addition: add braces in if statements, as it is hell know where those continue and break statements meant to be.

 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always post code in code tags, with indentation and without line numbers. The code tags add their own line numbers. That way people looking at the code will be able to read it.

Please tell us where that code is from. It looks like the sort of illegible code you see in exam preparation books, where incorrect indentation is often used to confuse the reader. It also shows how you can use break and continue to create incomprehensible code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!