===========================
2022-7-19
===========================
一.温故知新
分支语句
if
if else
语句1
if(条件1)
{
语句2
}
else if(条件2)
{
语句3
}
else if(条件3)
{
语句4
}
语句5
switch(变量)
{
case 整常量1: 语句1; break;
case 整常量2: 语句2; break;
case 整常量3: 语句3; break;
default : 语句4; break;
}
循环语句
for(; ;)//这是一个死循环
for(表达式1; ;表达式2)
等价于
for(表达式1; 1; 表达式2)
int i = 0, j = 0;
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| ```C #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { int i = 0, j = 0;
for(i = 0; i < 4; i++) { for(j = 0; j < 5; j++) { printf("[%d %d]", i, j); } printf("\n"); }
return 0; }
|
打印99乘法表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { int i = 1, j = 1;
for(i = 1; i < 10; i++) { for(j = 1; j < 10; j++) { if(j <= i) printf("%d X %d = %d ", i, j, i*j); } printf("\n"); }
return 0; }
|
99乘法表

“百钱百鸡”问题,百钱买百鸡,公鸡1个3块钱,母鸡1个2块钱,小鸡3个一块钱,问公鸡,母鸡,小鸡各多少个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { int g = 0, m = 0, x = 0;
for(g = 0; g < 33; g++) { for(m = 0; m < 50; m++) { for(x = 0; x < 100; x += 3) { if((g*3+m*2+x/3==100) && (g+m+x==100)) printf("G:%d M:%d X:%d\n", g, m, x); } } }
return 0; }
|
随机函数
rand(); //功能:就是用来产生随机数的
rand() % 1000; //[0 ~ 999]
rand() % 100 - 50;//[-50 ~ 49]
srand(); //功能:可以保证每一次产生的随机数可变
srand函数在调用时需要有可变的因子
time(NULL); //时间种子
getpid(); //进程种子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/types.h> #include <unistd.h>
int main(int argc, char *argv[]) { int sum = 0; int i = 0;
srand(getpid());
for(i = 0; i < 5; i++) { sum = rand() % 100 - 50; printf("sum = %d\n", sum); }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/types.h> #include <unistd.h>
int main(int argc, char *argv[]) { int T = 0; int guess = 0; int num = 1;
srand(getpid()); T = rand() % 10;
for(num = 1; ; num++) { scanf("%d", &guess); if(T > guess) printf("[%d] Small!\n", num); else if(T < guess) printf("[%d] Big!\n", num); else { printf("[%d] Bingo!\n", num); break; } }
return 0; }
|
循环语句
do...while循环
1.do while是C语言的关键字
2.do...while循环的使用方法
语句1
do
{
要循环的语句
}while(循环条件);
语句2
3.do...while循环的执行流程
1 2 3 4 5 6
| |-------<---------<--------- | ^ V | 语句1->要循环的语句->循环条件的判断(真)-> | (假)->语句2
|
4.while循环与do...while循环的对比
while : 先执行循环条件的判断,再执行循环的语句
do...while : 先执行循环的语句,再执行循环条件的判断
while : 可以一次也不执行循环的语句
do...while : 至少会执行一次循环的语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { int i = 1; int sum = 0;
while(i <= 100) { sum += i; i++; }
printf("sum = %d\n", sum);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { int i = 0, j = 0;
while(i < 4) { j = 0; while(j < 5) { printf("[%d %d]\n", i, j); j++; } i++; }
return 0; }
|
1 2 3 4 5 6 7 8
| #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) {
return 0; }
|
跳转语句 : break continue goto return
1.break continue goto return都是C语言的关键字
2.break关键字
break只能出现在循环结构或者switch语句中
使用在循环结构中是用来跳出循环的
使用在switch语句中是用来跳出switch
如果break关键字出现在嵌套结构中,只作用于最近的那一层
3.continue关键字
continue关键字只能出现在循环结构中
使用在循环结构中跳过本次循环,继续下一次循环
4.return关键字
return关键字一般来说出现在函数最后的位置
当一个函数碰到return关键字,这个函数会结束,
会把返回值返回给函数的调用者
------------------------------------------
return返回的返回值有两种含义
1)返回的是运算的结果
2)返回的是函数运行的状态
return 0; 代表函数运行正常
return 正数; 代表函数运行有异常
return 负数; 代表函数运行有错误
------------------------------------------
注意 : return关键字一次只能返回一个值
5.goto关键字(太灵活了)
可以轻易改变代码的结构
语句1
标识符:
语句2
goto 标识符;
语句3;
标识符的命名要求和变量的命名要求一致
(标识符可以和变量重名,但是不要这样使用)
同一个标识符不能出现在多个位置
goto不能跨函数使用
*/
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { if(1) { break; }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { int i = 0;
for(i = 0; i < 5; i++) { if(i == 2) continue; printf("i = %d\n", i); }
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <stdio.h> #include <stdlib.h>
int main(int argc, char *argv[]) { if(!1) { flag: printf("Hello World!\n"); } else { printf("天气好热!\n"); goto flag; }
return 0; }
|