如何控制循环体中的某一条语句在多次循环中只执行一次呢?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define SIZE 10
#define PAR 72
int main(void)
{
int index, score[SIZE];
int sum = 0;
float average;
printf("Enter %d golf scores:\n", SIZE);
for (index = 0; index < SIZE; index++) {
scanf("%d", &score[index]);
printf("%5d\n", score[index]);
sum += score[index];
}
average = (float)sum / SIZE;
printf("Sum of scores=%d,average=%.2f\n", sum, average);
printf("That's a handicap of %.0f.\n", average - PAR);
return 0;
}
例如我想在for循环中控制printf("The scores read in are as follows:\n")这条语句仅仅在第一次执行循环时打印。
if(index==0) printf("The scores read in are as follows:\n")
加个if判断应该可以
在你的for循环中判断
if(index==0){
}
在括号中的就是只执行一次。同理:将0替换为其他值(n)时,是在第n次执行