C语言
#include<stdio.h>int main()
{
int m, k = 0, s = 0;
for(m = 1;m <= 4; m++)
{
switch(m%4)
{
case 0:
case 1:s+=m;break;
case 2:
case 3:s-=m;break;
}
k+=s;
}
printf("%d",k);
}
执行过程:
初始化:m = 1; k = 0, s = 0
①:m%4 = 1执行case1;s=0+1=1;break;
k = k+s = 1;
②:m++=2;m%4 = 2;执行case2;s=1-2=-1;
k = k+s = 0;
③:m++=3;m%4 = 3;执行case3;s=-1-3=-4;
k = k+s = -4;
④:m++=4;m%4 = 3;执行case4;s=-4+4=0;
k = k+s = -4
上述程序的运行结果是这样的吗?
是的,k 最终是 -4
页:
[1]