1summing.c
/* summing.c -- 根据用户键入的整数求和 */
#include <stdio.h>
int main(void)
{
long num;
long sum = 0L; /* 把sum 初始化为0 */
int status;
printf("Please enter an integer to be summed ");
printf("(q to quit): ");
status = scanf("%ld", &num);
while (status == 1) /* == means "is equal to" */
{
sum = sum + num;
printf("Please enter next integer (q to quit): ");
status = scanf("%ld", &num);
}
printf("Those integers sum to %ld.\n", sum);
return 0;
}
2 when.c
// when.c -- 何时退出循环
#include <stdio.h>
int main(void)
{
int n = 5;
while (n < 7) // line 7
{
printf("n = %d\n", n);
n++; // line 10
printf("Now n = %d\n", n); // line 11
}
printf("The loop has finished.\n");
return 0;
}
3 while1.c
/* while1.c -- 注意花括号的使用 */
/* 糟糕的代码创建了一个死循环 */
#include <stdio.h>
int main(void)
{
int n = 0;
while (n < 3)
printf("n is %d\n", n);
n++;
printf("That's all this program does\n");
return 0;
}
4 while2.c
/* while2.c -- 注意分号的位置 */
#include <stdio.h>
int main(void)
{
int n = 0;
while (n++ < 3); /* line 7 */
printf("n is %d\n", n); /* line 8 */
printf("That's all this program does.\n");
return 0;
}
5 cmpflt.c
// cmpflt.c -- 浮点数比较
#include <math.h>
#include <stdio.h>
int main(void)
{
const double ANSWER = 3.14159;
double response;
printf("What is the value of pi?\n");
scanf("%lf", &response);
while (fabs(response - ANSWER) > 0.0001)
{
printf("Try again!\n");
scanf("%lf", &response);
}
printf("Close enough!\n");
return 0;
}
6 t_and_f.c
/* t_and_f.c -- C中的真和假的值 */
#include <stdio.h>
int main(void)
{
int true_val, false_val;
true_val = (10 > 2); // value of a true relationship
false_val = (10 == 2); // value of a false relationship
printf("true = %d; false = %d \n", true_val, false_val);
return 0;
}
7 truth.c
// truth.c -- 哪些值为真
#include <stdio.h>
int main(void)
{
int n = 3;
while (n)
printf("%2d is true\n", n--);
printf("%2d is false\n", n);
n = -3;
while (n)
printf("%2d is true\n", n++);
printf("%2d is false\n", n);
return 0;
}
8 trouble.c
// trouble.c -- 这里误用了=导致了死循环
#include <stdio.h>
int main(void)
{
long num;
long sum = 0L;
int status;
printf("Please enter an integer to be summed ");
printf("(q to quit): ");
status = scanf("%ld", &num);
while (status = 1)
{
sum = sum + num;
printf("Please enter next integer (q to quit): ");
status = scanf("%ld", &num);
}
printf("Those integers sum to %ld.\n", sum);
return 0;
}
9 boolean.c
// boolean.c -- 使用 _Bool类型的变量
#include <stdio.h>
int main(void)
{
long num;
long sum = 0L;
_Bool input_is_good;
printf("Please enter an integer to be summed ");
printf("(q to quit): ");
input_is_good = (scanf("%ld", &num) == 1);
while (input_is_good)
{
sum = sum + num;
printf("Please enter next integer (q to quit): ");
input_is_good = (scanf("%ld", &num) == 1);
}
printf("Those integers sum to %ld.\n", sum);
return 0;
}
10 sweetie1.c
// sweetie1.c -- 一个计数循环
#include <stdio.h>
int main(void)
{
const int NUMBER = 22;
int count = 1; // i初始化
while (count <= NUMBER) // 测试
{
printf("Be my Valentine!\n"); // 行为
count++; // 更新计数
}
return 0;
}
11 sweetie2.c
// sweetie2.c -- 使用for循环的计数循环
#include <stdio.h>
int main(void)
{
const int NUMBER = 22;
int count;
for (count = 1; count <= NUMBER; count++)
printf("Be my Valentine!\n");
return 0;
}
12 for_cube.c
/* for_cube.c -- 使用for循环创建一个立方表 */
#include <stdio.h>
int main(void)
{
int num;
printf(" n n cubed\n");
for (num = 1; num <= 6; num++)
printf("%5d %5d\n", num, num*num*num);
return 0;
}
13 postage.c
// postage.c --邮资
#include <stdio.h>
int main(void)
{
const int FIRST_OZ = 46; // 2013 邮资
const int NEXT_OZ = 20; // 2013 邮资
int ounces, cost;
printf(" ounces cost\n");
for (ounces=1, cost=FIRST_OZ; ounces <= 16; ounces++,cost += NEXT_OZ)
printf("%5d $%4.2f\n", ounces, cost/100.0);
return 0;
}
14 zeno.c
/* zeno.c -- 求序列的和*/
#include <stdio.h>
int main(void)
{
int t_ct; // 项计数
double time, power_of_2;
int limit;
printf("Enter the number of terms you want: ");
scanf("%d", &limit);
for (time=0, power_of_2=1, t_ct=1; t_ct <= limit;t_ct++, power_of_2 *= 2.0)
{
time += 1.0/power_of_2;
printf("time = %f when terms = %d.\n", time, t_ct);
}
return 0;
}
15 do_while.c
/* do_while.c -- 出口条件循环 */
#include <stdio.h>
int main(void)
{
const int secret_code = 13;
int code_entered;
do
{
printf("To enter the triskaidekaphobia therapy club,\n");
printf("please enter the secret code number: ");
scanf("%d", &code_entered);
} while (code_entered != secret_code);
printf("Congratulations! You are cured!\n");
return 0;
}
16 entry.c
/* entry.c -- 出口条件循环 */
#include <stdio.h>
int main(void)
{
const int secret_code = 13;
int code_entered;
printf("To enter the triskaidekaphobia therapy club,\n");
printf("please enter the secret code number: ");
scanf("%d", &code_entered);
while (code_entered != secret_code)
{
printf("To enter the triskaidekaphobia therapy club,\n");
printf("please enter the secret code number: ");
scanf("%d", &code_entered);
}
printf("Congratulations! You are cured!\n");
return 0;
}
17 rows1.c
/* rows1.c -- u使用嵌套循环 */
#include <stdio.h>
#define ROWS 6
#define CHARS 10
int main(void)
{
int row;
char ch;
for (row = 0; row < ROWS; row++) /* line 10 */
{
for (ch = 'A'; ch < ('A' + CHARS); ch++) /* line 12 */
printf("%c", ch);
printf("\n");
}
return 0;
}
18 rows2.c
// rows2.c -- 依赖外部循环的嵌套循环
#include <stdio.h>
int main(void)
{
const int ROWS = 6;
const int CHARS = 6;
int row;
char ch;
for (row = 0; row < ROWS; row++)
{
for (ch = ('A' + row); ch < ('A' + CHARS); ch++)
printf("%c", ch);
printf("\n");
}
return 0;
}