julia_v_iluhina

Untitled

Oct 23rd, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. У меня есть непонятка с этим коаном, конкретно непонятен момент со строчкой №10
  2.     @Koan
  3.     public void forLoopContinueLabel() {
  4.         int count = 0;
  5.         outerLabel:
  6.         for (int i = 0; i < 6; i++) {
  7.             for (int j = 0; j < 6; j++) {
  8.                 count++;
  9.                 if (count > 2) {
  10.                     continue outerLabel;
  11.                 }
  12.             }
  13.             count += 10;
  14.         }
  15.         // What does continue with a label mean?
  16.         // What gets executed? Where does the program flow continue?
  17.         assertEquals(count, 8);
  18.     }
  19.  
  20. решение
  21.  
  22.     @Koan
  23.     public void forLoopContinueLabel() {
  24.         int count = 0;
  25.         outerLabel:
  26.         for(int i = 0; i < 5; i++) {
  27.             for(int j = 0; j < 5; j++)
  28.             {
  29.                 count++;
  30.                 if(count > 2) {
  31.                     continue outerLabel;
  32.                 }
  33.             }
  34.             count += 10;
  35.         }
  36.         // What does continue with a label mean?
  37.         // What gets executed? Where does the program flow continue?
  38.         assertEquals(count, 7);
  39.     }
  40.  
  41. /*
  42.     http://joxi.ru/zANDEYxul43Rzm
  43.     при выполнении continue outerLabel;
  44.     мы снова переходим на строку for(int i = 0; i < 5; i++)
  45.     начиаем новую его итерацию
  46.  
  47.     что у нас будет происходить
  48.     i=0 -  первая итерация внешнего цикла (count = 0)
  49.     j = 0(count = 1), 1(count=2), 2(count=3) = прошло 3 итерации внутреннего цикла
  50.     вернулись на новую итерацию внешнего цикла
  51.  
  52.     i=1
  53.     j = 0(count = 4) = прошла 1 итерация внутреннего цикла
  54.     вернулись на новую итерацию внешнего цикла
  55.  
  56.     i=2
  57.     j = 0(count = 5) = прошла 1 итерация внутреннего цикла
  58.     вернулись на новую итерацию внешнего цикла
  59.  
  60.     i=3
  61.     j = 0(count = 6) = прошла 1 итерация внутреннего цикла
  62.     вернулись на новую итерацию внешнего цикла
  63.  
  64.     i=4
  65.     j = 0(count = 7) = прошла 1 итерация внутреннего цикла
  66.     вернулись на новую итерацию внешнего цикла
  67.  
  68.     i=5 - все, вышли по условию i < 5
  69.  
  70.     что почитать
  71.     http://developer.alexanderklimov.ru/android/java/break.php
  72.     http://www.avajava.com/tutorials/lessons/how-do-i-use-the-break-and-continue-statements.html
  73.     http://voituk.kiev.ua/2007/01/11/java-label-operator/
  74.  
  75.     мое мнение
  76.     мне ни разу не понадобилось использовать метки)
  77.     ну и в общем - я бы не советовала это применять направо и налево) -
  78.     чаще всего без переходов по меткам - вполне можно обойтись
  79.     и код будет проще при этом
  80.     и лишь в редчайших случаях это стоит применять)
  81.  
  82.     http://stackoverflow.com/questions/14960419/is-using-a-labeled-break-a-good-practice-in-java
  83.     вот этот ответ мне кажется наиболее толковым
  84.     It's not as terrible as goto because it only sends control to the end of the labeled statement
  85.     (typically a loop construct). The thing that makes goto unlikeable is that it is an arbitrary branch
  86.     to anywhere, including labels found higher up in the method source, so that you can have home-grown
  87.     looping behavior. The label-break functionality in Java doesn't allow that kind of craziness because control
  88.     only goes forward.
  89.  
  90.     I've only used it once about 12 years ago, in a situation where I was needing to break out of nested loops,
  91.     the more-structured alternative would've made for more complicated tests in the loops.
  92.     I wouldn't advise using it a lot but I wouldn't flag it as an automatic bad-code smell.
  93.  
  94.  
  95.     http://stackoverflow.com/questions/46496/should-i-avoid-using-java-label-statements
  96. */
Advertisement
Add Comment
Please, Sign In to add comment