512逻辑运算符 逻辑非有最高的优先级,其次是逻辑与,最后才是 逻辑或。 !逻辑非 a i a &逻辑与 true alse 逻辑或 true a a&& b i b true true true true true false alse true falsetrue se true false false alse false JAVA
6 5.1.2 逻辑运算符 • 逻辑非具有最高的优先级,其次是逻辑与,最后才是 逻辑或。 ! 逻辑非 && 逻辑与 || 逻辑或 a b a && b a || b true true true true true false false true false true false true false false false false a !a true false false true
逻辑运算符经常构成复然拌為 考虑下面的语句,它在什么情况下将执行输出语句? if(total MAX && found) System. out. printin ("Processing " )i total<MAX found Ifound total < MAX &&Ifound false false true false false true false false true fal true true true true false false JAVA
7 逻辑运算符经常构成复杂的条件 total < MAX found !found total < MAX && !found false false true false false true false false true false true true true true false false 考虑下面的语句,它在什么情况下将执行输出语句? if (total < MAX && !found) System.out.println ("Processing…");
注意: >运算符&&和具有一个重要的特征:短路性。 也就是说左边的操作数已经足以确定整个运算 的结果,那么右边的操作数就不会再参与运算。 8 JAVA
8 注 意: ➢ 运算符&& 和||具有一个重要的特征:短路性。 ➢也就是说左边的操作数已经足以确定整个运算 的结果,那么右边的操作数就不会再参与运算
5.2if语句(选择语句、条件语句 语句的基本语法 if保留字 条件表达式,布尔型值 condition evaluated if( condition statement true false 如果表达式为true,则执行 statement statement, 否则执行下一条语句。 例:5.1 JAVA
9 5.2 if 语句(选择语句、条件语句) • If语句的基本语法: if ( condition ) statement; if 保留字 条件表达式,布尔型值 如果表达式为true,则执行 statement, 否则执行下一条语句。 condition evaluated statement true false 例:5.1
5.2.1 if-else statement If-else语句可以使 程序在某个条件表 condition 达式的值为true时 evaluated 执行一段代码,其 值为fase时执行 true false 另一段代码。 statement1 statement2 在例:52。。。。。。 10 JAVA
10 5.2.1 if-else statement condition evaluated statement1 true false statement2 例:5.2。。。。。。。 If-else语句可以使 程序在某个条件表 达式的值为true时 执行一段代码,其 值为false时执行 另一段代码