It is also possible to test whether more than one condition is satisfied at the same time. E.g.
if ((w < z) && (a == b)) { x = y; }This uses what is known as the AND logical operator, which is represented in the code above by the symbol &&. It does exactly what the name suggests -- the code will set x equal to y only if both w is less than z and a is equal to b. Look carefully at the use of round brackets in the example above. These are important as the conditions within the innermost brackets are evaluated first. When these are found to be either true or false, the && operator asks whether both sets of brackets are true.
There are also other logical operators, including OR and NOT which again do rather what their names suggest.
Exercise 2.3
Look at the code given below.
What is the output from this program when:
if ((a>b) && (c!=d)) { System.out.println("First if block of code executed"); } else { System.out.println("First else block of code executed"); } if ((a==c) || (b<=d)) { System.out.println("Second if block of code executed"); } else { System.out.println("Second else block of code executed"); }