问题遇到的现象和发生背景
C语言在表达式2中可以使用逻辑表达式吗?我尝试了下使用逻辑表达式作为结束条件,但是没有达到相应的效果。
结束条件是:i <= 1000 或者 total >= 10000
问题相关代码,请勿粘贴截图
int main()
{
float amount, aver, total;
int i;
float SUM = 10000;
for (i = 1, total = 0; (i <= 1000) || (total >= SUM) ; i++)
{
printf("please enter amount:");
scanf_s("%f", &amount);
total = total + amount;
printf("总和:%f 目标值:%f 次数:%d\n",total,SUM,i);
//if (total >= SUM) break;
}
aver = total / i;
printf("num=%d\naver=%10.2f\n", i, aver);
return 0;
}
运行结果及报错内容
输入了3次数据,3000、6000、6000,总和已经超过了10000但是循环还未结束。