你该逆袭了
四、for循环
注意:const int NUMBER = 22; 在以后的代码中,要学会使用。
1、利用 for 的灵活性
(1)、可以用 字符 代替数字计数。
(2)、除了测试迭代次数外,还可以测试其他条件。
(3)、可以让递增的量几何增长,而不是算数增长。
(4)、for循环中的 3 个表达式可以是不同的量。
(5)、可以省略表达式,但是不能省略 分号 ’ ; '。
#include <stdio.h>
int main()
{
int ans, n;
ans = 2;
for (n = 3; ans <= 25;)
ans = ans * n;
printf("n=%d;ans=%d.\n", n, ans);
return 0;
}
//ans = 2;
//ans = 2*3 = 6; 小于25,满足循环条件
//ans = 6*3 = 18; 小于25,满足循环条件
//ans = 18*3 = 54; 不满足判断条件,循环终止
(6)、for循环只对第 1 个表达式 求值一次 或 执行1次 ;第 1 个表达式不一定是给变量赋初值。
#include <stdio.h>
int main()
{
int num = 0;
for (printf("keep entering number\n"); num != 6;)
{
scanf("%d", &num);
}
printf("that's the one i want\n");
return 0;
}
(7)、循环体中的行为可以改变循环头中的表达式。
五、逗号运算符
#include <stdio.h>
int main()
{
const int FIRST_OZ = 46; // 邮资
const int NEXT_OZ = 20; // 邮资
int ounces = 0;
int cost = 0;
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;
}
逗号运算符 在 for循环 中,经常使用。
作用1:
作用1:保证了被它分隔的表达式从左往右求值。(换言之:逗号 是一个 序列点,所以逗号左侧项的所有副作用都在程序执行逗号右侧项之前发生。)
作用2:
作用2:整个逗号表达式的值是 右侧项的值。
深刻理解的案例
逗号也用作 分隔符,此时就不是 逗号运算符了!
总结
1、逗号 运算符通常在 for 循环头的表达式中用于包含更多的信息。
2、逗号运算符把两个表达式连接成一个表达式,并保证最左边的表达式最先求值。(从左到右)
3、整个逗号表达式的值是逗号右侧表达式的值。
六、do while 循环
for 、while 是 入口条件循环。
do while 是 出口条件循环。
七、嵌套循环
//P162 程序清单6.17
//嵌套循环
//常用于按行和列显示数据。
//也就是说:一个循环处理一行中的所有列,另一个循环处理所有的行。
#include <stdio.h>
#define ROWS 6
#define CHARS 10
int main()
{
int row = 0;
char ch = '0';
for (row = 0; row < ROWS; row++)
{
for (ch = 'A'; ch < ('A' + CHARS); ch++) //记得加上 圆括号
{
printf("%c", ch);
}
putchar('\n');
}
return 0;
}
1、嵌套变式
#include <stdio.h>
#define ROWS 6
#define CHARS 6
int main()
{
int row = 0;
char ch = '0';
for (row = 0; row < ROWS; row++)
{
for (ch = 'A'+ row; ch < ('A' + CHARS); ch++)
{
printf("%c", ch);
}
putchar('\n');
}
return 0;
}
八、数组简介
下标、索引、偏移量:数组元素的数字
1、在 for循环 中使用 数组
// 使用循环处理数组
#include <stdio.h>
#define SIZE 10
#define PAR 72 //handicap 它是平均分与标准分的差值
int main()
{
int index = 0;
int score[SIZE] = { 0 };
int sum = 0;
float average = 0;
printf("enter %d golf scores:\n", SIZE);
for (index = 0; index < SIZE; index++)
{
scanf("%d", &score[index]); // 注意:一定要加上 取地址符号 &
//读取 10 个分数
}
printf("the scores read in are as follows:\n");
for (index = 0; index < SIZE; index++)
{
printf("%5d", score[index]); //验证输入
}
printf("\n");
for (index = 0; index < SIZE; 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;
}
(1)、较好的编程风格
一共有三点:
九、(非常重要)使用 函数返回值 的循环示例
//P167 程序清单6.20
//计算数的整数幂
#include <stdio.h>
double power(double n, int p); //函数原型 //末尾要有 分号
int main()
{
double x = 0;
double xpow = 0;
int exp = 0;
printf("enter a number and the positive integer power");
printf(" to which\nthe number will be raised.Enter (q");
printf(" to quit)");
while (scanf("%lf%d", &x, &exp) == 2) //重点!!!!
//不仅仅是输入 q 就结束程序,只要输入的不符合 %lf%d 这样的格式,就会立马结束程序
{
xpow = power(x, exp); //函数调用
printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
printf("enter next pair of numbers or q to quit.\n");
}
printf("hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p) //函数定义
{
double pow = 1;
int i = 0;
for (i = 1; i <= p; i++)
{
pow *= n;
}
return pow; //返回 pow 的值
}
1、对该程序的解析(非常的重要)
三个步骤:设计算法、在函数中表示算法并返回计算的结果、提供一个测试函数的便利方法。
首先,for 循环,先是正整数进行相乘
for (index = 1; index < SIZE; index++) // SIZE 指数:循环相乘的次数
{
pow *= j; //j 底数:不断的相乘
}
算法确定下来了,接下来决定 使用何种数据类型
double power(double dishu, int index)
{
int i = 0;
double pow = 1; //必须是 1
for (i = 0; i < index; i++)
{
pow *= dishu;
}
return pow;
}
2、程序分析
3、使用带返回值的函数
十、关键概念
十一、本章小结
微软雅黑字体
黑体
3号字
4号字
红色
绿色
蓝色