JAVA

반복문 빠져나오기 (break, continue)

오매준 2024. 4. 19. 17:12
 

break
  
  - swithc-case문 내부에서 사용하면 해당 case를 탈출한다 
  - 반복문 내부에서 사용하면 해당 반복문을 탈출한다
  
  continue
 
  - 반복문 내부에서 continue를 만나면 즉시 다음번 반복으로 넘어간다

 

 

for (int i = 0; i < 1000; i++) {
			System.out.println(i); // i 값이 323일 때 break를 만나 반복문을 탈출한다
			if (i == 10) {
				break;
			}
		}

i 값이 10 이 되니까 break 를 만나서 반복문에서 탈출한다

 

for (int i = 0; i < 100; i++) {
			if (i % 2 == 0) {
				continue;
			}
			System.out.println(i);

i 값이 2로 나눠질때, 즉 짝수일때 continue 를 만나서 건너뛰게 되서 2로 나눠지지 않는 홀수만 출력된다

char a = 'W', b = ' ', c = '7' , d = '가', e = 'ㅡ';
		//1. char형 변수 a가 'q' 또는 'Q' 일 때 true
		System.out.println(a=='q' || a=='Q');
		
		//2. char형 변수 b가 공백이나 탭이 아닐 때 true
		System.out.println(b!=' ' && b!='\t' );
		
		//3. char형 변수 c가 '0' ~ '9'일 때 true
		System.out.println(c<='9' && c>='0');
		
		//4. char형 변수d가 영문자(대문자 또는 소문자)일 때 true
		System.out.println(d<=90 && d>=65 || d<=122 && d>=97);
		System.out.println(d>='a' && d<= 'z' || d>='A' && d <='Z');
		
		//5. char형 변수 e가 한글일 때 true (
		System.out.println(e<=55203 && e>=44032 || e>=12593 && e<=12643);
		System.out.println(e >= '가' && e <= '힣');
		System.out.println(0xAC00);
		
		//6. 사용자가 입력한 문자열이 quit일 때 true
		System.out.print("입력하세요 >");
		String answer = new Scanner(System.in).next();
		System.out.println(answer.equalsIgnoreCase("quit"));