C语言——第二章选择与循环
- 引言
- 1.if_else
- 2.switch月份1
- 3.switch月份2
- 4.goto
- 5.goto向下跳转
- 6.while语句
- 7.do_while语句
- 8.for循环
- 总结
引言
C语言的一些基本例题
全部例题来自于C语言王道训练营 链接如下
B站链接.
书籍:《跟“龙哥”学C语言编程》
1.if_else
下面展示一些 可运行的代码。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i;
while (scanf("%d", &i) != EOF)
{
if (i > 0)
{
printf("i is bigger than 0n");
}
else
{
printf("i is not bigger than 0n");
}
}
system("pause");
return 0;
}
运行结果
10
i is bigger than 0
0
i is not bigger than 0
^Z
^Z
^Z
请按任意键继续. . .
2.switch月份1
下面展示一些 可运行的代码。
#include<stdio.h>
#include<stdlib.h>
//打印哪一年月份的天数
int main()
{
int mon, year;
while (scanf("%d%d", &year, &mon) != EOF)
{
switch (mon)
{
//二月份闰年29天!
case 2:printf("mon=%d is %d daysn", mon,28+(year%4==0&&year%100!=0||year%400==0)); break;
case 1:printf("mon=%d is 31daysn", mon); break;
case 3:printf("mon=%d is 31daysn", mon); break;
case 5:printf("mon=%d is 31daysn", mon); break;
case 7:printf("mon=%d is 31daysn", mon); break;
case 8:printf("mon=%d is 31daysn", mon); break;
case 10:printf("mon=%d is 31daysn", mon); break;
case 12:printf("mon=%d is 31daysn", mon); break;
case 4:printf("mon=%d is 30daysn", mon); break;
case 6:printf("mon=%d is 30daysn", mon); break;
case 9:printf("mon=%d is 30daysn", mon); break;
case 11:printf("mon=%d is 30daysn", mon); break;
default:
printf("error monthn");
}
}
system("pause");
return 0;
}
运行结果
2000 2
mon=2 is 29 days
2010 2
mon=2 is 28 days
2022 10
mon=10 is 31days
2022 13
error month
^Z
^Z
^Z
请按任意键继续. . .
3.switch月份2
下面展示一些 可运行的代码。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int mon, year;
while (scanf("%d%d", &year, &mon) != EOF)
{
switch (mon)
{
//二月份闰年29天!
case 2:printf("mon=%d is %d daysn", mon, 28 + (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)); break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:printf("mon=%d is 31daysn", mon); break;
case 4:
case 6:
case 9:
case 11:printf("mon=%d is 30daysn", mon); break;
default:
printf("error monthn");
}
}
system("pause");
return 0;
}
运行结果
2000 2
mon=2 is 29 days
2010 2
mon=2 is 28 days
2022 10
mon=10 is 31days
2003 22
error month
^Z
^Z
^Z
请按任意键继续. . .
4.goto
下面展示一些 可运行的代码。
#include<stdio.h>
#include<stdlib.h>
//goto只能在一个函数中使用
//求1到100之间所有整数之和的程序
int main()
{
int i=1;
int total = 0;
label:
total = total + i;
i++;
if (i <= 100)
{
goto label;
}
printf("total=%dn", total);
system("pause");
return 0;
}
运行结果
total=5050
请按任意键继续. . .
5.goto向下跳转
下面展示一些 可运行的代码。
#include<stdio.h>
#include<stdlib.h>
//goto向下跳转实现部分代码不执行
int main()
{
int disk;
scanf("%d", &disk);
if (disk == 0)
{
goto label_disk_error;
}
//写入磁盘操作...
label_disk_error:
printf("system is errorn");
system("pause");
return 0;
}
运行结果
0
system is error
请按任意键继续. . .
6.while语句
下面展示一些 可运行的代码。
#include<stdio.h>
#include<stdlib.h>
//while语句计算1到100之间的所有整数之和
int main()
{
int i = 1;
int total = 0;
while (i <= 100)
{
total = total + i;
i++;
}
printf("total=%dn", total);
system("pause");
return 0;
}
运行结果
total=5050
请按任意键继续. . .
7.do_while语句
下面展示一些 可运行的代码。
#include<stdio.h>
#include<stdlib.h>
//do while语句第一次执行循环体语句之前不会判断表达式的值,也就是如果i的初值为101,那么依然会进入循环体
int main()
{
int i = 1;
int total = 0;
do
{
total = total + i;
i++;
} while (i <= 100);
printf("total=%dn", total);
system("pause");
return 0;
}
运行结果
total=5050
请按任意键继续. . .
8.for循环
下面展示一些 可运行的代码。
#include<stdio.h>
#include<stdlib.h>
//for表达式1,2,3均可省略
int main()
{
int i, total;
for (i = 1, total = 0; i <= 100; i++)
{
total = total + i;
}
printf("total=%dn", total);
i = 1;
total = 0;
for (;;) {
total = total + i;
if (i < 100)
{
i++;
}
else
{
break;
}
}
printf("省略后total=%dn", total);
//continue
for (i = 1, total = 0; i <= 100; i++)
{
if (i % 2 == 0)
{
continue;//提前结束循环,不走下面的内容,直接进行下一轮循环
}
total = total + i;
}
printf("1-100奇数total=%dn", total);
//break
for (i = 1, total = 0; i <= 100; i++)
{
if (total>2222)
{
break;//跳出for循环
}
total = total + i;
}
printf("break后total=%dn", total);
system("pause");
return 0;
}
运行结果
total=5050
省略后total=5050
1-100奇数total=2500
break后total=2278
请按任意键继续. . .
总结
对于一些遗漏,读者不要太过深究~
比较适合基础新手大佬请绕路~
希望这些可以帮助你更好的理解C语言
马上就考研了 我居然还在纠结一些基础 真是闲的闲的闲的闲*10000!
过几天在更新后几章的内容
欢迎大家评论、收藏、点赞 !!!
最后
以上就是外向洋葱最近收集整理的关于C语言--选择与循环引言总结的全部内容,更多相关C语言--选择与循环引言总结内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复