{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec4

lec4 - CSE 8A Lecture 4 Programming and logic Comparison...

This preview shows pages 1–6. Sign up to view the full content.

Page 1 of 24 CSE 8A, UCSD LEC 4 CSE 8A: Lecture 4 Programming and logic Comparison operators Boolean operators Conditional statements Comments (Reading: Savitch, Ch. 2 , Ch. 3)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Page 2 of 24 CSE 8A, UCSD LEC 4 Programming and logic Computers are universal logic machines (though they can be programmed to act illogically!) To get them to do what you want them to do (the goal of programming), it is essential to understand basic logic One important aspect of this is the syntax and semantics of Boolean expressions (“Boolean” comes from the name of George Boole, a 19th century mathematician who developed the kind of true-false logic now used in programming.)
Page 3 of 24 CSE 8A, UCSD LEC 4 Boolean expressions In a programming language, Boolean expressions are expressions which have a value that is interpreted as True or False In Java, Boolean expressions have a value of the primitive type boolean , which is not a numeric type, and will never be converted to or from any numeric type A Java expression of boolean type has one of two possible values, which can be expressed with the boolean literal constants true or false We will look at: comparison operators which generate boolean values, and boolean operators which combine boolean values, and using boolean expressions to control branching and iteration

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Page 4 of 24 CSE 8A, UCSD LEC 4 Comparison operators in Java Comparison operators take two arguments, and return the boolean value true or false depending on whether the comparison of the values of the arguments is true or false: e1 == e2 // has value true iff e1 ’s value equals e2 ’s e1 != e2 // has value false iff e1 ’s value equals e2 ’s e1 < e2 // has value true iff e1 ’s value is less than e2 ’s e1 > e2 // has value true iff e1 ’s value is greater than e2 ’s e1 <= e2 // has value true iff e1 ’s value is less or equal to e2 ’s e1 >= e2 // has value true iff e1 ’s value is greater or equal e2 ’s Comparison operators have lower precedence than any arithmetic operators, so e.g. a + b < c + d compares the two sums Comparison operators associate left-to-right Comparison operators have no side effects Automatic type conversions of arguments happen as for numeric binary operators
Page 5 of 24 CSE 8A, UCSD LEC 4 Nonnumeric equality testing The operators > , < , >= , <= work only on arguments of non- boolean primitive types The == and != operators will also work on arguments of boolean type, and on arguments of reference type However, on reference type arguments, the == operator has value true (and the != operator has value false) if and only if its arguments reference exactly the same object (what is really compared is the addresses of the object(s) location(s) in memory) Often you are really more interested in whether two objects contain data that has the same value To test for this, you can use one of the object’s equals() instance method. In Java, every object has an equals() instance method, which takes another object as argument In particular, when comparing two String objects, you can use the equals() method of one of them

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 24

lec4 - CSE 8A Lecture 4 Programming and logic Comparison...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online