第四章 分支结构程序设计
?表达式
o 关系表达式
o 逻辑表达式
o 条件表达式
?语句
o if 语句
o 多分支语句 (switch)
4.1.1 关系运算符和关系表达式
?比较两个量 (x,y)之间的关系
x < y x <= y x == y
x > y x >= y x != y
?比较的结果:
真 3>1
假 1>3
int x=2,y=6;
x = (y > 3)
x = (x > y)
1
0
x=1
x=0
关系运算符的优先级
运算符优先级,算术运算符 高
< <= > >=
== !=
= 低
d = b+2 == 3
d = ((b+2) == 3)
b - 1 == a != c
((b – 1) == a) != c
关系表达式
3 < x < 5
x=1时
x=4时
( 3<x ) < 5
恒为 1
4.1.2 逻辑运算符和逻辑表达式
3 < x < 5
0 3 5 x
x > 3 并且 x < 5
3 < x && x < 5
逻辑表达式
用逻辑运算符将关系表达式或逻辑量连接起来
逻辑运算符
X&&Y X||Y
X Y X Y
!X X
与 或

逻辑与 && 逻辑或 || 逻辑非 !
(x>1)&&(y>1) (x>1)||(y>1) (x>1)
!(x>1) 即 x<=1
逻辑运算符
逻辑运算的规则-真值表
逻辑与 && 逻辑或 || 逻辑非 !
真值表
x y x&&y x||y !x
假 假 0 0 1
假 真 0 1 1
真 假 0 1 0
真 真 1 1 0
逻辑运算的规则-真值表
逻辑与 && 逻辑或 || 逻辑非 !
真值表
x y x&&y x||y !x
假 假 0 0 1
假 真 0 1 1
真 假 0 1 0
真 真 1 1 0
5&&7是否合法?
0 0
0 非 0
非 0 0
非 0 非 0
! !2 =? =1
逻辑表达式
对于 X && Y,X和 Y可以是:
关系表达式 或 逻辑量
x <= 5 && x >= 3
3 && 5
(x+y) && 7
X,Y可以是任意表达式
判断逻辑量的真假
非 0表示 真
0表示 假
逻辑运算的结果
1表示 真
0表示 假

算术运算符
< <= > >=
== !=
&&
||
=
,
运算符的优先级
设 x = 1,y = 2,c = 0
x >= y == c && !x+2 > 3
(x >= y == c) && (!x+2 > 3)
((x >= y) == c) && ((!x+2) > 3)
((x >= y) == c) && (((!x)+2) > 3))
例 4- 3
写出满足下列要求的表达式
⑴ ch 是小写英文字母
ch >= 'a' && ch <= 'z'
⑵ x 为零
关系表达式 x == 0
或 逻辑表达式 !x
验证,x取 0 !x 真
x取非 0 !x 假
x取 0 x==0 真
x取非 0 x==0 假等价
⑶ x 不为零
x !=0 或 x
例 4- 3
(4) x 和 y 不同时为零
x != 0 || y!=0 或 x || y
(5) year 是闰年, 即 year 能被 4 整除但不能被 100
整除, 或 year 能被 400 整除 。
(year % 4 == 0 && year % 100 != 0) || (year % 400 = = 0)

(!(year % 4) && year % 100) || !(year % 400 )
逻辑运算符 && 和 ||
int x,y; /* 假设每次运算后, 将 x的值置 1 */
y = (3 >1 && x++);
y = (3<1 && x++);
y = (3<1 || x++);
y = (3>1 || x++);
exp1 && exp2 先算 exp1,若其值为 0,STOP
exp1 || exp2 先算 exp1,若其值为 1,STOP
y=1 x=2
y=0 x=1
y=1 x=2
y=1 x=1
4.2.1 基本的 if 语句
1,if – else结构
if (exp)
statement_1
else
statement_2
语句 1
表达式
语句 2
非 0 0
x+2 x>0
y=
x2 x<=0
if ( x>0 )
y=x+2;
else
y=x*x;
if – else结构
if (exp)
statement_1
else
statement_2
scanf("%d%d",&a,&b);
if ( a !=b ){
t = a;
a=b;
b=t;
printf("%d,%d",a,b);
}
else
printf("a==b");
一条语句
一条语句
4.2.1 基本的 if语句
2,省略 else的 if结构
if (exp)
statement_1 语句 1
表达式
非 0 0
ch=getchar();
if(ch>='a'&&ch<='z')
ch=ch-'a'+'A';
putchar(ch);
ch=getchar();
if(ch>='a'&&ch<='z'){
ch=ch-'a'+'A';
putchar(ch);
}
例 4- 7 求最大值
输入 3个数, 输出其中的最大值 。
#include <stdio.h>
void main( )
{
int a,b,c,max;
printf("input a,b,c:\n");
scanf("%d%d%d",&a,&b,&c);
max = a;
if(max < b) max = b;
if(max < c) max = c;
printf("max is %d\n",max);
}
a max
b
c
max
max
4.2.2 嵌套的 if语句
if (exp)
statement1
else
statement2
1,else – if 结构
if (exp_1) statement_1
else if(exp_2) statement_2
……
else if(exp_n-1) statement_n-1
else statement_n
if 语句
if 语句
嵌套的 if语句
if (exp_1) statement_1
else if(exp_2) statement_2
……
else if(exp_n-1) statement_n-1
else statement_n
0
表达式
1
表达式
2
语句 1 语句 2 语句 n-1 语句 n
非 0
表达式 n-1
0
非 0
0
0
非 0
n个分支需要 n-1次比较
输入变量 x,计算符号函数:
1 当 x>0
y = 0 当 x=0
-1 当 x<0
例:计算符号函数
if (x>0) y = 1;
else if (x==0) y = 0;
else y = -1;
0
x > 0
x = 0
y = 1 y = 0
非 0
0
非 0
y = -1
输入变量 x,计算 y:
x+1 x<1
y = x+2 1<=x<2
x+3 2<=x<3
x+4 x>=3
例 4-9
if (x<1) y = x+1;
else if (x<2) y = x+2;
else if(x<3) y = x+3;
else y = x+4;
y = x+4
0
x <1
x <2
y = x+1 y = x+2
非 0
0
非 0 x <3
y = x+3
非 0
0
4.2.2 嵌套的 if语句
2,嵌套的 if – else 结构
if (exp_1)
if(exp_2) statement_1
else statement_2
else
if(exp_3) statement_3
else statement_4
if (exp)
statement1
else
statement2
if 语句
if 语句
嵌套的 if语句
输入变量 x,计算符号函数:
1 当 x>0
y = 0 当 x=0
-1 当 x<0
例:计算符号函数
if (x>=0)
if(x>0) y = 1;
else y = 0;
else y = -1;
x >=
0
x > 0
y = 1 y = 0
非 0 0
非 0
y = -1
0
输入变量 x,计算 y:
x+1 x<1
y = x+2 1<=x<2
x+3 2<=x<3
x+4 x>=3
例 4-9
if (x<2)
if(x<1) y = x+1;
else y = x+2;
else
if(x<3) y = x+3;
else y = x+4;
x <2
x <1
y = x+1 y = x+2
非 0 0
非 0 0
x <3
y = x+3 y = x+4
非 0 0
if (x>0) y = 1;
else if (x==0) y = 0;
else y = -1;
if (x>=0)
if(x>0) y = 1;
else y = 0;
else y = -1;
x>=0
x >
0
y = 1 y = 0
非 0 0
非 0
y = -1
0
0
x >
0 x =
0
y = 1 y = 0
非 0
0
非 0
y = -1
2种嵌套 if语句的比较
else 和 if 的匹配
if (exp_1)
if(exp_2) statement_1
else statement_2
else
if(exp_3) statement_3
else statement_4
if (exp_1)
if(exp_2) statement_1
else
if(exp_3) statement_3
else statement_4
else 与最靠近它的、没有与别的 else 匹配过的 if 匹配
if (exp_1)
if(exp_2) statement_1
else
if(exp_3) statement_3
else statement_4
改变 else 和 if 的配对
if (exp_1)
if(exp_2) statement_1
else
if(exp_3) statement_3
else statement_4
if (exp_1)
if(exp_2) statement_1
else
if(exp_3) statement_3
else statement_4
if (exp_1){
if(exp_2) statement_1
}
else
if(exp_3) statement_3
else statement_4
if (exp_1)
if(exp_2) statement_1
else
else
if(exp_3) statement_3
else statement_4
4.2.3 条件表达式
exp1? exp2, exp3
非 0 0
exp1
y = (x>0)? x+2, x*x;
int n;
(n>0)? 2.9, 1
n = 10
n = -10
2.9
1.0
x+2 x>0
y =
x2 x<=0
if ( x>0 )
y=x+2;
else
y=x*x;
4.3 switch语句
switch( 表达式 )
{
case 常量 表达式 1:语句 段 1
case 常量 表达式 2:语句 段 2
....…
case 常量 表达式 n:语句 段 n
default, 语句 段 n+1
}
switch( 表达式 )
{
case 常量 表达式 1:语句 段 1
case 常量 表达式 2:语句 段 2
....…
case 常量 表达式 n:语句 段 n
default, 语句 段 n+1
}
表达式的值 =常量表达式 2的值



语句段 1
语句段 2
语句段 n
语句段 n+1
表达式 的值 =常量表达式 1的值
表达式的值 =常量表达式 n的值
其他
例 4- 13
# include <stdio.h>
void main( )
{
int k;
scanf("%d",&k);
switch( k ){
case 1,printf ( "I'm in the case1\n" );
case 2,printf ( "I'm in the case2\n" );
case 3,printf ( "I'm in the case3\n" );
default,printf ( "I'm in the default\n");
}
}
输入 2
输入 6
# include <stdio.h>
void main( )
{
int k;
scanf("%d",&k);
switch( k ){
case 1,printf ( "I'm in the case1\n" ); break;
case 2,printf ( "I'm in the case2\n" ); break;
case 3,printf ( "I'm in the case3\n" ); break;
default,printf ( "I'm in the default\n"); break;
}
}
输入 2
输入 6
if(k==1) printf ( "I'm in the case1\n" );
else if(k==2) printf ( "I'm in the case2\n" );
else if(k==3) printf ( "I'm in the case3\n" );
else printf ( "I'm in the default\n");
例 4- 14
void main( )
{ char c;
printf("Please input a character:\n");
c = getchar();
switch(c) {
case ' ',
case '\n':
printf("This is a blank or enter");
break;
case '0', case '1', case '2', case '3', case '4',
case '5', case '6', case '7', case '8', case '9',
printf("This is a digit.\n");
break;
default:
printf("This is an other character,\n");
}
}
输入 2
输入 a
几个常量表达式共有
一个语句段
例 4- 10
# include <stdio.h>
void main( )
{ char c;
printf("Please input a character:\n");
c = getchar();
if(c==' ' || c=='\n')
printf("This is a blank or enter");
else if(c>='0' && c <= '9')
printf("This is a digit.\n");
else if(c>='a' && c <= 'z' || c>='A' && c <= 'Z')
printf("This is a letter.\n");
else
printf("This is an other character,\n");
}