Lecture 02 Object Oriented Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 02 Object Oriented Programming

Description:

Wash car. Test. cancelled. Study for test. NO. NO. YES. YES ... less than. less than or equal to. greater than. greater than or equal to. equal to. not equal to ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 37
Provided by: jaeki8
Learn more at: http://jsong.ba.ttu.edu
Category:

less

Transcript and Presenter's Notes

Title: Lecture 02 Object Oriented Programming


1
Lecture 04Control Structure - Selection, and
Repetition -
2
Outline
  • Selection
  • if statement
  • switch statement
  • Repetition
  • while statement
  • for statement

3
Control Structure
  • A standard progression of logical steps to
    control the sequence of statement execution
  • Basic control structures
  • Sequence
  • Selection
  • Repetition

4
Flowcharts
  • Often use either flowcharts or pseudocode to plan
    a programs logic
  • Pseudocode
  • Write down the steps required to accomplish a
    given task using English
  • Flowcharts
  • Write the steps you need in diagram form, as a
    series of shapes connected by arrows

5
Sequence Logical Control Structure
  • This is the overriding control structure.
  • Instructions are carried out in order or
    sequence.
  • Action can be inputs, processes, or outputs

Action 1
Action 2
Action 3
6
Selection Logical Control Structure I
  • Instructions are executed depending on the
    outcome of a test also called branching
  • If.then.else

Condition
Condition is FALSE
Condition is TRUE
Action 1
Action 2
7
Decision Making
Looks Like rain
NO
YES
Wash car
Test cancelled
NO
YES
Study for test
8
if Structure
  • Making a decision involves choosing between two
    alternative courses of action based on some value
  • The value upon which a decision is made is a
    boolean value

boolean expression
Comparison operators (relational operator) are
results in boolean values
Resulting action
9
Comparison Operators
lt
less than
lt
less than or equal to
gt
greater than
gt
greater than or equal to

equal to
!
not equal to
The boolean expression, e.g. (userResponse
Y), must appear within parentheses
10
The if Statement
  • Branch only if the condition is True
  • Format
  • if (relational expression)
  • block of statements to be executed if
  • relational expression is true

11
The if Statement The else Clause
  • Branch in one direction if test is True, another
    if False.
  • Format
  • if (relational expression)
  • block of statements executed when True
  • else
  • block of statements executed when False

12
Example
if (studentGrade gt 60) System.out.println (
Passed) else System.out.println (Failed)
Grade gt 60
true
Print passed
false
13
The if Statement Nesting if Statements
  • if and if-else statements can contain any valid
    JAVA statements, including other if or if-else
    statements.
  • Placing an if or if-else within an if or if-else
    is called nesting.
  • When nesting if-else statements within on
    another, care must be taken. The pairing of ifs
    and elses can be tricky!

14
If..else and ifelse if Statement
if (StudentGrade gt 90) System.out.println
(A) else if (StudentGrade gt 80)
System.out.println (B) else if
(StudentGrade gt 70)
System.out.println (C) else
if (StudentGrade gt 60)
System.out.println (D) else
System.out.println(F)
15
Example Grade
16
AND and OR Operators
  • Use AND operator () within a boolean expression
    to determine whether two expression are both
    true.
  • Both boolean expression must be true before the
    action in the statement can occur

if (itemSold gt 3 totalValue gt 1000) bonus
50
17
AND and OR Operators
  • Use OR operator () when you want some action ot
    occur even if only one of two conditions are true

// using two ifs if (itemSold gt 100) bonus
50 else if (totalValue gt 1000) bonus 50
// Using OR operator If (itemSold gt 100
totalValue gt 3000) bonus 50
18
Selection Logical Control Structure II
  • Case control structure
  • Allow more than two choices when the condition
    evaluated

Condition
Condition4 is TRUE
Conditio3 is TRUE
Conditio2 is TRUE
Conditio1 is TRUE
Action 1
Action 1
Action 1
Action 1
19
The switch Statement
  • An alternative to the series of nested if
    statements
  • Uses four keywords
  • switch starts the structure and is followed
    immediately by a test expression enclosed in
    parentheses.
  • case is followed by one of the possible values
    for the test expression and a colon
  • break optionally terminates a switch structure
    at the end of case
  • default optionally is used prior to any action
    that should occur if the test variable does not
    match any case

20
The switch Statement
Switch (year) case 1 System.out.prinln
(Freshman) break case 2
System.out.prinln (Sophomore) break case
3 System.out.prinln (Junior) break
case 4 System.out.prinln (Senior)
break default System.out.println (Invalid
year)
if (year 1) System.out.println
(Freshman) else if (year 2)
System.out.println (Sophomore) else if (year
3) System.out.println (Junior) else if
(year 4) System.out.println
(Senior) else System.out.println(
Invalid year)
21
The switch Statement
  • break statement
  • You can leave out the break statement in a switch
    structure
  • When you omit the break, if the program find a
    match for the test variable, the all statements
    within the switch statement form that point
    forward will execute

22
Example Shipping Option
23
The Repetition Structure
  • Repetition
  • Pre-Test Repetition
  • Sentinel Value Loop
  • Post-Test Repetition
  • Fixed-Count Loop
  • Nested Loops

24
Repetition Logical Control Structure
  • Allows for repeated execution of a set of
    statements based upon the outcome of a condition
    or test.
  • Also known as looping or iteration.
  • Two components of a repetition structure
  • Relational Expression or Test
  • Loop Body

25
Relational Expressions
  • Relational expression
  • Set up as for Selection.
  • Always evaluates to either True (any non-zero
    value) or False (a zero value).
  • Body is executed as long as the test is True.
  • Exit the loop when test is False.

26
Loop Body
  • Is usually a block of code
  • Contains instructions that reflect the task to be
    repeatedly performed
  • Can contain any valid Java statements
  • Usually includes code so that the test will
    eventually prove False (or an infinite loop
    condition will result).

27
Pretest Repetition while Statement
  • Condition is tested before the body is ever
    executed.
  • Use a while loop to execute a body of statements
    continuously while some condition continues to be
    true.

Boolean expression
true
Loop body
false
28
The while Statement
  • while loop can be used to implement pretest
    repetition
  • Format
  • while (relational expression)
  • statements executed while test
  • is True
  • Example
  • While (4 gt2)
  • System.out.println(hello)

29
The while Statement
  • To prevent an infinite while loop
  • The loop control variable is initialized
  • The loop control variable is tested in the while
    statement
  • The body of the while statement must take some
    action that alter the value of the loop control
    variable
  • Example
  • loopCount 1
  • while (loopCount lt 3)
  • System.out.println (Hello)
  • loopCount loopCount 1

30
Increment and Decrement Operators
  • Increment Operators
  • (all add 1 to the variable)
  • int icount 0
  • icount icount 1
  • icount 1
  • icount
  • icount
  • Decrement Operators
  • (all subtract 1 from the variable)
  • int dcounter 0
  • dcounter dcounter - 1
  • dcounter - 1
  • dcounter--
  • --dcounter

31
Example
  • Class Average Program

32
Pretest Repetition the for Statement
  • for loop is counted loop and can also be used to
    implement pretest repetition
  • Use a while loop when you need to perform a task
    some predetermined number of times
  • A loop that executes a specific number of time is
    a counted loop
  • Format
  • for(initialization expression altering)
  • statements executed while condition
  • is True
  • Example
  • for (int counter 1 counter lt10 counter)

Loop condition and final value
Control variable name and initial value
Increment of control variables
33
Output Format
Pakage from java.text import
java.text.DecimalFormat
DecimalFormat precisionTwo new Decimal Format
(0.00)
Pakage from javax.swing
Number of columns
import javax.swing.JTextArea
JTextArea outputTextArea new JTextArea (11,20)
Number of rows
34
Example The for Repetition
35
Posttest Repetition The do-while Statement
  • Condition is tested after the body is executed
  • Body is always executed at least once
  • Often used for data validation, where data is
    validated as it is interactively entered.

36
The do-while Statement
  • do-while loop is used to implement posttest
    repetition
  • Format
  • do
  • statements executed while
  • condition is True
  • while (relational expression)
Write a Comment
User Comments (0)
About PowerShow.com