1 / 11

Branching and Conditions

Branching and Conditions. CSIS 1595: Fundamentals of Programming and P roblem Solving 1. Sequential Program Flow. Sequential flow: Statements executed one after another from beginning to end of program Statement 1 Statement 2 Statement 3 …

lam
Download Presentation

Branching and Conditions

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. Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1

  2. Sequential Program Flow • Sequential flow: • Statements executed one after another from beginning to end of program • Statement 1 • Statement 2 • Statement 3 • … • Python syntax: By default statement ends at end of line • Other languages have symbols to indicate statement end ; • Can use \ to continue statement onto multiple lines (readbility)distance = math.sqrt((x1 – x2)**2 \ (y1 – y2)**2)

  3. Branching • Executing statements only under certain conditions • Condition: Expression that evaluates to either True or False condition statement in branch statement in branch … statement after branch Do these if condition true Skip if condition false

  4. Absolute Value • Requirement: Input number and print its absolute value • Pseudocode: • Input number (converting to float) • If the number is less than 0, multiply itself by -1 • Print the number • Condition: If the number is less than 0 • Statements executed only if true:multiply itself by -1

  5. Python Syntax if condition:statement to be executed if truestatement to be executed if truestatement to be executed if true … first statement executed regardless • conditionis a boolean expression • Indentation used to indicate what is part of branch

  6. Absolute Value Example

  7. Branching and Indentation • How does Python know where a branch ends? • Indentation used in Python • Lines after if indented only executed if condition true • Other languages use { … }, etc. • Readability: All statements in same branch should be lined up (same indentation)

  8. Branching and Indentation • “Negative!” only printed if number < 0(inside branch) • “Negative!” always printed (outside branch)

  9. If-Else Branching • Executing another set of statements if condition false if condition:statements to be executed if true … else: statements to be executed if false … first statement executed regardless

  10. Example: Coin Flip • Goal: Randomly print “Heads” or “Tails” • random module: simple random number generators • random.random() random float between 0 and 1 • Algorithm: • Generate random number between 0 and 1 • If number > 0.5, print “Heads” • Otherwise, print “Tails” • In either case, print “Thanks for playing” afterwards

  11. Example: Coin Flip

More Related