【C语言】判断

目录

1. if语句-- 比较数的大小

2. if-else语句-- 比较都两个数的大小

3. 嵌套if-else-- 比较三个数的大小

4. 级联if-else if-else-- 计算分段函数

5. switch-case-- 对级联if-else if-else的改进

5.1 成绩转换

5.2 输出对应的月份


视频只能起到穿针引线的作用,真正提升还是要不断的自己写代码,不断的学习“错误”才能进步。所以陈姥姥强调了看完视频要把书本对应的章节再看一遍,然后做对应章节的测验。学习顺序就是听课->完成作业->重读教材对应章节->做对应章节的测试卷

还要增加功力就去做题:浙大版《C语言程序设计(第3版)》题目集、浙大版《C语言程序设计实验与习题指导(第3版)》题目集、基础编程题目集。千里之行,始于足下。刷题也是由易到难,慢慢的积累,培养编程的感觉,不要想着一蹴而就。


 1. if语句-- 比较数的大小

#include <stdio.h>

int main()
{
	int a,b,max;
	scanf("%d %d",&a,&b);
	
	if(a > b){
		max = a;
	}
	printf("%d\n",max);
	return 0;	
}

通过if语句比较两个数的大小,但是它没有解决b大于a的问题。当a>b的条件不成立时,程序就结束了,max没有得到值。这很自然就引出了下面的if-else语句。

2. if-else语句-- 比较都两个数的大小

#include <stdio.h>

int main()
{
	int a,b,max;
	scanf("%d %d",&a,&b);
	
	if(a > b){
		max = a;
		//printf("max = %d\n",max);
	}
	else{
		max = b;
		//printf("max = %d\n",max);
	}

	printf("max = %d\n",max);//单一出口 
	return 0;	
}

不管是if还是else语句后面最好都加大括号缩进并不能暗示else的匹配,加{}才是保险的,即使只有一条语句,这是好习惯。因为你以为的和编译器以为的不一样 。

3. 嵌套if-else-- 比较三个数的大小

程序的流程图如下所示。

#include <stdio.h>

int main()
{
	int a,b,c,max;
	scanf("%d %d %d",&a,&b,&c);
	
	if(a > b){
		if(a > c){
			max = a;
		}else{
			max = c;
		}
	}else{
		if(b > c){
			max = b;
		}else{
			max = c;
		}
	}
	printf("max = %d\n",max);//单一出口 
	return 0;	
}

4. 级联if-else if-else-- 计算分段函数

#include <stdio.h>

int main()
{
	int x,f;
	scanf("%d",&x);
	
	if(x < 0){
		f = -1;
	}
	else if(x == 0){
		f = 0;
	}
	else{
		f = 2*x;
	}
	printf("%d\n",f);//单一出口 
	return 0;	
}

5. switch-case-- 对级联if-else if-else的改进

用级联的if-else if-else的写法。如果输入的type值为3,程序还是要先判断type是不是等于1,是不是等于2。如果使用switch-case语句就不会这样。在调试模式下查看执行过程。

#include <stdio.h>

int main()
{
	int type;
	scanf("%d",&type);
	
	if(type == 1){
		printf("早上好。\n");
	}
	else if(type == 2){
		printf("中午好。\n");
	}
	else{
		printf("晚上好。\n");
	}
	return 0;	
}

#include <stdio.h>

int main()
{
	int type;
	scanf("%d",&type);
	
	switch(type){
		case 1:printf("早上好。\n");break;
		case 2:printf("中午好。\n");break;
		case 3:printf("晚上好。\n");break;
	} 

	return 0;	
}

在使用switch-case语句时要注意:控制表达式只能是整数;以及break的作用。

5.1 成绩转换

#include <stdio.h>

int main()
{
	int grade;
	scanf("%d",&grade);
	grade /= 10;
	switch(grade){
		case 10:
		case 9:printf("A\n");break;
		case 8:printf("B\n");break;
		case 7:printf("C\n");break;
		case 6:printf("D\n");break;
		default:printf("E\n");break;
	} 

	return 0;	
}

不符合“单一出口”的原则,因为我们还没有学过字符或字符串数据的处理。 

5.2 输出对应的月份

#include <stdio.h>

int main()
{
	int month;
	scanf("%d",&month);
	
	switch(month){
		case 1:printf("January\n");break;
		case 2:printf("February\n");break;
		case 3:printf("Marth\n");break;
		case 4:printf("Aprial\n");break;
		default:printf("else\n");break;
	} 

	return 0;	
}

今后可以用数组做。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吾日叁問

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值