Booleans
August 13, 2021 (08:00:08 AM)
This lab serves four core goals:
- To help you manipulate boolean values,
- To practice boolean operators,
- To understand the concept of precedence,
- To practice simple mental calculations.
Truth Tables
- Copy-and-paste the following code into the
Mainmethod of a new project:
Console.WriteLine("Conjunction (and, &&) truth table:"
+ "\n\n && ||\t" + true+ "\t| " + false
+ "\n------||------------|--------"
+ "\n" + true + " ||\t" + (true && true)+ "\t| " + (true && false)
+ "\n" + false+ " ||\t" + (false && true)+ "\t| " + (false && false)
+ "\n\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
Console.WriteLine("Negation (not, !) truth table:"
+ "\n\n value ||\t ! \t "
+ "\n---------||------"
+ "\n" + true+ "\t ||\t" + false
+ "\n" + (!true)+ "\t ||\t" + (!false)
+ "\n\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");- Compile and execute it. This should display to the screen truth tables for conjunction (and,
&&) and negation (not,!). - Make sure you understand both the code and its output.
- Add after the truth table for the negation, write code to display truth tables for
- the binary operators disjunction (or,
||), - identity (equality,
==) and - difference (inequality,
!=). Normally, using the find-and-replace feature of your IDE should make this a quick and easy task.
- the binary operators disjunction (or,
- You can make sure you completed this exercise correctly by checking that your output match the truth tables on wikipedia for disjunction and equality. For inequality, in this case check against the table for exclusive disjunction. Exclusive disjunction (XOR) is conceptually different than inequality, but has the same truth table.
Precedence and Order of Evaluation
Reading and Understanding
If you read the documentation on operator precedence, you will see that operators are evaluated in a particular order. From higher precedence (that is, evaluated first) to lower precedence (that is, evaluated last), this order is: ! (* / %) (+ -) (< > <= >=) (== !=) && ||. Inside each group in parenthesis, operations are evaluated from left to right.
So that, for instance, ! true || false && 3 * 2 == 6 will be evaluated as
! true || false && 3 * 2 == 6 |
⇒ | false || false && 3 * 2 == 6 |
false || false && 3 * 2 == 6 |
⇒ | false || false && 6 == 6 |
false || false && 6 == 6 |
⇒ | false || false && true |
false || false && true |
⇒ | false || false |
false || false |
⇒ | false |
Note that an expression like !3 > 2 doesn’t make any sense: C# would try to take the negation of 3, but you can’t negate the truth value of an integer! Along the same lines, an expression like false * true doesn’t make any sense: you can not multiply booleans (what would be “true times false”?)! Similarly, 3 % false will cause an error: can you see why? These are all examples of “illegal” expressions.
Computing Simple Boolean Expressions
Evaluate the following expressions. Try to do this “by hand,” and write your answers down on paper.
true && false || true!true && falsefalse || true && !falsefalse == !true || false!(true || false || true && true)!(true || false) && (true && !false)!true || false && (true && !false)true != !(false || true)
Computing Expressions Involving Booleans and Numerical Values
For each of the following expressions, decide if it is “legal” or not. If it is, give the result of its evaluation.
3 > 22 == 43 >= 2 != false3 > falsetrue && 3 + 5 * 8 == 433 + true != false