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
42
Embed
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
2
3
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
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
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
7
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
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
10
The if-else Statement (continued)
if (condition) <-----------------no semicolon here
statement1;
else <-----------------no semicolon here
statement2;
11
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
13
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
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
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;
}
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
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;
19
The if-else Chain (continued)
• Alternate syntax:
if (expression1)
statement1;
else if (expression2)
statement2;
else
statement3;
• Used extensively in programming applications
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
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
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
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
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