C语言:程序流程结构

选择结构
  • if-else
    if-else-if
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

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<stdio.h>

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
    while
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

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

int main(void) {
	int a = 1;
	do {
		printf("a=%d\n", a);
		a++;
	} while (a < 10);
	return 0;
}
  • for
    for
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

int main(void) {
	int i,sum=0;
	for (i = 1; i < 100; i++) {
		printf("%d+",i);
		sum += i;
	}
	sum += 100;
	printf("%d=%d\n", i,sum);
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值