C语言的语句分为 5 类
- 1:表达式语句
- 2:函数调用语句
- 3:控制语句
- 4:复合语句
- 5:空语句
控制语句:用于控制程序的执行流程,以实现程序的各种结构方式,它们由特定的语句定义符组成,C语言有9种控制语句 。
if(表达式){
语句
}
1.0 if 语句
if 语句
if ------------else 语句
if 实现多分支语句
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int age = 10;
scanf("%d", &age);
if (age < 18) {
printf("青少年\n");
}
else if(age >= 18 && age <= 28){
printf("青年\n");
}
else if (age >= 28 && age < 40) {
printf("中年\n");
}
else if (age > 40 && age < 60) {
printf("壮年\n");
}
else if (age >= 60 && age <= 100) {
printf("老年\n");
}
else {
printf("老寿星\n");
}
return 0;
}
注:C语言的概念 0 表示假,1表示真,if else 语句在输出多条语句时需要添加{ }
规范编码:防止出现一些低级的错误
【变量的命名规则】
1:变量的命名要规范,命名见名知义,不能是C语言中的关键字,有一点理解障碍 。
好的代码风格,更容易让阅读者理解
C 语言练习判断一个数是否为奇数
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int num = 0;
scanf("%d", &num);
if (num % 2 == 0) {
printf("不是奇数num = %d", num);
}
else {
printf("是奇数num = %d", num);
}
return 0;
}
【输出1-100之间的奇数】
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int num = 0;
while (num < 100) {
num++;
if (num % 2 != 0) {
printf("%d\n", num);
}
}
return 0;
}
C语言学习方法总结
1:多练才是解药
2:练习在熟悉语法,语法熟悉才能无障碍的编写代码
3:练习就是在锻炼编程的思维,把实际问题转换为编写代码的能力
4:学会画图,理解内存,理解指针
画图可以理清思路
画图可以辅助理解强化理解
学会调试:调试可以让我们更好的理解和感知代码
借助调试:可以让我们找出代码中的bug
...............
1.1 switch case 语句
C语言的 Switch语句
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int day = 5;
scanf("%d", &day);
if (1 == day) {
printf("星期一");
}
else if (2 == day) {
printf("星期二");
}
else if (3 == day) {
printf("星期三");
}
else if (4 == day) {
printf("星期四");
}
else if (5 == day) {
printf("星期五");
}
else if (6 == day) {
printf("星期六");
}
else if (7 == day) {
printf("星期日");
}
return 0;
}
Switch 语句实现控制输出:switch语句后面的表达是必须是整型的不能是其他类型,case后面也必须是整型常量表达式 。
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int day = 5;
scanf("%d", &day);
switch (day) {
case 1:
printf("星期一");
break;
case 2:
printf("星期二");
break;
case 3:
printf("星期三");
break;
case 4:
printf("星期四");
break;
case 5:
printf("星期五");
break;
case 6:
printf("星期六");
break;
case 7:
printf("星期日");
break;
}
return 0;
}
【一种不同的写法】
C语言的编程习惯
注:case 后面是可以加字符的,因为字符后面是ASCII值,相当于也是一个整型的数
注:switch 语句是可以嵌套使用的,switch语句中的break只能跳出自己所在的switch语句
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int n = 1;
int m = 2;
switch (n) {
case 1: m++; // 2
case 2: n++; // 1
case 3:
switch (n) { // n == 2
case 1: n++;
case 2: m++; n++; // m == 3 n == 2
break;
}
case 4:
m++; // m == 4
break;
default:
break;
}
printf("m = %d,n = %d\n",m,n); // 5 3
return 0;
}
2.0 while循环语句
break关键字和continue关键字,break关键字的作用是跳出break关键字后面的语句【Break后面的语句不会得到执行】,continue是跳出后面的语句然后执行下一条语句,break在while语句中的用法是永久的中止循环。
int main() {
int i = 0;
// 在while循环中break用于永久的中止循环
// 在while循环中continue的作用是跳过continue后面的代码看是否进行下一次循环
while (i <= 10) {
if (i = 5) {
break;
}
}
printf("%d\n",i++);
i++;
return 0;
}
break的使用以及运行结果:
continue运用到while循环当中
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
// continue 的作用是跳过本次循环后面的代码,直接判断部分,进行下一次判断
int main() {
int i = 0;
// 在while循环中continue的作用是跳过continue后面的代码看是否进行下一次循环
while (i <= 10) {
i++;
if (5 == i) {
continue;
}
printf("%d\n", i);
}
return 0;
}
3.0 getchar()和putchar()
定义:getchar 读取正确的话会返回数据,如果读取错误会返回eof,eof表示的是文件结束的标志是-1 ,使用getchar的时候输入什么就会输出什么,敲完a再加回车的时候还是会输出换行的。【getchar和putchar的方法】
【代码的一种更新方式】
【修改后的方法】
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
char password[20] = { 0 };
printf("请输入密码:>");
scanf("%s", password);
int ch = 0;
while ((ch = getchar()) != '\n') {
;
}
getchar();
printf("请确认密码(Y/N):>");
int ret = getchar();
if ('Y' == ret) {
printf("YES\n");
}
else {
printf("NO\n");
}
return 0;
}
4.0 for循环语句
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int i = 1;
while (i <= 10) {
printf("%d ", i);
i++;
}
return 0;
}
for 循环语句的基础语法
【for循环打印输出1-10】
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int i = 0;
for (i = 0; i < 10; i++) {
printf("%d ", i);
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int i = 0;
for (i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
printf("%d ", i);
}
return 0;
}
注:
不要在for循环体内修改循环变量,防止for循环失去控制
建议for循环的循环控制变量采取前面闭合后面开区间的写法
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int i = 0;
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
for (i = 0; i <= 9; i++) {
printf("%d ", arr[i]);
}
return 0;
}
注:for循环判断部分的代码省略会导致for循环判断部分的代码恒定成立
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int i = 0;
for (i = 0; ; i++) {
printf("%d ", arr[i]);
}
return 0;
}
for循环的嵌套调用
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int arr[10] = {1,2,3,4,5,6,7,8,9,10 };
int i = 0;
for (i = 0;i< 10; i++) {
for (int j = 0; j < 10; j++) {
printf("hehe\n");
}
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int arr[10] = {1,2,3,4,5,6,7,8,9,10 };
int i = 0;
int j = 0;
for (;i< 3; i++) {
for (; j < 3; j++) {
printf("hehe\n");
}
}
return 0;
}
补充知识点:
使用多个变量控制我们的循环是可以的
以下的这代码循环的次数为0次
5.0 do --- while循环语句
在C语言中do ----while循环一上来就会将程序执行一次
do ----------while循环基础打印输出1-10
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int i = 1;
do {
printf("%d ", i);
i++;
}while (i <=10);
return 0;
}
在do ----------while循环中使用break语句
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int i = 1;
do {
i++;
if (i == 5)
break;
printf("%d ", i);
}while (i <=10);
return 0;
}
在do--------while循环中使用continue
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int i = 1;
do {
i++;
if (i == 5)
continue;
printf("%d ", i);
}while (i <=10);
return 0;
}
程序思考题
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <string.h>
int main() {
int i = 1;
do {
if (i == 5)
continue;
printf("%d ", i);
i++;
}while (i <=10);
return 0;
}
...