零基础入门学习C语言005讲:分支程序设计(2)

版权声明:转载请标明出处 https://blog.csdn.net/qq_41556318/article/details/89670508

if语句

if语句可以构成分支结构。它根据给定的条件进行判断,以决定执行某个分支程序段。C语言的if语句有三种基本形式。

 if语句的三种形式……

第一种形式为基本形式:

    if(表达式) 语句

其语义是:如果表达式的值为真,则执行其后的语句,否则不执行该语句。其过程可表示为下图。

【例5.3】

#include "stdio.h"

void main()
{
	int a, b, max;
	printf("\n input two numbers:");
	scanf("%d%d", &a, &b);
	max = a;
	if (max < b)
		max = b;
	printf("max=%d", max);
}

第二种形式为: if-else

【例5.4】

#include "stdio.h"

void main()
{
	int a, b, max;
	printf("\n input two numbers:");
	scanf("%d%d", &a, &b);
	if (a > b)
		printf("max=%d", a);
	else
		printf("max=%d", b);
}

第三种形式为if-else-if形式

前二种形式的if语句一般都用于两个分支的情况。当有多个分支选择时,可采用if-else-if语句,其一般形式为:

【例5.5】

#include "stdio.h"

void main()
{
	char c;
	printf("input a character:    ");
	c = getchar();
	if (c<32)
		printf("This is a control character\n");
	else if (c >= '0'&&c <= '9')
		printf("This is a digit\n");
	else if (c >= 'A'&&c <= 'Z')
		printf("This is a capital letter\n");
	else if (c >= 'a'&&c <= 'z')
		printf("This is a small letter\n");
	else
		printf("This is an other character\n");
}

在使用if语句中还应注意以下问题:

1)       在三种形式的if语句中,在if关键字之后均为表达式。该表达式通常是逻辑表达式或关系表达式,但也可以是其它表达式,如赋值表达式等,甚至也可以是一个变量。

例如:

    if(a=5) 语句;

    if(b) 语句;

都是允许的。只要表达式的值为非0,即为“真”。

比较如下程序段:

2)       if语句中,条件判断表达式必须用括号括起来,在语句之后必须加分号。

3)       在if语句的三种形式中,所有的语句应为单个语句,如果要想在满足条件时执行一组(多个)语句,则必须把这一组语句用{}括起来组成一个复合语句。但要注意的是在}之后不能再加分号。(最好是在单行的时候也加上大括号。任何情况下都加上大括号。)

例如:

	if (a>b)
	{
		a++;
		b++;
	}
	else
	{
		a = 0;
		b = 10;
	}

补充例题:写一个程序完成下列功能:

 1 、输入一个分数score

 2 、score<60             输出  E

 3 、60 <=score <70 输出  D

 4 、75 <=score <80 输出  C

 5 、80 <=score <90 输出  B

 5 、90 <=score        输出  A

#include "stdio.h"

void main()
{
	int score;
	printf("Input a score:");
	scanf("%d", &score);
	if (score < 60)
	{
		printf("The score is E!\n");
	}
	else if (score >= 60)
	{
		printf("The score is D!\n");
	}
	else if (score >= 70)
	{
		printf("The score is C!\n");
	}
	else if (score >= 80)
	{
		printf("The score is B!\n");
	}
	else
	{
		printf("The score is A!\n");
	}
}

补充例题2:输入三个数a,b,c,要求按由小到大的顺序输出。

提示:

If  a>b   将a和b对换

If  a>c   将a和c对换

If  b>c   将b和c对换

#include "stdio.h"

void main()
{
	int a,b,c;
	int d;
	printf("Input three number:");
	scanf("%d%d%d", &a,&b,&c);
	if (a > b)
	{
		d = a;
		a = b;
		b = d;
	}
	if (a > c)
	{
		d = a;
		a = c;
		c = d;
	}
	if (b > c)
	{
		d = b;
		b = c;
		c = d;
	}
	printf("%d %d %d", a, b, c);
}

if语句的嵌套

if语句中的执行语句又是if语句时,则构成了if 语句嵌套的情形。

在嵌套内的if语句可能又是if-else型的,这将会出现多个if和多个else重叠的情况,这时要特别注意ifelse的配对问题。

例如:

其中的else究竟是与哪一个if配对呢?

比较以下两题目:哪一个是运用嵌套方法?

【例5.6】

#include "stdio.h"

void main()
{
	int a, b;
	printf("please input A,B:    ");
	scanf("%d%d", &a, &b);
	if (a != b)
	if (a>b)  printf("A>B\n");
	else     printf("A<B\n");
	else     printf("A=B\n");
}

【例5.7】

#include "stdio.h"

void main()
{
	int a, b;
	printf("please input A,B:      ");
	scanf("%d%d", &a, &b);
	if (a == b)        printf("A=B\n");
	else if (a>b)   printf("A>B\n");
	else               printf("A<B\n");
}

采用嵌套结构实质上是为了进行多分支选择,实际上有三种选择即A>BA<BA=B。这种问题用if-else-if语句也可以完成。而且程序更加清晰。因此,在一般情况下较少使用if语句的嵌套结构。以使程序更便于阅读理解

猜你喜欢

转载自blog.csdn.net/qq_41556318/article/details/89670508