Docsity

Understanding Selection Statements and UML in Java Programming, Slides of Applications of Computer Sciences

Toggle sidebar
bg1
Chapter 5:
Selection
Object-Oriented Program
Development Using Java:
A Class-Centered
Approach
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

Download Understanding Selection Statements and UML in Java Programming and more Slides Applications of Computer Sciences in PDF only on Docsity!

Chapter 5:

Selection

Object-Oriented Program

Development Using Java:

A Class-Centered

Approach

Objectives

  • Relational Expressions
  • The if-else Statement
  • Nested if Statements
  • The switch Statement
  • Program Design and Development: Introduction to UML
  • Application: A Date Class
  • Common Programming Errors

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

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

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

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

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

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

One-Way Selection

  • No else expression
  • Syntax:

if (condition)

statement;

  • The statement following if (condition) is only executed if condition has a true value

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

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;