1 / 9

关系运算符和关系表达式

关系运算符和关系表达式. 关系运算符是逻辑运算符中比较简单的一种,关系运算就是比较运算,即将两个值进行比较,判断是否符合或满足给定的条件。如果符合或满足给定的条件,则称关系运算的结果为“真”;如果不符合或不满足给定的条件,则称关系运算的结果为“假”. 优先级相同(高). 优先级相同(低). 知识点 1 :关系运算符及其优先次序. < ( 小于 ) <= ( 小于或等于 ) > ( 大于 ) >= ( 大于或等于 ) == ( 等于 ) != ( 不等于 ). 说明:

Download Presentation

关系运算符和关系表达式

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 关系运算符和关系表达式 关系运算符是逻辑运算符中比较简单的一种,关系运算就是比较运算,即将两个值进行比较,判断是否符合或满足给定的条件。如果符合或满足给定的条件,则称关系运算的结果为“真”;如果不符合或不满足给定的条件,则称关系运算的结果为“假”

  2. 优先级相同(高) 优先级相同(低) 知识点1:关系运算符及其优先次序 • < (小于) • <= (小于或等于) • > (大于) • >= (大于或等于) • == (等于) • != (不等于) 说明: 关系运算符的优先级低于算术运算符 关系运算符的优先级高于赋值运算符

  3. 例4-1 关系运算符的优先级的实例 • 1、c>a+b; 关系运算符的优先级低于算术运算符 • 2、a>b==c; “>”优先级高于“==” • 3、a==b<c; “<”优先级高于“==” • 4、a=b>c; 关系运算符的优先级高于赋值运算符的优先级 分析:若a=5,b=3,c=1,分析以上式子的值, 注意:关系运算的结果为“真”;值为“1”; 关系运算的结果为“假” ;值为“0”;

  4. 训练1:若a=3, b=2, c=1, 分析以下表达式值: a>b      真,表达式的值为1(a>b) = = c  真,表达式的值为1b+c<a     假,表达式的值为0 d = a>bd的值等于1f = a>b>cf的值等于0

  5. 1、a>b 2、a>c 3、b<c 4、b>a>c!=a>b>c 5、a==b>c 6、a-b!=c-b 训练2:设整型变量a=3,b=2,c=4;求下列表达式的值

  6. 知识点2、关系表达式 用关系运算符将两个表达式(可以是算术表达式或 关系表达式,逻辑表达式,赋值表达式,字符表达式) 接起来的式子,称关系表达式 例:a>b,a+b>b+c,(a=3)>(b=5),’a’<‘b’,(a>b)>(b<c) 关系表达式的值是一个逻辑值,即“真”或“假”。 例:关系表达式”a>b”的值为“真”,表达式的值为1。 C语言中没有专用的逻辑值,1代表真,0代表假

  7. 训练3:分析以下程序的运行结果 #include "stdio.h" void main() { int a,b,c,f,d; scanf("%d%d%d",&a,&b,&c); printf("%d\n",a>b); printf("%d\n",a>b==c); printf("%d\n",b+c<a); d=a>b; printf("%d\n",d); f=a>b>c; printf("%d\n",a>b); } 若从键盘输入数据:1 2 3,分析运行结果; 若从键盘输入数据:3 2 1,分析运行结果;

  8. 训练4:分析以下程序的运行结果 • #include "stdio.h" • void main() • { • char c='k'; • int i=1,j=2,k=3; • float x=3e+5,y=0.85; • printf("%d,%d\n",'a'+5<c,-i-2*j>=k+1); • printf("%d,%d\n",1<j<5,x-5.25<=x+y); • printf("%d,%d\n",i+j+k==-2*j,k=j==i+5); • }

  9. 训练5:分析以下程序的运行结果 #include "stdio.h" void main() { int i=65; char c='B'; printf("i+1==c--->%d\n",i+1==c); printf("c+32!='b'--->%d\n",c+32!='b'); }

More Related