1 / 17

Complex Branching Statements

Complex Branching Statements. CSIS 1595: Fundamentals of Programming and P roblem 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:

tambre
Download Presentation

Complex Branching Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 condition1do 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 elifc2: statements if c1 false but c2 true elifc3: statements if c1, c2 false but c3 true elifc4: 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 < 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 onebranch followed • Easier to write reliable code • Still need to do boundary testing

  8. Menus and Branching • Present user with list of options and prompt for choice • Use if/elif to compare choice to each option • If match, execute code for that option in that branch • If reach else, user has not chosen any legal option

  9. Menus and Branching

  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 trueif condition2:statements in condition2 sub-branchstatements in condition2 sub-branchstatements …

  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? mineral animal vegetable rock reptile or mammal? big or small? reptile big small mammal iguana tree flower bark or meow? bark meow dog cat

  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

  16. Validation • Making sure input to program “legal” before running code using that input • Customer can help determine what is “legal” • Often prevents program from crashing • Structure:code to get inputif input not legal:error messageelse:rest of program possibly with other conditions

  17. Validation • Can use elifbranching to handle different types of illegal input with different ways of handling • Structure:code to get inputif input not legal one way:error message for that problemelifinput not legal another way:error message for that problemelif …else:rest of program possibly with other conditions

More Related