## page was renamed from Types Operators and Expressions/2.06 Relational and Logical Operators ## page was renamed from Types Operators and Expressions/2.6 Relational and Logical Operators <> == 2.6 Relational and Logical Operators 关系运算符与逻辑运算符 == The relational operators are {{{ > >= < <= }}} They all have the same precedence. Just below them in precedence are the equality operators: {{{ == != }}} Relational operators have lower precedence than arithmetic operators, so an expression like i < lim-1 is taken as i < (lim-1), as would be expected. 关系运算符包括下列几个运算符:{{{ > >= < <= }}}它们具有相同的优先级。优先级仅次于它们的是相等性运算符:{{{ == != }}}关系运算符的优先级比算术运算符低。因此,表达式i < lim-1等价于i < (1im-1)。 More interesting are the logical operators && and ||. Expressions connected by && or || are evaluated left to right, and evaluation stops as soon as the truth or falsehood of the result is known. Most C programs rely on these properties. For example, here is a loop from the input function getline that we wrote in Chapter 1: {{{ for (i=0; i < lim-1 && (c=getchar()) != '\n' && c != EOF; ++i) s[i] = c; }}} Before reading a new character it is necessary to check that there is room to store it in the array s, so the test i < lim-1 must be made first. Moreover, if this test fails, we must not go on and read another character. 逻辑运算符&&与||有一些较为特殊的属性,由&&与||连接的表达式按从左到右的顺序进行求值,并且,在知道结果值为真或假后立即停止计算。绝大多数C语言程序运用了这些属性。例如,下列在功能上与第1章的输入函数getline中的循环语句等价的循环语句:{{{ for (i=0; i < lim-1 && (c=getchar()) != '\n' && c != EOF; ++i) s[i] = c; }}}在读入一个新字符之前必须先检查数组s中是否还有空间存放这个字符,因此必须首先测试条件i