Conditional Logic in Smalltalk - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Conditional Logic in Smalltalk

Description:

– PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 29
Provided by: donaldl1
Category:

less

Transcript and Presenter's Notes

Title: Conditional Logic in Smalltalk


1
Conditional Logic in Smalltalk
  • Control Structures
  • Boolean Logic
  • If Statements

2
Control Structures
  • Bohm and Jacopini developed the theory
  • All computer languages need each structure
  • All are necessary to harness the power of
    computers
  • Control Structures are
  • Sequence
  • Selection
  • Iteration

3
Sequence
  • Executing instructions in sequence
  • Working down the page

4
Selection
  • Conditional logic
  • Choosing between alternative paths based on some
    predefined test
  • Commonly implemented as If statements

5
Iteration
  • Executing a set of instructions more than once
  • A loop
  • There must be a mechanism for exiting the loop
  • if not an infinite loop
  • Exit mechanism always involves some test

6
Boolean Logic
  • (10 gt 12) evaluates to
    false
  • (3 lt 14) evaluates to
    true
  • (Joe lt Sam) evaluates to
    true
  • (a gt b)
  • (amtDue gt amtPaid)
  • (custName gt custBalance)

7
true and false
  • true is an instance of True
  • false is an instance of False
  • True and False are subclasses of the class
    Boolean

8
More Logic
  • amtOwed amtPaid
  • amtPaid 1000
  • amtOwed 2000
  • (amtPaid gt amtOwed)
  • how about if amount paid is 3000

9
Look Up Boolean in the Class Browser
  • The Hierarchy
  • Object
  • Boolean
  • False
  • True

10
Messages for True
  • (and)
  • (or)
  • not
  • ifFalse
  • ifTrue
  • ifTrueifFalse
  • ifFalseifTrue

11
Messages for False
  • (and)
  • (or)
  • not
  • ifFalse
  • ifTrue
  • ifTrueifFalse
  • ifFalseifTrue

12
Logical AND -- (and)
  • true true evaluates to
    true
  • true false evaluates to
    false
  • false true evaluates to
    false
  • false false evaluates to
    false
  • and is an equivalent shortcut method

13
Logical OR -- (or)
  • true true evaluates to
    true
  • true false evaluates to
    true
  • false true evaluates to
    true
  • false false evaluates to
    false
  • or is an equivalent shortcut method

14
Some Samples
  • (a gt b) (7 lt 5)
  • (a gt b) (7 lt 5)
  • (atBats gt 100) (battingAverage gt 300)
  • (amtOwed gt amtPaid) ((lastPaymentDate
    subtractDate dueDate) gt 30)
  • Hint Use lots of parentheses

15
Conditional Logic
  • boolean
  • ifTrue block of code.
  • boolean
  • ifFalse block of code.
  • boolean
  • ifTrue block of code
  • ifFalse block of code.

16
Blocks
  • A Block is one or more Smalltalk statements
  • a module (i.e., hunk of code)
  • Contained in square brackets
  • Used with conditional logic
  • A block can be
  • an argument
  • a receiver
  • A block is executed when it receives the selector
    value

17
Examples of Blocks
  • 85 12 / 34
  • to execute this
  • 85 12 / 34 value.
  • newAmt oldAmt 1.1.
  • Transcript show New Amt Is , newAmt
    printString.

18
Conditional Logic
  • boolean
  • ifTrue block of code.
  • boolean
  • ifFalse block of code.
  • boolean
  • ifTrue block of code for true
  • ifFalse block of code for false.

19
IfTrue Examples
  • (Janet lt Pete)
  • ifTrue Janet comes first.
  • .
  • (Janet gt Pete)
  • ifTrue Janet comes first.
  • .

20
IfTrue More Complex Examples
  • (amtRequestedFromATM gt maxWithdrawal)
  • ifTrue window show Sorry, this
    transaction is not approved.
  • customerBeingServed
    transactionReject.
  • .

21
IfFalse Examples
  • (Janet lt Pete)
  • ifFalse Janet comes first.
  • .
  • (Janet gt Pete)
  • ifFalse Janet comes first.
  • .

22
IfTrueIfFalse Examples
  • minAmt curBalance
  • (minAmt lt curBalance)
  • ifTrue status OK
  • ifFalse status not OK.

23
IfTrueIfFalse Example Continued
  • minAmt curBalance
  • minAmt 100.0.
  • curBalance 300.0.
  • (minAmt lt curBalance)
  • ifTrue status OK
  • ifFalse status not OK.

24
Nested Ifs
  • boolean1
  • ifTrue true block
  • ifFalse false block.
  • The true and and false blocks can themselves
    contain conditional statements
  • boolean1
  • ifTrue (boolean2)
  • ifTrue block1
  • ifFalse block2
  • ifFalse (boolean3)
  • ifTrue block3
  • ifFalse block4
  • .

25
Nested Ifs -- Social Security Example
  • prompter wage yearToDateSocSec
  • prompter CwTextPrompter new.
  • prompter title 'Enter Wage'.
  • wage prompter prompt.
  • wage wage asDecimal asFloat.
  • prompter CwTextPrompter new.
  • prompter title 'Enter Year-To-Date Social
    Security'.
  • yearToDateSocSec prompter prompt.
  • yearToDateSocSec yearToDateSocSec asDecimal
    asFloat.

26
If Message (example continued)
  • (yearToDateSocSec lt 3757.20)
  • ifTrue
  • (yearToDateSocSec (wage
    0.062) lt 3757.20 )
  • ifTrue wage 0.062
  • ifFalse 3757.20 -
    yearToDateSocSec
  • ifFalse 0.

27
Summary -- Conditional Logic
  • Conditional logic is necessary for most programs
  • Conditional logic allows you to take different
    paths through a program based on predefined
    criteria
  • Conditional logic in Smalltalk uses message
    passing

28
Summary -- If statements
  • starts with an expression in parentheses that
    evaluates to true or false
  • this is followed by a keyword message with such
    selectors as ifTrue ifFalse
    ifTrueifFalse
  • each of the keywords is followed by a block which
    is one or more statements in square brackets
  • formatting is very important for readability
  • writing If statements is easy after a little
    practice
Write a Comment
User Comments (0)
About PowerShow.com