Lecture-14 Selection_Nested_2024-25
Lecture Overview
COMP101 Introduction to Programming
Topic: Selection in Programming
Focus: Nested Selection Structures
Page 1: Introduction to Nested Selection
Lecture-14: Focus on Nested if Statements.
Page 2: Nested if Structure
Candidates for election must be:
Female
Minimum age: 21
Code snippet:
candidate = input("Candidate gender: ") age = int(input("Enter age of candidate: ")) if candidate == "female": if age >= 21: print("female 21 or over") print("accept") else: print("female, but not 21") print("dismiss") else: print("Not female") print("construct exited")Execution Process:
Outer if executes if candidate is 'female'.
Inner if checks if age is >= 21.
Else statements provide feedback based on conditions not met.
Page 3: Nested if Compound Statement
New code structure:
if candidate == "female" and age >= 21: print("female 21 or over - accept") elif age < 21: print("Underage - not yet 21 - dismiss") else: print("Candidate not female - dismiss")Explanation:
ifexecutes when both checks are true.eliftests another condition wheniffails.elsecaptures any failure cases.
Page 4: Compound Statement Enhanced
Revised code structure:
if candidate == 'female' and age >= 21: print("female 21 or over - accept") elif candidate == 'female' and age < 21: print("Female but underage - dismiss") else: print("Candidate not female - dismiss")Summary:
Each
eliftests specific conditions to clarify checks made inif.
Page 5: Testing Conditions
Test Cases and Expected Results:
Input (gender, age)
Expected
Actual
female, 21
accept
accept
female, 22
accept
accept
female, 20
dismiss
dismiss
male, 21
dismiss
dismiss
male, 22
dismiss
dismiss
male, 20
dismiss
dismiss
Testing Considerations:
Evaluates 3 conditions, minimum of 6 tests needed.
Page 6: Nested if Using Elif
Initial versus improved structure:
# Initial if score >= 90: print("A") else: if score >= 80: print("B") ... # Improved if score >= 90: print("A") elif score >= 80: print("B") ...Benefits of using
elif:Cleaner structure.
Reduces deep nesting.
Page 7: Color Input Example
Code snippet for colour detection:
colour = input("Enter a colour from the spectrum: ") if len(colour) <= 6: if len(colour) == 3: print("Colour is red") elif len(colour) == 4: print("Colour is blue") ... else: print("Colour is not in spectrum")
Page 8: Dog Age Calculation
Example for calculating human age equivalent:
dog_Age = int(input ("Dog's age?: ")) if dog_Age == 1: print("14 years human") elif dog_Age == 2: print("22 years human") elif dog_Age > 2: human_Age = (22 + (dog_Age - 2) * 5) print(human_Age) else: print ("Too old")
Page 9: Initial Draft for UK Origin
Example to determine where person was born:
bornIn = input("Where in the UK where you born?: ") if bornIn == "England": print("You are English") elif bornIn == "Ireland": ... else: print("You are Welsh")Discussion of logic failures in the structure.
Page 10: Second Draft Handling Input Case
Enhanced case handling:
bornIn = input("Where in the UK where you born?: ").lower() ...Implementing boolean operators to cover variations in input case.
Page 11: Upper/Lower Case Standardization
Final recommended structure:
bornIn = input("Where in the UK where you born?: ").upper() if bornIn == "ENGLAND": print("You are English") ...Ensuring consistency and functionality regardless of input case.
Page 12: Dangling Else Problem
Logic Error:
Caused by unintended interactions of indents.
Suggested to add an additional else for clarity and debugging.
Page 13: Elaborating on Dangling Else
Revised Approach:
Each
ifshould correspond to anelsefor better logical clarity.
Page 14: Complicated Conditional Structure
Overview of complex conditions:
Tests should focus on clear paths for execution.
Need for testing and clarity in logic due to complexity.
Page 15: Flowchart Testing
Importance of 100% decision coverage:
Tests for conditions A, D, E, F.
Page 16: Implementing Flowchart Test Cases
Scenario Testing for inputs and expected outcomes
Ensures that all logic paths are covered in conditions to ensure robust testing.