My Boolean Won't Come Back |
We're getting stuck right in with some more true/false nonsense.
class Hello { public static void main(String[] args) { if (true) System.out.println("This is always printed."); if (false) System.out.println("This is never printed."); } }You should be able to use your understanding of how
ifworks (or your common sense!) to figure out that this prints:
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello This is always printed. [nurmes]btg:And here's another:
class Hello { public static void main(String[] args) { if (true) System.out.println("This is always printed."); else System.out.println("This is also never printed."); if (false) System.out.println("This is never printed."); else System.out.println("This is also always printed."); } }This is probably the world's least useful computer program.
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello This is always printed. This is also always printed. [nurmes]btg:Well, from lots of pointless facts about
boolean, to some really useful stuff which I guarantee you'll use in most of the programs you ever write. Suppose we want to check for two different conditions together, like this:
class Hello { public static void main(String[] args) { int x = 3, y = 4; if (x == 3) if (y == 4) System.out.println("I'm only printing this if x equals 3, and y equals 4."); } }Don't get too scared by the first statement, we've gone back to being lazy and doing everything in one line.
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello I'm only printing this if x equals 3, and y equals 4. [nurmes]btg:If and only if
xequals3, we look at the secondif. When we do look at that, we print the message if and only ifyequals4. What we have is anifwithin anif. To prove it, try changingxto2:
int x = 2, y = 4;Now try changing
yto2as well:
int x = 2, y = 2;Now leave
yas2but putxback to3:
int x = 3, y = 2;Nothing will be printed if you try any of these changes, honest. Now this is all fine, but as with so many things in Java, there's another way of doing it:
class Hello { public static void main(String[] args) { int x = 3, y = 4; if (x == 3 && y == 4) System.out.println("I'm only printing this if x equals 3, and y equals 4."); } }
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello I'm only printing this if x equals 3, and y equals 4. [nurmes]btg:The same result as before (if yours didn't work, check what values
xandyare set to in the first statement). We've introduced the symbol&&, which means "and". In this example we are saying "if x equals 3 AND y equals 4". It's actually as simple as that. You can use this symbol between any two conditions. Any two at all:
class Hello { public static void main(String[] args) { int x = 3, y = 4; if (x == 3 && y == 4) System.out.println("I'm only printing this if x equals 3, and y equals 4."); if (x < y && y > 0) System.out.println("I'm only printing this if x is less than y, and y is positive."); if (x + y == 7 && x - y == -1) System.out.println("I'm only printing this if x plus y equals seven, and x minus y equals minus one."); } }As you can see, the
&&operator is pretty handy.
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello I'm only printing this if x equals 3, and y equals 4. I'm only printing this if x is less than y, and y is positive. I'm only printing this if x plus y equals seven, and x minus y equals minus one. [nurmes]btg:We know that, for example,
x == 3is a condition (ie something which evaluates to abooleanvalue), andy == 4is also a condition. Well in fact, if we think about it,x == 3 && y == 4must also be a condition. Because the only thing we can put into anifis a condition. If you didn't know that, I'm telling you. So we can use the&&operator on it too. To put it simply, we can use&&as many times as we like:
class Hello { public static void main(String[] args) { int x = 3, y = 4; if (x == 3 && y == 4) System.out.println("I'm only printing this if x equals 3, and y equals 4."); if (x < y && y > 0 && x + y == 7 && x - y == -1) System.out.println("There's lots of reasons why I'm printing this, and they're all true."); } }This means, in English, "Print the message if x is less than y AND y is positive AND x plus y equals 7 AND x minus y equals minus one". If you can say that without catching breath then you deserve a prize. Unfortunately it's the sort of thing you often end up writing in big programs. To be clear, all four of these things have to be true for the statement to be printed. Feel free to experiment with different values of
xandyto see this for yourself.From AND to OR - we're going to use the
||operator next. This is used to mean "one condition or another, or both". To put it another way, "at least one condition is true". Let's shut up now and do some examples:
class Hello { public static void main(String[] args) { int x = 3, y = 4; if (x == 3 || y == 4) System.out.println("I'm printing this if x equals 3, or y equals 4, or both."); } }
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello I'm printing this if x equals 3, or y equals 4, or both. [nurmes]btg:Try changing the values of
xandyas before. You'll now find, I hope, that the only time when the message isn't printed isxis not equal to3, ANDyis not equal to4. In fact, we could have written
if (x != 3 && y != 4)and had exactly the same effect. This is true in general; it's always possible to rewrite something from OR to AND, also from AND to OR. As before, we can do as many ORs as we like:
class Hello { public static void main(String[] args) { int x = 3, y = 4; if (x == 3 || y == 13 || x + y == 21 || x - y == -6) System.out.println("Almost anything will make me print this."); } }Even though
ydoesn't equal13, andx + yisn't21, andx - yisn't-6either, because one of the conditions is true, the message is printed:
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello Almost anything will make me print this. [nurmes]btg:Of course you can use these operators in combination. It's at this point that using brackets to make it clear what you mean becomes really important.
class Hello { public static void main(String[] args) { int x = 3, y = 4; if ((x > 0 && y > 0) || (x < 0 && y < 0)) System.out.println("Both x and y are either positive or negative."); } }
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello Both x and y are either positive or negative. [nurmes]btg:The message is printed in this case, or for example when
x = -3andy = -4, but not whenx = 3andy = -4, nor whenx = -3andy = 4.The last point I want to make may seem quite obvious, but technically
trueandfalseare conditions. They're not very exciting, becausetruealways evaluates totrueandfalsealways evaluates tofalse. You can also usebooleanvariables in place of a condition. In other words, you could write the followingifstatement:
if (x == 3 && b && false)If you did, you would be pretty stupid - whatever values
xandbcan take, this will always be false. It's more useful to write something likeif (x == 3 && b && b2)for two boolean variables calledbandb2.One final thing you might want to refer to later is the two different types of AND and OR. There is a strict type and a shortcut type. This is something you'll rarely have to worry about so I'll put it in a later lesson. But look at Lecture Note 22 if you're interested.
Well that's pretty much it for
boolean. The reason there was so much stuff is thatbooleanstuff is concerned with the control of all the rest of the program. Understanding this, andifproperly will help you so much in almost any program that you will ever write. Our next lesson is going to continue looking at the different types in Java.A Numerical Character
Too patronising? Too complex? Typing error? Offended by traffic cones?
Got a question or something I should add? Send an email to ben_golding@yahoo.co.uk !
visits to this site
The contents of this site are copyright of Ben Golding