Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- У меня есть непонятка с этим коаном, конкретно непонятен момент со строчкой №10
- @Koan
- public void forLoopContinueLabel() {
- int count = 0;
- outerLabel:
- for (int i = 0; i < 6; i++) {
- for (int j = 0; j < 6; j++) {
- count++;
- if (count > 2) {
- continue outerLabel;
- }
- }
- count += 10;
- }
- // What does continue with a label mean?
- // What gets executed? Where does the program flow continue?
- assertEquals(count, 8);
- }
- решение
- @Koan
- public void forLoopContinueLabel() {
- int count = 0;
- outerLabel:
- for(int i = 0; i < 5; i++) {
- for(int j = 0; j < 5; j++)
- {
- count++;
- if(count > 2) {
- continue outerLabel;
- }
- }
- count += 10;
- }
- // What does continue with a label mean?
- // What gets executed? Where does the program flow continue?
- assertEquals(count, 7);
- }
- /*
- http://joxi.ru/zANDEYxul43Rzm
- при выполнении continue outerLabel;
- мы снова переходим на строку for(int i = 0; i < 5; i++)
- начиаем новую его итерацию
- что у нас будет происходить
- i=0 - первая итерация внешнего цикла (count = 0)
- j = 0(count = 1), 1(count=2), 2(count=3) = прошло 3 итерации внутреннего цикла
- вернулись на новую итерацию внешнего цикла
- i=1
- j = 0(count = 4) = прошла 1 итерация внутреннего цикла
- вернулись на новую итерацию внешнего цикла
- i=2
- j = 0(count = 5) = прошла 1 итерация внутреннего цикла
- вернулись на новую итерацию внешнего цикла
- i=3
- j = 0(count = 6) = прошла 1 итерация внутреннего цикла
- вернулись на новую итерацию внешнего цикла
- i=4
- j = 0(count = 7) = прошла 1 итерация внутреннего цикла
- вернулись на новую итерацию внешнего цикла
- i=5 - все, вышли по условию i < 5
- что почитать
- http://developer.alexanderklimov.ru/android/java/break.php
- http://www.avajava.com/tutorials/lessons/how-do-i-use-the-break-and-continue-statements.html
- http://voituk.kiev.ua/2007/01/11/java-label-operator/
- мое мнение
- мне ни разу не понадобилось использовать метки)
- ну и в общем - я бы не советовала это применять направо и налево) -
- чаще всего без переходов по меткам - вполне можно обойтись
- и код будет проще при этом
- и лишь в редчайших случаях это стоит применять)
- http://stackoverflow.com/questions/14960419/is-using-a-labeled-break-a-good-practice-in-java
- вот этот ответ мне кажется наиболее толковым
- It's not as terrible as goto because it only sends control to the end of the labeled statement
- (typically a loop construct). The thing that makes goto unlikeable is that it is an arbitrary branch
- to anywhere, including labels found higher up in the method source, so that you can have home-grown
- looping behavior. The label-break functionality in Java doesn't allow that kind of craziness because control
- only goes forward.
- I've only used it once about 12 years ago, in a situation where I was needing to break out of nested loops,
- the more-structured alternative would've made for more complicated tests in the loops.
- I wouldn't advise using it a lot but I wouldn't flag it as an automatic bad-code smell.
- http://stackoverflow.com/questions/46496/should-i-avoid-using-java-label-statements
- */
Advertisement
Add Comment
Please, Sign In to add comment