【C语言】:
操作符(https://mp.csdn.net/editor/html/115218055)
数据类型(https://mp.csdn.net/editor/html/115219664)
自定义类型:结构体、枚举、联合(https://mp.csdn.net/editor/html/115373785)
变量、常量(https://mp.csdn.net/editor/html/115230188)
分支、循环语句(https://mp.csdn.net/editor/html/115234118)
字符串+转义字符+注释(https://mp.csdn.net/editor/html/115231391)
指针(https://mp.csdn.net/editor/html/115281303)
数组(https://mp.csdn.net/editor/html/115266750)
函数(https://mp.csdn.net/editor/html/115265396)
内容来自 B站 C语言教学视频 https://www.bilibili.com/video/BV1RX4y1u7Zh
分支、循环语句
C语言中如何实现循环? while语句 for语句 do...while语句
printf("hehe");
1+2;
1.分支语句:
1.1 if 语法结构:
//单分支
if(表达式)
语句;
//双分支
if(表达式)
语句1;
else
语句2;
//多分支
if(表达式1)
语句1;
else if(表达式2)
语句2;
else
语句3;
解释一下: 如果表达式的结果为真,则语句执行。
在C语言中如何表示真假? 0表示假,非0表示真。
注意:
int age = 6;
if (18<=age<28) //错误的表达方式,执行顺序 首先判断 18<=age 为假(0)下一步判断 0<28 真,所以执行内部语句
printf("青年\n");//此时会打印青年,为什么呢
if(18<=age && age<28)//正确的表达方式 &&-并且 ||-或
printf("青年\n");//不会打印
悬空else else跟离它最近的if匹配(同一个代码块中)对齐
int a = 0; int a = 0;
int b = 2; int b = 2;
if(a == 1) if(a == 1)
if(b == 2) 匹配格式> if(b == 2)
printf("hehe\n");//不打印 printf("hehe\n");
else else
printf("haha\n"); //不打印 printf("hehe\n");
#include <stdio.h>
int main()
{
int a = 0;
int b = 2;
if(a == 1)
{
if(b == 2)
{
printf("hehe\n");
}
}
else
{
printf("haha\n");
}
return 0; }
1.2 switch 分支语句
switch(整型表达式)
{
case 整形常量表达式:
语句;
}
#include <stdio.h>
int main()
{
int day = 0;
switch(day)
{
case 1:
printf("星期一\n");
break;
case 2:
printf("星期二\n");
break;
case 3:
printf("星期三\n");
break;
case 4:
printf("星期四\n");
break;
case 5:
printf("星期五\n");
break;
case 6:
printf("星期六\n");
break;
case 7:
printf("星期天\n");
break;
}
return 0; }
switch 输入1-5是weekday 6-7是weedend
#include <stdio.h>
int main()
{
int i;
scanf("%d\n", &i);
switch(day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("工作日\n");
break;
case 6:
case 7:
printf("休息\n");
break;
default;
printf("输入错误\n");
break;
}
return 0;
}
break语句的实际效果是把语句列表划分为不同的部分。
switch 中的 break 和 continue
break用于永久终止循环,同代码块后面语句不再读取,如果没遇到break那么后面代码继续执行 ; continue 终止本次循环,下次可再读取同代码块后面语句
#include <stdio.h>
int main()
{
int x = 3; int y = 3;
switch(x % 2) // 1
case 1:
switch(y)
{
case 0:
printf("first");
case 1:
printf("second");
break;
default: printf("hello");
}
case 2:
printf("third");
return 0;
} //最后打印结果为 hello third
2.循环语句
2.1 while 循环语句
while(表达式)
循环语句;
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
printf("%d ", i);
i = i+1;
}
return 0;
}
while语句中的break和continue
break
//break 代码实例
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
if(i == 5)
break;
printf("%d ", i);
i = i+1;
}
return 0;
}
打印结果 1 2 3 4
总结: break在while循环中的作用:
其实在循环中只要遇到break,就停止后期的所有的循环,直接终止循环。 所以:while中的
break是用于永久终止循环的。
contiune
//continue 代码实例1
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
if(i == 5)
continue;
printf("%d ", i);
i = i+1;
}
return 0;
}
打印结果:1 2 3 4
//continue 代码实例2
#include <stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
i = i+1;
if(i == 5)
continue;
printf("%d ", i);
}
return 0;
}
结果 :2 3 4 6 7 8 9 10
总结: continue在while循环中的作用就是:
continue是用于终止本次循环的,也就是本次循环中continue后边的代码不会再执行,而是直接
跳转到while语句的判断部分。进行下一次循环的入口判断。
getchar()函数 putchar()函数
#include <stdio.h>
int main()
{
int ch = 0 ;
//EOF end of file 文件结束标志 本质上为 -1
while ((ch = getchar()) != EOF) //输入函数getchar() 键盘输入字符 输入 ctrl + z 结束
//int ch = putchar();//输出函数 putchar()
{
putchar(ch); //显示打印ch的值
//printf("%c\n", ch);
}
return 0;
}
#include <stdio.h>
int main()
{
int ret = 0;
int ch = 0;
char password[20] = {0};
printf("请输入密码:");
//输入12345 但缓存区内为 12345\n
scanf("%s", password); //输入密码,并放在password数组中
//缓存区还剩一个\n
//读取一下\n
while ((getchar()) != '\n') //循环直到读取到\n 多个多余字符如12345asc
{
;//空语句
}
printf("请确认(Y/N):");
ret = getchar();// Y/N
if(ret == 'Y')
printf("确认成功\n");
else
printf("确认失败\n");
return 0;
}
//代码1
#include <stdio.h>
int main()
{
int ch = 0;
while ((ch = getchar()) != EOF)
putchar(ch);
return 0;
}
//代码2
#include <stdio.h>
int main()
{
while ((ch = getchar()) != EOF)
{
if (ch < ‘0’ || ch > ‘9’)
continue;
putchar(ch);
}
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/f6dc66cc5e791590f2fb579fe0182261.png)
2.2 for 循环语句
for(表达式1;表达式2;表达式3)
循环语句;
表达式1 表达式1为初始化部分,用于初始化循环变量的。 表达式2 表达式2为条件判断部分,用于判断
循环时候终止。 表达式3 表达式3为调整部分,用于循环条件的调整。
使用for循环 在屏幕上打印1-10的数字。
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++) // for(i=1/*初始化*/; i<=10/*判断部分*/; i++/*调整部分*/)
{
printf("%d ", i);
}
return 0;
}
现在我们对比一下for循环和while循环。
int i = 0;
//实现相同的功能,使用while
i = 1; //初始化部分
while (i <= 10) // 判断部分
{
printf("hehe\n");
i = i + 1; //调整部分
}
//实现相同的功能,使用while
for (i = 1; i <= 10; i++)
{
printf("hehe\n");
}
可以发现在while循环中依然存在循环的三个必须条件,但是由于风格的问题使得三个部分很可能偏离
较远,这样查找修改就不够集中和方便。所以,for循环的风格更胜一筹。 for循环使用的频率也最高。
//代码1
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
if (i == 5)
break;
printf("%d ", i); // 1 2 3 4
}
return 0;
}
//代码2
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
if (i == 5)
continue;
printf("%d ", i); // 1 2 3 4 6 7 8 9 10
}
return 0;
}
for 的变种
#include <stdio.h>
int main()
{
int i = 0;
int j = 0;
//for循环的初始化、判断、调整都可以省略,但是for循环的判断部分被省略,
//那判断条件就是恒为真,如果不是非常熟练,建议不要随便省略
/* for( ; ; ) //进入死循环
{
printf("hehe\n");
}
*/
for (; i < 10; i++) //省略条件部分 i=0 开始
{
for (; j < 10; j++)
//从上个的第一次for循环开始,进入本次for循环,j从0到9循环十次并且打印了十次,j=10时,
//结束本次for循环,然后到上个for循环,i=1,进入到本次for循环时,j没有销毁,此时j=10;跳出
{
printf("哈哈\n"); //打印十次 哈哈
}
}
return 0;
}
2.3 do while 循环语句
do
循环语句;
while(表达式);
#include <stdio.h>
int main()
{
int i = 10;
do
{
printf("%d\n", i);
} while (i < 10);
return 0;
}
#include <stdio.h>
int main()
{
int i = 10;
do
{
if (5 == i)
break;
printf("%d\n", i); //10
} while (i < 10);
return 0;
}
#include <stdio.h>
int main()
{
int i = 10;
do
{
if (5 == i)
continue;
printf("%d\n", i); // 10
} while (i < 10);
return 0;
}
3. goto语句
一个关机程序
#include <stdio.h>
int main()
{
char input[10] = { 0 };
system("shutdown -s -t 60");
again:
printf("电脑将在1分钟内关机,如果输入:我是猪,就取消关机!\n请输入:>");
scanf("%s", input);
if (0 == strcmp(input, "我是猪"))
{
system("shutdown -a");
}
else
{
goto again;
}
return 0;
}
goto语言真正适合的场景如下:
for (...)
for (...)
{
for (...)
{
if (disaster)
goto error;
}
}
…
error :
if (disaster)
// 处理错误情况