[출력결과]
2 4 6 8 10 12 14 16 18 20 22 24 26 28
public class Java100_license_CosPattern2 {
public static void main(String[] args) {
// [1] : 변수 선언
int num=1;
// [2] : 반복문 돌면서 홀수 인지 체크--> 홀수면 Pass(continue)
while(num<=30) {
if(num%2!=0) {
num++;
continue;
}
System.out.print(num+" ");
num++;
}
}
}
0 2 4 6 8
10 12 14 16 18
20 22 24 26 28
30 32 34 36 38
public class Java100_license_CosPattern3 {
public static void main(String[] args) {
// [1] : 이중 반복문
for(int i=0;i<=3;i++) {
for(int j=0;j<5;j++) {
// 출력값
int output = i*10+ j*2;
System.out.print(output+" ");
}
System.out.println();
}
}
}
public class Java100_license_CosPattern1 {
public static void main(String[] args) {
// [1] : 이중 반복문
for(int i=0;i<=3;i++) {
for(int j=0;j<10;j++) {
// 출력값
int output = i*10+ j;
// 짝수만 출력
if(output%2!=0)
continue;
else
System.out.print(output+" ");
}
System.out.println();
}
}
}
-첫 for문은 0~3 세번 돌면서 행 3개를 만들어 준다.
-두번째 for문은 1~9 9번 돌면서 정수를 9까지의 수를 만든다.
-출력값 변수를 만들어 행(i)이 증가할수록(3번) 정수의 십의 자리수가 증가하도록 연산해준다.
-조건문 if를 통해 출력값이 짝수일때 continue명령에 의해 다음 반복문(j++)으로 넘어가도록 조건(연산)을 걸어준다.
<출력결과>
0
10
20
30
: 여러개의 중첩된 반복문을 한번에 빠져나와야 할때 쓰인다.
라벨명: for(조건){문장}
if(조건){continue 라벨명;}
-j가 1이면 outerloop label이 선언된 바깥쪽 for문으로 분기하여 그 다음 단계부터 다시 수행
public class Java100_license_CosPattern4 {
public static void main(String[] args){
// [1] : 이중 반복문
outerloop:
for(int i=0;i<=3;i++) {
for(int j=0;j<10;j++) {
// 출력값
int output = i*10+ j;
// 짝수만 출력
if(output%2!=0) {
if(j == 1) {
System.out.println();
continue outerloop;
}
continue;
}
else
System.out.print(output+" ");
}
System.out.println();
}
}
}
-j가 0일때 else로 넘어가서 0를 출력한 후 다음 루프로 넘어가 j가 1이 된다.
-j가 홀수 1이므로 첫번째 if문에서 먼저 개행를 해준 뒤 continue에 의해 outerloop, 즉 가장 첫번째 for문으로 넘어가서 i가 1이된다.
-다시 j가 0부터 초기화되어 루프를 돌고 출력값 ouput이 10으로 연산되어 출력된다.
-위 과정이 i가 3이 될때 반복되어 0 10 20 30이 차례대로 개행되어 출력된다.