ocajp 자격증 (Oracle Certified Associate Ja

자바 ocjap 문제 해설 - do while

xemaker 2020. 2. 24. 20:48

Given the following main method:

public class DoWhile14 {
public static void main(String[] args) {
int num=5;
do {
System.out.print(num--+" ");
}while(num==0);
}
}

What is the result?

A. 5 4 3 2 1 0

B. 5 4 3 2 1

C. 4 2 1 

D. 5

E. Nothing is printed

 

정답: D

해설:

do while은 do를 먼저 실행하고 while문 이 참이면 do를 계속 실행한다. while문에 거짓이면 실행안한다.

여기서는 do를 먼저 실행해서 5를 출력하고 while문에서 num이 0이 아니니까 빠져나간다.

결과: