30天学会C语言: Day 6-当情况从复杂,变成更加复杂...

当条件很多而且很复杂的时候,可以分成两种情形

多个条件,而且我全都要

当有多个条件,且所有条件都要同时成立,可以在 if() 里面加上 if()

下面的程序码中,需要 x>=100 而且 x<=200,才会执行第9行的 printf()

#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);
	if(x>=100){
		if(x<=200)
			printf("%d is in range!\n", x);
	}
	
	return 0;
}

这样的写法简单好懂,但有两个缺点:

  1. 如果有 n 个条件就要写 n 个 if,条件很多的时候就会变成:
#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);
	if(x>=100){
		if(x<=200){
			if(x%2==0){
				if(x%3==0){
					...
				}
			}
		}
	}
	
	return 0;
}
  1. 需要 else 的时候,要写 n 个 else
#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);
	if(x>=100){
		if(x<=200){
			if(x%2==0){
				if(x%3==0){
					...
				}
				else{
					...
				}
			}
			else{
				...
			}
		}
		else{
			...
		}
	}
	else{
		...
	}
	
	return 0;
}

简化

这种情况可以用昨天讲到的逻辑运算子 && 把条件串起来

#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);
	if(x>=100 && x<=200 && x%2==0 && x%3==0){
		...
	}
	else{
		...
	}
	
	return 0;
}

这段 if 代表x>=100 而且 x<=200 而且 x%2==0 而且 x%3==0 时,执行...,否则执行...

下面的程序码可以判断输入的数,是不是在100~200的范围

#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);
	if(x>=100 && x<=200)
		printf("%d is in range!\n", x);
	else
		printf("%d is not in range!\n", x);

	return 0;
}

判断输入的数,是不是大於100的奇数

#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);
	if(x>=100 && x%2==1)
		printf("True\n");
	else
		printf("False\n");

	return 0;
}

多个条件,有一个成立就好

另一种情况是有多个条件,但只要有一个条件成立就执行,这个时候可以用多个 if() 达成类似的效果

下面程序码可以达到类似 x>100 或者 x%2==0 的效果

#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);
	if(x>100){
		...
	}
	if(x%2==0){
		...
	}
	
	return 0;
}

但这个种写法会有更多问题,除了要写很多次 if(),还会有重复执行以及结果错误的问题

下面的程序码,在 x 两个条件都符合/不符合时,执行结果会出现两次 True/False;当只有一个条件符合时,TrueFalse 会各显示一次

#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);

	if(x>100)
		printf("True\n");
	else
		printf("False\n");

	if(x%2==0)
		printf("True\n");
	else
		printf("False\n");
	
	return 0;
}

简化

要解决这个问题,可以用逻辑运算子 || 把条件合并

所以上面的例子可以改写成:

#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);

	if(x>100 || x%2==0)
		printf("True\n");
	else
		printf("False\n");
	
	return 0;
}

分开和合并,都挤?

要用多组 if() 还是用逻辑运算把条件写在一起其实都可以,如果分开写比较简单(或是只能分开写)可以选择分开写,合在一起比较简单就合一起写

分辨输入的数是正奇数、正偶数、负奇数、负偶数、或0
用逻辑运算:

#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);

	if(x>0 && x%2==0)
		printf("x is a positive even number.\n");
	else if(x>0 && x%2==1)
		printf("x is a positive even odd number.\n");
	else if(x<0 && (-x)%2==0)
		printf("x is a negative even number.\n");
	else if(x<0 && (-x)%2==1)
		printf("x is a negative even odd number.\n");
	else
		printf("x equals to 0.\n");
	
	return 0;
}

不用逻辑运算:

#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);

	if(x>0){
		if(x%2==0)
			printf("x is a positive even number.\n");
		else
			printf("x is a positive odd number.\n");	
	}
	else if(x<0){
		if((-x)%2==0)
			printf("x is a negative even number.\n");
		else
			printf("x is a negative odd number.\n");	
	}
	else
		printf("x equals to 0.\n");
	
	return 0;
}

或是:

#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);

	if(x==0)
        printf("x equals to 0.\n");
    else{
        if(x>0)
            printf("x is a positive ");
        else{
            x=-x;
            printf("x is a negative ");
        }

        if(x%2==0)
            printf("even number.\n");
        else
            printf("odd number.\n");
    }

	return 0;
}

我觉得几这种都满好懂的,所以依照自己的习惯写就可以了

补充

程序码缩排

虽然C语言的语法没有规定,但习惯上会在区块的最左边留空格,称为缩排,目的是让程序码更容易阅读

用什麽类型的空白符号做缩排、空多少格也没有硬性规定,但通常以一个 Tab 键(在键盘左边) 为单位,每多一个大括号,就多空一个单位

如果大括号被省略还是要缩排

#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);
	
	if(x%2==0)
		printf("%d is a even number.")
	else
		printf("%d is a odd number.")
	
	return 0;
}

比较看看有缩排和没有缩排的差异

#include<stdio.h>
#include<stdlib.h>

int main(){
	int x;
	scanf("%d", &x);

	if(x>0){
		if(x%2==0)
			printf("x is a positive even number.\n");
		else
			printf("x is a positive odd number.\n");	
	}
	else if(x<0){
		if(x%2==0)
			printf("x is a negative even number.\n");
		else
			printf("x is a negative odd number.\n");	
	}
	else
		printf("x equals to 0.\n");
	
	return 0;
}
#include<stdio.h>
#include<stdlib.h>

int main(){
int x;
scanf("%d", &x);

if(x>0){
if(x%2==0)
printf("x is a positive even number.\n");
else
printf("x is a positive odd number.\n");	
}
else if(x<0){
if(x%2==0)
printf("x is a negative even number.\n");
else
printf("x is a negative odd number.\n");	
}
else
printf("x equals to 0.\n");
	
return 0;
}

挖坑清单:

  • main() 前面的 int,和最後一行的 return 0;
  • 可以拆成多行的情况
  • \0 在字串、字元中的意思
  • 指标

<<:  [08] [Flask 快速上手笔记] 07. 重新导向x状态码xJSON

>>:  #8 Web Crawler 1

学习笔记:一起进入 PixiJS 的世界 (五)

目前已接触的DisplayObject包含了PIXI.Graphics()的图像绘制、PIXI.Te...

Day1对於学习Java的看法&安装程序

刚读大一的时候,最让我感到头痛的就是程序设计课了!因为我一直以来都不怎麽喜欢电脑相关的东西,更别说是...

D26. 学习基础C、C++语言

D26. 关於注解 今天想要讲比较轻松的东西就是 【注解】 在未来所写的程序码绝对是几千几万行起跳,...

【Day7】人算不如天算的运算式

这个部分算是JavaScript比较难缠的部分,不是难以理解,而是因为比较冗杂,稍稍心浮气躁,就没...

当TrustView(档案加密软件) 白老鼠的惨痛过程记录 ~

外部稽核要求文件需要有保护的机制 , 所以我们引进了 ~ 加密了哪些软件产生的文件 ? Micros...