Chapter 5: Controlling Program Flow with Decision-Making Statements (Web site developers)
Chapter 5: Controlling Program Flow with Decision-Making Statements 115 conclude that the whole expression is false. The fact is that, in Java, any && operator is evaluated before any ||operator. So the expression really asks if 2 < 5 || (something-or-other). Because 2 < 5 is true, the whole expression is true. To change the expression s value from trueto false, you can put the expression s first two comparisons in parentheses, like this: (2 < 5 || 100 < 6) && 27 < 1 Java s ||operator is inclusive. This means that you get a truevalue whenever the thing on the left side is true, the thing on the right side is true, or both things are true. For instance, the expression 2 < 10 || 20 < 30is true. In Java, you can t combine comparisons the way you do in ordinary English. In English, you may say, We ll have between three and ten people at the dinner table. But in Java, you get an error message if you write 3 <= people <= 10. To do this comparison, you need something like 3 <= people && people <= 10. Building a Nest Have you seen those cute Russian Matryoshka nesting dolls? Open up one, and another one is inside. Open up the second, and a third one is inside it. You can do the same thing with Java s ifstatements. (Talk about fun!) Listing 5-5 shows you how. Listing 5-5: Nested if Statements import static java.lang.System.out; import java.util.Scanner; class Authenticator2 { public static void main(String args[]) { Scanner myScanner = new Scanner(System.in); out.print( Username: ); String username = myScanner.next(); if (username.equals( bburd )) { out.print( Password: ); String password = myScanner.next(); if (password.equals( swordfish )) { (continued)
Please check our cheap domain web hosting services, our secret is fast and stable servers, dedicated 24/7 customer support, and many useful FREE bonuses.