elif - YSU CSIS

Complex Branching Statements
CSIS 1595: Fundamentals of
Programming and Problem Solving 1
Multiple Outcomes in Branching
• Examples:
– Grading: numeric value  A, B, C, D, or F
– 20 questions:
• Multiple questions
• Next question depends on answer to previous questions
• Tools:
– elif statement for chain of “else if”
– Nested branches
“Else if” Branching
• Basic structure
–
–
–
–
–
if condition1 do something
else if condition2 do something else
else if condition3 do something else
…
else do something
• Basic idea:
– If first n conditions fail, try condition n+1
– Stop at first condition that succeeds
– If all conditions fail, do something else
elif Syntax
if c1:
statements if condition1 true
elif c2:
statements if c1 false but c2 true
elif c3:
statements if c1, c2 false but c3 true
elif c4:
statements if c1, c2, c3 false but c4 true
… as many elif’s as needed
else:
statements if all conditions false
Grade Range Example
• Translating numeric grade to letter grade
– 90 & above  A, 80 up to 90  B, 70 up to 80  C, 60 up to 70  D, < 60  F
• Idea: Each condition eliminates subrange of grades
– grade >= 90  A:
• Remaining number in range 0 – 90, must be B, C, D, or F
– grade >= 80  B:
• Remaining numbers in range 0 – 80, must be C, D, or F
– grade >= 70  C:
• Remaining numbers in range 0 – 70, must be D or F
– grade >= 60  C:
• Remaining numbers in range 0 – 60, must be F
Grade Range Example
Alternatives to elif Branching
• Could also use separate if statements, boolean operators
if grade >= 90:
letter = “A”
if grade < 90 and grade >= 80:
letter = “B”
if grade < 80 and grade >= 70:
letter = “C”
if grade < 70 and grade >= 60:
letter = “D”
if grade < 60:
letter = “F”
• elif branching  one and only one branch followed
– Easier to write reliable code
– Still need to do boundary testing
elif and Menus
• Menu = list of choices for user
– Take different actions depending on user choice
• Can implement with elif
– Present list of choices
– Input user choice
– Use elif to compare with each legal option
• Put corresponding action down its branch
– If reach else user did not choose legal option
elif and Menus
Randomness and Ranges
• Many games use random values to make decisions
– Some actions more likely than other
• Example: Lottery program
–
–
–
–
50%  payout = 0
40%  payout = $10
9%  payout = $100
1%  payout = $1000
• Approach:
– Generate random value
– Create ranges by summing probabilities already covered by peviously
checked outcomes
– Use elif to implement the ranges
Lottery Example
Nesting and Branches
• Often have large list of statements in a branch
• Than list can contain other if statements (sub-branch)
– Only executed if first branch followed
– Indentation to appropriate level to show level of nesting
if condition1:
statements if condition1 true
if condition2:
statements in condition2 sub-branch
statements in condition2 sub-branch
statements
…
Nesting Example
• 20 questions (actually, much fewer)
animal, vegetable, or mineral?
animal
vegetable
reptile or mammal?
reptile
iguana
rock
big or small?
big
mammal
small
tree
bark or meow?
bark
meow
dog
cat
mineral
flower
– Current questions based on answers to previous questions
– Prompts and conditions in appropriate branches
Nesting Pseudocode
Prompt for animal, vegetable, or mineral
If animal:
Prompt for reptile or mammal
If reptile:
Guess iguana
If mammal:
Prompt for bark or meow
If bark:
Guess dog
If meow:
Guess cat
If vegetable:
Prompt for big or little
If big:
Guess tree
If small:
Guess flower
If mineral:
Guess rock
20 Questions Code