Presentation is loading. Please wait.

Presentation is loading. Please wait.

Complex Branching Statements CSIS 1595: Fundamentals of Programming and Problem Solving 1.

Similar presentations


Presentation on theme: "Complex Branching Statements CSIS 1595: Fundamentals of Programming and Problem Solving 1."— Presentation transcript:

1 Complex Branching Statements CSIS 1595: Fundamentals of Programming and Problem Solving 1

2 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

3 “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

4 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

5 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

6 Grade Range Example

7 Alternatives to elif Branching Could also use separate if statements, boolean operators if grade >= 90: letter = “A” if grade = 80: letter = “B” if grade = 70: letter = “C” if 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

8 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

9 elif and Menus

10 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

11 Lottery Example

12 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 …

13 Nesting Example 20 questions (actually, much fewer) – Current questions based on answers to previous questions – Prompts and conditions in appropriate branches animal, vegetable, or mineral? animal vegetable mineral reptile or mammal?big or small? rock reptile iguana bark or meow? mammal barkmeow dogcat bigsmall treeflower

14 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

15 20 Questions Code


Download ppt "Complex Branching Statements CSIS 1595: Fundamentals of Programming and Problem Solving 1."

Similar presentations


Ads by Google