CIS 111 Selection

Objectives

  • Solve problems using Python selection
  • Use Python if statements
  • Use Python if/else statements
  • Use Python nested if/else statements
  • Validate user input (very basic validation)

Single branched selection

if statement - used for single-branched selection if test: statement(s) # executed if test is true
Single branched selection

Two branched selection

if/else - used for two-branched selection if test: statement(s) # executed if test is true else: statement(s) # executed if test is false
Two branched selection

Nested selection

nested if/else - used for complex selection if test1: statement(s) # executed if test1 is true if test2: statement(s) # executed if test1 and test2 are both true else: statement(s) # executed if test1 is true and test2 is false else: statement(s) # executed if test1 is false
Nested selection

Chained selection

chained conditionals if test1: statement(s) # executed if test1 is true elif test2: statement(s) # executed if test1 is false and test2 is true else: statement(s) # executed if test1 and test2 are both false
Chained selection

Sample of nested "if"

if eligibleForOvertime: if hoursWorked > 40: grossPay = hoursWorked * rate + (hoursWorked-40) * rate/2 else: grossPay = hoursWorked * rate

Sample of multi-select flowchart

multi-branched selection