THIRD DAY LEARNING (CYCLE C 语言)
循环
循环结构: 用于执行大量相同的操作
三种循环结构
1.for循环
2.while循环
3.do....while循环
for循环
/*
for(1; 2; 3){
4
}
*/
1.循环变量初始化
2.循环条件
3.循环增量
4.循环体
for循环执行过程
第一次循环: 1234
以后的循环: 234
注: 循环变量初始化只执行一次; 当循环条件不满足时, 循环结束
打印5次
/*
for (int i = 0; i < 5; i++) {
printf("Hello, World!\n");
}
*/
打印1~5的数
for (int i = 1; i <= 5; i++) {
printf("%d ",i);
}
printf("\n");
计算1~100的和
/*
int sum = 0;
for (int i = 0; i <= 100; i++) {
sum += i;
}
printf("sum = %d ", sum);
printf("\n");
*/
过七
0~100之间, 7的倍数和包含7的数(十位是7,个位是7)的数, 打印"过",其他打印这个数
int i = 0;
for (i = 1
; i <= 100; i++) {
if (i % 7 == 0 || i / 10 == 7 || i % 10 == 7) {
printf("过 ");
}
else{
printf("%d ", i);
}
}
printf("\n");
死循环: 循环一直执行, 无法得到停止
随机数 arc4random(): 随机返回一个正整数
unsigned int: 无符号整型, 正整数, 用%u打印
unsigned int number = arc4random();
printf("number = %u\n",number);
产生5个随机数, 并打印
for (int i = 0; i < 5; i++) {
unsigned int number = arc4random();
printf("number = %u\n",number);
}
思考: 怎么产生5个0~100之间的随机数
for (int i = 0; i < 5; i++) {
unsigned int number = arc4random() % 101;
printf("number = %u\n", number);
}
产生[x, y]之间的随机数公式
arc4random() % (y - x + 1) + x
产生8个,[29, 78]的随机数, 并求最大值
unsigned int number;
int max = 29;//存最大值
for (int i = 0; i < 8; i++) {
//随机数
number = arc4random() % 50 + 29;
printf("%u ", number);
//比较随机数max
//1
if (number > max) {
max = number;
}
//2
//max = number > max ? number : max;
}
printf("max = %d\n", max);
求某个范围的最大值,初值赋成最小值
求某个范围的最小值,初值赋成最大值
for的变形
int i = 0;
for (; i < 5; ) {
printf("Hi\n");
i++;
}
for (int i = 0, j = 0; i < 5 && j < 10; i++, j++) {
printf("Hello\n");
}
外层循环执行一次, 内层循环执行一遍
打印4遍 1 2 3 4
for (int j = 0; j < 4; j++) {
for (int i = 1; i <= 4; i++) {
printf("%d ", i);
}
printf("\n");
}
九九乘法表
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++ ) {
printf("%dx%d=%2d ", i, j, i * j);
}
printf("\n");
}
4 3 2 1
3 2 1
2 1
1
for (int j = 0; j <= 4; j++) {
for (int i = 4-j; i >= 1; i--) {
printf("%d ", i);
}
printf("\n");
}
注: 循环嵌套一般不超过3层
再循环中使用, break 和 continue
break, 跳出本层循环
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
printf("%d\n", i);
}
continue, 跳过本次循环, 加速循环的执行
for (int i = 1; i <= 10; i++){
if (i % 2 == 0) {
continue;
}
printf("%d ", i);
}
printf("\n");
while循环
/*
while (循环条件) {
循环体
}
打印1~5
for (int i = 1; i <= 5; i++) {
printf("%d ",i);
}
printf("\n");
int i = 1;
while (i <= 5) {
printf("%d ",i);
i++;
计算1~100之间的偶数之和
int i = 1, sum = 0;
while (i <= 100) {
if (i % 2 == 0) {
sum += i;
}
i++;
}
printf("sum = %d\n",sum);
产生20个[111, 333]随机数, 求最大值最小值, 平均值
int i = 0,max = 111, min = 333, sum = 0;
unsigned int m = 0;
while (i < 20) {
m=arc4random() % (333 - 111 + 1) + 111;
printf("%u ",m);
//最大值
if (m > max) {//逐个比较
max = m ;
}
//max = m > max ? m : max;
//最小值
if (m < min) {//逐个比较
min = m ;
}
sum += m;
i++;
}
printf("\n");
printf("Max = %d\n", max);
printf("Min = %d\n", min);
printf("Average = %.2f\n", sum / 20.0);
do...while循环
/*
do {
循环体
} while (循环条件);
*/
执行顺序: 先执行一次循环体, 再判断循环条件, 如果成立, 执行循环体, 如果不成立, 循环结束
注:do...while循环至少一次循环体
打印5遍Hello
int i = 0;
do {
printf("Hello ");
i++;
} while (i < 5);
printf("\n");
for, while, do...while
知道循环的次数, 使用for循环
知道循环的条件, 使用while循环
do...while循环, 比while循环, 至少执行一次循环体
输入一个整数, 计算其位数
int count = 0, number = 0;//count:位数
printf("Input a number:");
scanf("%d", &number);
1.while
if (number == 0) {
count = 1;
} else {
while (number != 0 ) {
number = number / 10;
count++;
}
}
printf("%d是%d位数\n", number, count);
2.do...while
do {
number /= 10;
count++;
} while (number != 0);
printf("%d是%d位数\n", number, count);