







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to boolean logic, including boolean variables and values, logical operators &&, ||, and !, short circuit evaluation, and de morgan's laws. It includes examples and explanations of exclusive or, inclusive or, and logical operator precedence.
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Boolean Values/Expressions
Copyright 2006 by Pearson Education 2
boolean : A primitive type to represent logical values. A boolean expression produces either true or false. The
Examples: boolean minor = (age < 21); boolean expensive = (iPhonePrice > 500.00); boolean iLoveCS = true; if (minor) { System.out.println("Can't purchase " + "alcohol!"); } You can create boolean variables, pass boolean parameters, return boolean values from methods, ...
2 > 3 && 4 < 5 F &&? = F
2 < 3 || 4 > 5 T ||? = T (^7)
Not! Highest Precedence
And &&
Or || Lowest Precedence
Example:
2 < 3 || 4 > 5 && 6 > 7
HINT: When in doubt, use parentheses!
public static double calculateTax(boolean isMarried, int income) { double tax = 0; if (isMarried) { if (income <= 16000) { tax = .10 * income; } else if (income <= 64000) { }^ tax = 1600 + .15 * (income - 16000); else { tax = 8800 + .25 * (income - 64000); } } else { (^) if (income <= 8000) { }^ tax = .10 * income; else if (income <= 32000) { tax = 800 + .15 * (income - 8000); } else { }^ tax = 4400 + .25 * (income - 32000); } return tax; } NOTE: Class constants should be used instead of magic numbers!!!