栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言:程序流程结构

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言:程序流程结构

选择结构
  • if-else
#define _CRT_SECURE_NO_WARNINGS
#include

int main(void) {
	int score;
	scanf("%d", &score);
	if (score >= 90) {
		printf("score=%d,优秀n", score);
	}
	else if(score>=70){
		printf("score=%d,良好n", score);
	}
	else if (score >= 60) {
		printf("score=%d,及格n", score);
	}
	else
	{
		printf("score=%d,不及格n", score);
	}
	return 0;
}
  • switch
#define _CRT_SECURE_NO_WARNINGS
//#pragma warning(disable:4996)
#include

int main(void) {
	int score;
	scanf("%d", &score);
	switch (score/10)
	{
	case 10:
	case 9:
		printf("score=%d,优秀n", score);
		break;
	case 8:
	case 7:
		printf("score=%d,良好n", score);
		break;
	case 6:
		printf("score=%d,及格n", score);
		break;
	default:
		printf("score=%d,不及格n", score);
		break;
	}
	return 0;
}
循环结构
  • while
#define _CRT_SECURE_NO_WARNINGS
#include

int main(void) {
	int a = 20;
	while (a > 10) {
		scanf("%d", &a);
		printf("a=%dn", a);
	}
	return 0;
}
  • do-while
#define _CRT_SECURE_NO_WARNINGS
#include

int main(void) {
	int a = 1;
	do {
		printf("a=%dn", a);
		a++;
	} while (a < 10);
	return 0;
}
  • for
#define _CRT_SECURE_NO_WARNINGS
#include

int main(void) {
	int i,sum=0;
	for (i = 1; i < 100; i++) {
		printf("%d+",i);
		sum += i;
	}
	sum += 100;
	printf("%d=%dn", i,sum);
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include

int main(void) {
	int sum = 0;
	for (int i = 1; i <= 100; i++) {
		sum = sum + i;
	}
	printf("sum=%dn", sum);
	return 0;
}

for相关注意事项
1.循环条件中的变量i在外部定义时,for循环执行完依旧可以使用;
变量i在for循环中定义时,for循环外部不可以使用;
2.for循环里的3个表达式都可以省略,但是2个;不能省略。即可以写成for( ; ; ){…}【这种写法会出现死循环,可以在循环体内通过break跳出循环】

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847172.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号