|
CSCI A201/A597 and I210
Lecture Notes Ten
Second semester 2000-2001
|
Boolean values, expressions, and if statements.
What's this?
x < 15
|
A boolean expression: an expression
whose value is either true or false.
|
Can you define boolean variables in Java?
|
Sure, boolean is a primitive type in Java.
|
boolean b;
|
b can only hold true and
false values.
|
How do you read this?
x <= 15
|
"x is less than or equal to 15"
|
|
How do you write "x is in between 9 and 15"?
|
First, what value does it have?
|
|
We don't know yet, it depends on what x is.
|
So we might as well call it p (x).
|
|
Very well; now we can look at it for particular values of x.
|
p (3) is false
|
p (20) also is false
|
Come to think of it, p (x) is false for many values.
|
|
What's p (x) again?
|
A statement about x being between 9 and 15.
|
When is it true?
|
When
x <= 15
and at the same time
x >= 9
|
|
How do you write AND in Java?
|
&&
|
|
So p (x) can be written as:
|
&& is read as AND
|
(x >= 9) && (x <= 15)
And || is read as OR.
|
While ! stands for NOT in Java.
|
I like A201 !
|
I do ! think this is that funny...
|
So &&, ||, and
are operators for truth values.
|
Yes. How do they work?
|
p |
q |
p && q |
p || q |
! q |
true |
true |
true |
true |
false |
true |
false |
false |
true |
true |
false |
true |
false |
true |
|
false |
false |
false |
false |
|
| |
|
AND |
OR |
NOT |
| |
So && works in the following way: you graduate if you
satisfy both of two requirements.
|
|
Otherwise you don't graduate.
|
|| is a bit more lenient.
|
|
You graduate if you satisfy at least one of the
two requirements.
|
And only when none of them has been satisfied you do not graduate.
|
And ! is easy.
|
It is, indeed.
|
The boolean type is called after mathematician
George Boole, a pioneer in the study of logic.
|
Logic is tricky: suppose a is a boolean
value, true or false. What value does
a || !a
|
Doesn't it depend on the value of a?
|
No.
|
|
Well, then let's look at all possible cases:
| |
a |
! a |
a || (! a) |
true |
false |
true |
false |
true |
true |
| |
Doesn't it look easy now?
|
|
Yes, and there weren't even too many cases.
| How do you compute?
3 + 5 * 2
|
|
Why are you bringing this up?
|
Because as you know there is an implicit order of evaluation for arithmetic
expressions.
|
Does a similar set of rules apply to boolean expressions?
|
Yes. In arithmetic, unary minuses are taken into account before
we do any multiplications...
|
|
... and only after that we may do addtions, if any.
|
If there are no parentheses, otherwise the parens dictate the order of evaluation.
|
What rules govern the order of evaluation for
&&, ||, and ! ?
|
! has the highest priority. Then comes &&,
and the || has the lowest priority.
|
So if you look at
a || b && ! c
|
It's evaluated as
a || (b && (! c))
|
What is the truth table for
!a && !b
|
Let's build it at the same time for
!(a || b)
|
a |
b |
a && b |
!(a && b) |
!a |
!b |
!a || !b |
true |
true |
true |
false |
false |
false |
false |
true |
false |
false |
true |
false |
true |
true |
false |
true |
false |
true |
true |
false |
true |
false |
false |
false |
true |
true |
true |
true |
|
We have just proved one of DeMorgan's law.
|
What is the other one?
|
It's the dual of this:
!(a || b)
|
is the same as
!a && !b
|
|
There are many other identities that one can prove.
|
Perhaps we can do that later, as needed.
|
|
Yes, but let me give some examples, in case you get
bored and want to practice.
|
Sure.
|
|
This
|
... is the same as this
|
a && (b || c)
|
a && b || a && c
|
|
Let's work some more exercises...
|
... with if statements.
|
|
OK, the text of the problem is always the same:
what is the output produced by the following snippet
of code...
|
... when embedded in a complete program. Let's see the snippets.
|
Snippet 1:
int x = 3;
if (2 > x)
System.out.print(1);
else
System.out.print(2);
if (x < 2)
System.out.println(3);
System.out.print(4);
|
Easy. Draw a diagram.
|
Snippet 2:
int x = 3;
if (x > 5) {
if (x < 10)
System.out.print(1); }
else
System.out.print(2);
System.out.print(3);
|
Messy. The curly braces change everything.
|
|
What if you take them out?
|
The diagram changes significantly.
|
|
And you have experienced a dangling else.
|
That's right.
|
int x = 3;
if (x > 0) System.out.print(x + 1);
else if (x > 1) System.out.print(x);
else if (x > 2) System.out.print(x - 1);
else if (x > 3) System.out.print(2 * x);
else System.out.print(x * x);
int x = 3;
if (x > 0) System.out.print(x + 1);
else if (x > 1) System.out.print(x);
else if (x > 2) System.out.print(x - 1);
else if (x > 3) System.out.print(2 * x);
else System.out.print(x * x);
|
|
Who would ever do that in a program?
|
|
Nobody. It's for practice.
|
Messy again. You have to redraw everything.
|
|
I agree it's messy, but is it hard?
|
No. Is this the last one?
|
|
Yes.
|
Can we do a reasonable example now?
|
|
OK, here's problem 19 from problem set 2.
| |
/* Solution to problem nineteen in the first problem set. Use
ConsoleReader from lab notes 2 as explained. The trick here (as
hinted in the text) is to transform a number for a month in a
position (index) in the string where the month name is starting,
all names being made of the same length, and then concatenated
together in one final string. */
public class Nineteen {
public static void main(String[] args) {
String monthNames = "January " +
"February " +
"March " +
"April " +
"May " +
"June " +
"July " +
"August " +
"September " + // longest
"October " +
"November " +
"December " ;
// open a connection with the keyboard
ConsoleReader console = new ConsoleReader(System.in);
// greet the user, and ask for input
System.out.println("Please enter a month number from 1 to 12.");
// get month name
int month = console.readInt();
// report the name of the month
System.out.println(
monthNames.substring("September ".length() * (month-1),
"September ".length()*month));
// formula uses the length of the longest name
}
}
| |
Here is it with if statements:
|
public class P19 {
public static void main(String[] args) {
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("Please enter a month number from 1 to 12.");
int month = console.readInt();
if (month == 12) System.out.println("December");
else if (month == 11) System.out.println("November");
else if (month == 10) System.out.println("October");
else if (month == 9) System.out.println("September");
else if (month == 8) System.out.println("August");
else if (month == 7) System.out.println("July");
else if (month == 6) System.out.println("June");
else if (month == 5) System.out.println("May");
else if (month == 4) System.out.println("April");
else if (month == 3) System.out.println("March");
else if (month == 2) System.out.println("February");
else if (month == 1) System.out.println("January");
}
}
I thought we agreed to use block statements for the bodies of
if statements and else alternatives
all the time.
|
Yes, but just for once I wanted to keep the code somewhat shorter.
|
|
Well, then, just for once, I have two more exercises.
|
OK, I will remember to put braces from now on, always.
|
|
Too late.
|
Where's the first snippet?
|
|
Here it is:
|
Can't be true!
|
if (false && false || true) {
System.out.print(false);
} else {
System.out.print(true);
}
|
Snippet 2:
|
I can see the difference.
|
if (false && (false || true)) {
System.out.print(false);
} else {
System.out.print(true);
}
|
I'm sure you do.
|
That's probably true or false.
|
Last updated: Feb 4, 2001 by Adrian German for A201
|