问题
单项选择题
设x和y均为int型变量,则执行下面的循环后,y值为( )。
public class Sun
public static void main(String args[ ])
int x, y;
for (y=1, x=1; y<=50; y++)
if(x>=10)
break;
if (x%2==1)
x+=5;
continue;
x-=3;
System. out.println (y);
A.2
B.4
C.6
D.8
答案
参考答案:C
解析: 该题考查for循环和if条件语句的嵌套应用。当y=1,x=1时,不满足第1个if语句向下继续执行第2个if语句,x=6,继续执行for循环:当y=2,x=6时,不满足第1个和第2个if语句,x=3,继续执行for循环;当y=3,x=3时,不满足第1个if语句向下继续执行第2个if语句,x=8,继续执行for循环;一直执行下去,直到y=6,x=12是满足第1个 if语句退出for循环。故本题答案是C。