package kr.s03.operation;
public class BreakMain01 {
public static void main(String[] args) {
//break는 switch블럭을 빠져나갈 떄,
//반복문에서 특정 조건일 때 반복문을 빠져나가는 용도로 사용함.
int n = 1;
while(n <=10) {
System.out.println(n);
n++;
if(n==8) break;
}
}
}
1
2
3
4
5
6
7
package kr.s03.operation;
public class BreakMain02 {
public static void main(String[] args) {
//다중반복문에서 break 사용하기
for (int i=0;i<3;i++) {
for(int j =0; j<5;j++) {
if (j==3) {
/*
* 특정 조건일 때 반복문을 빠져나감.
* 다중 반복문 일 때 전체 반복문을 빠져나가는 것이 아니라
* break가 포함되어 있는 반복분만 빠져나감.
*/
break;
}
System.out.println(i+","+j);
}
}
}
}
0,0
0,1
0,2
1,0
1,1
1,2
2,0
2,1
2,2
package kr.s03.operation;
public class BreakMain03 {
public static void main(String[] args) {
//다중반복문에서 break를 이용해서 모든 반복문 빠져나가기
exit_for: //break label 지정, 명칭은 상관없음
for (int i=0;i<3;i++) {
for(int j =0; j<5;j++) {
if (j==3) {
//레이블이 지정된 for문 영역을 빠져나감.
break exit_for;
}
System.out.println(i+","+j);
}
}
}
}
0,0
0,1
0,2