The Department of Computer Science & Engineering |
STUART C. SHAPIRO: CSE
115 C
|
If you have set of basic operations/actions/behaviors, how can you combine them to make a more complex operation/action/behavior?
According to a theorem proved by Corrado Böhm and Giuseppe Jacopini (CACM, 1966), all you need is:
A block is a sequence of statements surrounded by braces (curly
brackets), { ... }
.
Anywhere a statement is legal, a block may be used.
if (condition) statement1
else statement2
]statement
could be a block.)
condition
is a Java expression that evaluates to a
boolean value (true
or false
).
If the condition
evaluates to
true
, statement1
is
executed.
If the condition
evaluates to
false
, statement1
is not
executed, and if the else
clause is present, statement2
is executed.
A conditional expression in Java is
(condition ? expression1 : expression2)
The parentheses are optional.
If the condition
evaluates to
true
, the conditional expression evaluates to the value
of expression1
.
If the condition
evaluates to
false
, the conditional expression evaluates to the value
of expression2
.
boolean
is a Java primitive (base) type like
int
and double
.
It has only two values, true
and false
.
boolean operators:
not:
p | !p |
---|---|
true | false |
false | true |
and, or, exclusive or, equivalent, not equivalent:
p | q | p && q | p || q | p ^ q | p == q | p != q |
---|---|---|---|---|---|---|
true | true | true | true | false | true | false |
true | false | false | true | true | false | true |
false | true | false | true | true | false | true |
false | false | false | false | false | true | false |
Relational operators:
Operate on two numbers, evaluates to a boolean.
== | equal to |
!= | not equal to |
< | less than |
> | greater than |
<= | less than or equal to |
>= | greater than or equal to |
See Example code.