来知道电脑站

您的位置: 主页>编程> 初学C语言,下面程序不明白,请高手分析一下,谢谢.

初学C语言,下面程序不明白,请高手分析一下,谢谢.

 kscairon525052 

初学C语言,下面程序不明白,请高手分析一下,谢谢.


我是一位自学的初学者,对这程序有点不明白,这是简略的程序,为什么结果是5,而不是3,是我那里写错了吗?这高手指教分析,谢谢.
#include <stdio.h>

int main (void)
{
int counter;
int total;

counter=1;
total=0;

while (counter<=2){
printf ("%d ",counter );

total=total counter;
}

printf ("1~2的和为:%d ",total);

return 0;
}

解决方法



开始时:counter=1;满足循环条件,开始执行while循环,counter 的意思是:先打印当前counter 的值,再让counter=counter 1;既是第一次执行while 循环之后,printf :1,counter的值变为2;total=0 2=2;
执行第二次循环,printf:2,counter的值变为3,total=2 3=5;
所以,你最后printf 的值是 5.

请教专业人士

当counter=1的时候,执行counter 时会等于2,这时counter赋值给total等于2
当couter=2时,执行counter 时会等于3,于是counter累加把值赋给total时

就会等于5

上一篇:关于物流方面数据库的应用    下一篇:没有了

编程相关文章

编程热门文章

编程推荐文章