반응형
⬛break문
package basic;
//break : switch 구문 또는 반복문을 강제로 종료하기 위한 키워드
public class BreakApp {
public static void main(String[] args) {
//반복문의 조건식이 거짓인 경우 반복문 종료
for(int i=1; i<=5; i++) {
//if 구문의 조건식이 참인 경우 break 명령을 실행하여 반복문 종료
if(i==3) break;
System.out.print(i+"\t");
}
System.out.println();
System.out.println("=========================================");
for(int i=1; i<=5; i++) {
for(int j=1; j<=5; j++) {
//break 라벨명 : 라벨명으로 지정된 반복문 종료
if(j==3) break;//for(int j=1; j<=5; j++) 반복문 종료
System.out.println("i = "+i+", j = "+j);
}
}
System.out.println("=========================================");
//반복문 작성시 반복문을 구분하기 위한 식별자(라벨명) 선언 가능
//형식)라벨명:반복문
loop:
for(int i=1; i<=5; i++) {
for(int j=1; j<=5; j++) {
//break 키워드 포함된 반복문
if(j==3) break loop;//for(int j=1; j<=5; j++) 반복문 종료
System.out.println("i = "+i+", j = "+j);
}
}
System.out.println("=========================================");
}
}
⬛ continue문
package basic;
//continue : 반복문의 명령을 처음부터 다시 실행하기 위한 키워드
// => 반복문의 블럭 내부에서 continue 명령이 실행되면 하단에 작성된 명령을 실행하지 않고
//처음부터 다시 실행
public class ContinueApp {
public static void main(String[] args) {
for(int i=1; i<=5; i++) {
if(i==3) continue;
System.out.print(i+"\t");
}
System.out.println();
System.out.println("=========================================");
loop:
for(int i=1; i<=5; i++) {
for(int j=1; j<=5; j++) {
if(j==3) continue loop;
System.out.println("i = "+i+", j = "+j);
}
}
System.out.println();
System.out.println("=========================================");
}
}
반응형
'Backend > Java' 카테고리의 다른 글
[Java]다중for문으로 별만들기 (0) | 2023.12.04 |
---|---|
[Java]다중for문(Multi-for) (0) | 2023.12.04 |
[Java]반복문 - For문 & While문(+do-while문) (0) | 2023.11.29 |
[Java]제어문 - If~else문 & Switch문 (0) | 2023.11.28 |
[Java]연산자(단항 연산자 / 이항 연산자 / 삼항 연산자) (1) | 2023.11.25 |