SUNY Geneseo Department of Computer Science
Conditionals
and Logic
{Date}
CSci 141, Fall 2003
Prof. Doug Baldwin
Return to
List of Lectures
Previous Lecture
Misc
Hand out Lab 3
Java clock function = System.currentTimeMillis()
- See Sect. 4.7
- Digression:
- "Static" members = data or methods associated with class,
not an object
System
is a library class
- Automatically imported into every program
currentTimeMillis
is a static method in System
out
is a static variable in System
- Handles
println
, print
, etc. messages
- Also
public static void main(...)
Questions?
Conditionals and Logic
Sections 5.1 through 5.5
Terminology and format
- Test
- Action done if true
- Alternative, done if test is false
Testing or fundamental logic
- And
- Or (either or both)
- Exclusive Or (one or the other)
- Negation
- Is !!p equivalent to p in Java programs?
- Meanings (semantics) are the same
- !!p has two operations to perform
- (!!p == p) will always be true
- Implication (like modus ponens)
Boolean algebra
- Complex truth tables and proofs
- Math rules -- commutativity, associativty, etc.
Correctness
Analysis of conditionals
- Time has to be bounded -- best and worst cases
- And average (not really for this course though)
- Empirical issues -- selecting data to force into worst case, best case,
average (tricky)
Examples
The generic search (e.g., search array A of n elements for value x)
- loop until either A[i] = x or i >= n
- But Java (and many other languages) have "while", not "until"
-- how do you change the loop?
- loop while !( either A[i] = x or i >= n )
- loop while A[i] != x or i < n
- oops - loop while A[i] != x and i < n
- DeMorgan's law
Is a point on a cylinder?
- x2 + z2 = 1 and 0 <= y <= 1
- or y = 0 and x2 + z2 < 1
- or y = 1 and x2 + z2 < 1
- Lots of places in this expression to write the logic differently -- factoring
common terms out of some expressions (distributive law), replacing explicit
logic with conditional statements, etc.
Next
Introduction to Recursion
Read Sec. 6.1
Next Lecture