Sunday, October 7, 2012

10/7/12 Booleans and basic logic

Today I will teach you about logic with booleans. A boolean is a primitive data type that can  only hold a value of 0 or 1. In computer science, You use booleans to represent if a certain statement is true (1) or false (0). An example of a true statement is 2==2 and an example of a false one is 2==3. In java, you can also do complex statements that my include and (&&) or or (||).

Just in case you do not fully understand what I just said, let me give you some examples of what I meant:

1 == 2; is false
2 ==2; is true

Using and and or makes this a little bit more complicated. For && the statement is true iff both sides of the equation are true while for || the statement is true if ether side of the equation is true. I will give you some examples:

true && true; is true
true && false; is false
false && false; is false
true || true; is true
true || false; is true
false || false; is false

We can use both && and || in the same expression. If we do this, we need to know that you evaluate && before ||, but if something is in parentheses, you do that first.  Here are some examples:

true && true || false; is true
false && true || false && false; is false
(true || false) && (false || false); is false
false || true && (false || true); is true

These can get infinity complex, but here are some helpful shortcuts to telling if something will be true or false:
1) true || (stuff) is always true
2) false && (stuff) is always false

Hopefully you learned a thing or two about booleans, and please comment if you have any further questions.

PS- This Saturday, I went to a Quiz Bowl tournament and our team won! Go LASA!

No comments:

Post a Comment