SlideServe
Menu
Préférences
© SlideServe · v2026

Branching and Conditions

lam avatar
11 diapositives · 291 Views · Jul 31, 2014

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 …

Branching and Conditions - slide 1
01 / 11Branching and Conditions
1 / 17

Télécharger la présentation

Branching and Conditions
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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