while (条件) {
// 循环体,当条件为真时执行
}
#include <stdio.h>
int main()
{
int fahr, cel;
int lower, upper, step;
lower = 0; // 最低温度
upper = 300; // 最高温度
step = 20; // 步长
fahr = lower;
while (fahr <= upper)
{
cel = 5 * (fahr - 32) / 9;
printf("%d\t%d\n", fahr, cel);
fahr = fahr + step;
}
return 0;
}
➜
0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148
for (初始化; 条件; 更新) {
// 循环体,当条件为真时执行
}
#include <stdio.h>
int main()
{
int fahr, cel;
int lower, upper, step;
lower = 0; // 最低温度
upper = 300; // 最高温度
step = 20; // 步长
for (fahr = lower; fahr <= upper; fahr = fahr + step)
{
cel = 5 * (fahr - 32) / 9;
printf("%d\t%d\n", fahr, cel);
}
return 0;
}