《(3.5)--3.5Relationaloperatorsandrelatio.pdf》由会员分享,可在线阅读,更多相关《(3.5)--3.5Relationaloperatorsandrelatio.pdf(6页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、Relational Operators&Relational ExpressionsRelational Operators&Relational Expressions3.5Relational operators are used to compare the magnitude relationship oftwo operands.The result of the relational operator is a Bool value.Ifthe relationship is established,the result is logically true,otherwise i
2、tis logically false.Relational Operators&Relational ExpressionsRelational Operators&Relational Expressions3.5Assume the defined variables:int m=7,n=5;OperatorMeaningFunctionExampleResultLess thanIf the left operand is less than the right operand,the result is true,otherwise the result is false.mnfal
3、se=Less than or equal toIf the left operand is less than or equal to the right operand the result is true,otherwise the result is false.mMore thanIf the left operand is more than the right operand,the result is true,otherwise the result is false.mntrue=More than or equal toIf the left operand is mor
4、e than or equal to the right operand the result is true,otherwise the result is false.m=ntrue=Equal toIf the left operand is equal to the right operand,the result is true,otherwise the result is false.m=nfalse!=Not equal toIf the left operand is not equal to the right operand the result is true,othe
5、rwise the result is false.m!=ntrueRelational Operators&Relational Expressions3.5E.g.1If a positive number n is needed to participate in the calculation,write arelationship expression that determines whether n is a positive number.Solution:n0Explanation:If the relational expression n 0 is true,it mea
6、ns that n is apositive number greater than 0 and can participate in the operation;otherwise,n cannot participate in the operation.E.g.2Write an expression that determines whether x is evenly divisible by 5.Solution:x%5=0Explanation:The arithmetic operator%is used here.If the remainder of xdivided by
7、 5 is 0,it means x isevenlydivisible by 5.Relational Operators&Relational Expressions3.5E.g.3Assuming that a is not equal to 0,write a relational expression thatjudges whether the unary quadratic equation ax2+bx+c=0 has real roots.Solution:b*b-4*a*c=0Explanation:If the result of the relational expre
8、ssion b*b-4*a*c=0 istrue,then the quadratic equation ax2+bx+c=0 has real roots;Otherwise the unary quadratic equation ax2+bx+c=0 has no real root.Question:Multiple types of operators are mixed together,which oneshould be counted first?Relational Operators&Relational Expressions3.5The relational oper
9、ators =should not havespaces between the two symbols,that is,“=When multiple operators are together,the type of expression and thetype of expression value are determined by the last operatorevaluated.For example,int x=5,y=3;then x+1y-2 is a relational expression whose value is true(Booltype).Note the difference between the relational operator=and theassignment operator=.Tips: