1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes...
-
Upload
robert-obrien -
Category
Documents
-
view
224 -
download
0
Transcript of 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes...
![Page 1: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/1.jpg)
1
Relational Expressions
• Relational expressions:
– Expressions that compare operands
– Sometimes called conditions
– Evaluated to yield a result
– Typically used as part of a selection statement
• A simple relational expression consists of a relational operator connecting two variable and/or constant operands
![Page 2: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/2.jpg)
2
![Page 3: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/3.jpg)
3
![Page 4: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/4.jpg)
4
Logical Operators
• Complex conditions can be created using Boolean logical operations:– AND
– OR
– NOT
• AND operator– &&
– True only if both individual expressions are true by themselves
![Page 5: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/5.jpg)
5
Logical Operators (continued)
• OR operator– ||
– True if either one or both of two expressions are true
• NOT operator– !
– Changes expression to opposite state
• Relational operators have higher precedence than logical operators
![Page 6: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/6.jpg)
6
Logical Operators (continued)
• && and || operators:
– Can only be used with Boolean operands
– Second operand not evaluated if evaluation of first is sufficient to determine final value of logical operation
![Page 7: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/7.jpg)
7
![Page 8: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/8.jpg)
8
A Numerical Accuracy Problem
• Tests for equality of numbers using relational operator == should be avoided– Applies to:
• Floating-point
• Double-precision
• Many decimal numbers cannot be represented exactly in binary using a finite number of bits
• Require that absolute value of difference between operands be less than an extremely small value
![Page 9: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/9.jpg)
9
The if-else Statement
• Directs computer to select a sequence of instructions based on the result of a comparison
• Condition is evaluated first
– If the value of condition is true statement1 is executed
– If the value is false the statement after reserved word else is executed
![Page 10: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/10.jpg)
10
The if-else Statement (continued)
if (condition) <-----------------no semicolon here
statement1;
else <-----------------no semicolon here
statement2;
![Page 11: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/11.jpg)
11
![Page 12: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/12.jpg)
12
Compound Statements
• Any number of single statements contained between braces
• Takes place of single statement
• Semicolon is not placed after braces that define compound statement
![Page 13: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/13.jpg)
13
![Page 14: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/14.jpg)
14
The Boolean Data Type
• Tested condition in if-else statement must always evaluate to a Boolean value
• Value of condition must be either true or false• The boolean data type is restricted in its usage as
the value of a relational expression• Boolean values can be:
– Displayed
– Compared
– Assigned
![Page 15: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/15.jpg)
15
One-Way Selection
• No else expression
• Syntax:
if (condition)
statement;
• The statement following if (condition) is only executed if condition has a true value
![Page 16: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/16.jpg)
16
Placement of Braces in a Compound Statement
• Common practice for some programmers:– Place opening brace of compound statement on same
line as if and else statements
if (condition) {
statement1;
} else {
statement2;
}
![Page 17: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/17.jpg)
17
Nested if Statements
• One or more if-else statements can be included within either part of if-else statement
• Last else is with closest unpaired if
– Unless braces alter default pairing
• Process of nesting if statements can be extended indefinitely
![Page 18: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/18.jpg)
18
The if-else Chain
• Else part of an if statement contains another if-else statement
• Syntax:if (expression1)
statement1;
else
if (expression2)
statement2;
else
statement3;
![Page 19: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/19.jpg)
19
The if-else Chain (continued)
• Alternate syntax:
if (expression1)
statement1;
else if (expression2)
statement2;
else
statement3;
• Used extensively in programming applications
![Page 20: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/20.jpg)
20
The switch Statement
• Provides alternative to if-else chain
– For cases that compare the value of an integer expression to a specific value
• Reserved words:
– Switch
– Case
– Default
– Break
![Page 21: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/21.jpg)
21
Switch Syntaxswitch (expression){ // start of compound statement
case value-1: <------------terminated with a colonstatement1;statement2;break;
case value-2: <-------------terminated with a colonstatementm;statementn;break;
default: <-------------------terminated with a colonstatementaa;statementbb;
} // end of switch and compound statement
![Page 22: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/22.jpg)
22
The switch Statement (continued)
• Once entry point has been located:
– All further case evaluations are ignored
– Execution continues through end of compound statement
– Unless break statement is encountered
• The break statement causes an immediate exit from the switch statement
![Page 23: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/23.jpg)
23
Program Design and Development:Introduction to UML
• Explicit design
– Should always be undertaken before coding begins
– Referred to as program modeling
• Unified Modeling Language (UML)
– Program modeling language with its own set of rules and notations
– Not part of Java language
![Page 24: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/24.jpg)
24
Program Design and Development:Introduction to UML
(continued)
• Must understand and specify:
– What the objects in the system are
– What can happen to these objects
– When it can happen
• Each item is addressed by a number of individual and separate views and diagrams
![Page 25: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/25.jpg)
25
UML Diagram Types
• Class• Object • State• Sequence • Activity • Use-case
• Component• Deployment• Collaboration
![Page 26: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/26.jpg)
26
![Page 27: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/27.jpg)
27
Class and Object Diagrams
• Class diagrams – Used to describe classes and their relationships
• Object diagrams – Describe specific objects and relationships
• Attribute– Characteristic that each object in class must have
• Attribute type– Primitive type
– Class type
![Page 28: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/28.jpg)
28
Class and Object Diagrams (continued)
• Visibility
– Defines where the attribute can be seen
– Whether the attribute can be used in other classes or is restricted to the class defining it
• Operations
– Transformations that can be applied to attributes
– Ultimately coded as Java methods
![Page 29: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/29.jpg)
29
![Page 30: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/30.jpg)
30
![Page 31: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/31.jpg)
31
Relationships
• The three basic relationships include:
– Association
– Aggregation
– Generalization
![Page 32: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/32.jpg)
32
![Page 33: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/33.jpg)
33
![Page 34: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/34.jpg)
34
![Page 35: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/35.jpg)
35
Application: A Date Class
• Need to develop a date class
• Extremely important in financial programs
• Design stages:
– Identify type of objects
– Decide how Date object is internally represented
• Identify attributes
– Identify initial set of operations
![Page 36: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/36.jpg)
36
![Page 37: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/37.jpg)
37
User Interfaces, Definitions, and Information Hiding
• User interface consists of: – Class’s public member methods’ header lines
– Supporting comments
• Definition consists of:– Class’s definition section
– Class’s private data members
• Information hiding refers to the principle that how a class is internally constructed is not relevant to any programmer who wishes to use the class
![Page 38: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/38.jpg)
38
Simplifying the Code
• Once a program is working, simplify the code:
– Remove print statements
– Use methods such as setDate() to initialize values in constructors
![Page 39: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/39.jpg)
39
Adding Class Methods
Leap Year Algorithm
If the year is divisible by 4 with a remainder of 0 AND the year is divisible by 100 with a nonzero remainder
OR the year is divisible by 400 with no remainder
then the year is a leap year
Else
the year is not a leap year
EndIf
![Page 40: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/40.jpg)
40
Common Programming Errors• Assuming the if-else statement is selecting an
incorrect choice when the problem is really the values being tested
• Using nested if statements without including braces to clearly indicate the desired structure
• Mistakenly using the assignment operator = instead of the relational operator == when comparing Boolean data
• Forgetting to use a break statement to close off a case within a switch statement
![Page 41: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/41.jpg)
41
Summary
• Relational expressions:
– Referred to as conditions
– Used to compare operands
• if-else statements:
– Used to select between two alternative statements
– Based on a value of a relational or logical expression
– Can contain other if-else statements
![Page 42: 1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649ec45503460f94bcee0d/html5/thumbnails/42.jpg)
42
Summary (continued)
• A compound statement consists of a number of individual statements enclosed within the brace pair { and }
• The switch statement is a multiway selection statement