Given the following main method:
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
題解
do-while迴圈第一次執行,輸出「5 」並將num變數所儲存的數值再減一,num=5-1=4。4不等於0,因此while條件式不成立,迴圈不再執行。所以只有輸出「5 」。