SELECTION II - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

SELECTION II

Description:

Democrats vote on Wednesday. Everyone ... registration' is set to 'Democrat', otherwise set votingInstruction' to ' ... equals('Democrat') ? 'Wednesday' ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 31
Provided by: thaddeusf
Category:

less

Transcript and Presenter's Notes

Title: SELECTION II


1
SELECTION II
  • CSC 171 FALL 2004
  • LECTURE 9

2
Sequential statements
start
Block
3
Simple Branching
If (test) A
start
test
4
Branching
start
If (test) A else B
test
5
Structured Programming
  • Using structures as building blocks

6
(No Transcript)
7
start

8
start

9
(No Transcript)
10
(No Transcript)
11
(No Transcript)
12
  • if (maritalStatus single)
  • if (income lt 21450.0)
  • taxRate .15
  • else
  • taxRate .27
  • else
  • if (income lt 86500.0)
  • taxRate .28
  • else
  • taxRate .31

13
(No Transcript)
14
  • if ((maritalStatus single)
  • (income lt 21450.0)) taxRate .15
  • if ((maritalStatus single)
  • (income gt 21450.0)) taxRate .27
  • if ((maritalStatus ! single)
  • (income lt 86500.0)) taxRate .28
  • if ((maritalStatus ! single)
  • (income gt 86500.0)) taxRate .31

15
start
16
(No Transcript)
17
  • double rich String effct
  • if (richt gt 8.0)
  • effct most structures fall
  • else if (richt gt 7.0)
  • effct many buildings destroyed
  • else if (richt gt 6.0)
  • effct some buildings damaged
  • else if (richt gt 4.5)
  • effct some damage to poor construction
  • else if (richt gt 3.5) effct no damage
  • else if (richt gt 0) effct not felt much

18
  • int gNum String gName
  • if (gNum 1) gName freshman
  • if (gNum 2) gName sophomore
  • if (gNum 3) gName junior
  • if (gNum 4) gName senior
  • else gName unmatriculated

19
  • int gNum String gName
  • if (gNum 1) gName freshman
  • if (gNum 2) gName sophomore
  • if (gNum 3) gName junior
  • if (gNum 4) gName senior
  • else gName unmatriculated

20
Switch Statement
  • Test cases must be constant
  • Test cases must be integers
  • Test is

21
T
break
F
T
break
F
default
22
  • int gNum String gName
  • switch(gNum)
  • case 1 gName freshman break
  • case 2 gName sophomore break
  • case 3 gName junior break
  • case 4 gName senior break
  • default gName unmatriculated break

23
  • int gNum String gName
  • if (gNum 1) gName freshman
  • if (gNum 2) gName sophomore
  • if (gNum 3) gName junior
  • if (gNum 4) gName senior
  • else gName unmatriculated

24
  • int gNum String gName
  • switch(gNum)
  • case 1 gName freshman
  • case 2 gName sophomore
  • case 3 gName junior
  • case 4 gName senior
  • default gName unmatriculated
  • // everyone is unmatriculated

25
The Selection operator
  • Semantics (3 operands)
  • lttestgt ? ltvalue1gt ltvalue2gt
  • Usually, Shorthand for
  • if (lttestgt) x y else x z
  • Which can be written as
  • x lttestgt ? y z

26
Absolute Value of x
  • int x, absoluteX
  • x myGetInt()// some user input
  • absoluteX (x gt 0) ? x -x
  • Instead of
  • if (x gt0) absoluteX x
  • else absoluteX -x

27
SimpleTaxes
  • double income, taxRate
  • income myGetd()// user input
  • taxRate (income gt 120000.0)
  • ? 0.31 0.40
  • Instead of
  • if (income gt 120000.0)
  • taxRate .31 else taxRate .41

28
Sample Problem Democracy
  • We expect record voter turnout, this election
  • In order to make things run smoothly
  • Democrats vote on Wednesday
  • Everyone else votes on Tuesday
  • We have been asked to write a program to help get
    these instructions out

29
Just for fun
  • Write an implicit if operator that sets a
  • String called votingInstruction to
  • Vote on Wednesday if the string called
    registration is set to Democrat, otherwise
    set votingInstruction to Vote on Tuesday

30
Helping Democracy
  • System.out.println(What party are you?)
  • String registration console.readline()
  • String votingInstruction
  • ((registration.equals(Democrat) ?
  • Wednesday Tuesday)
  • System.out.println(You should vote on
  • votingInstruction)
Write a Comment
User Comments (0)
About PowerShow.com