{"base":{"id":"cs10-05","subject":"Programming","title":"Selection: if, elif and else","scene":"py10","steps":[{"mode":"ifelse","eyebrow":"Making decisions","title":"if and else","body":"Selection lets a program choose a path. The condition after if is either True or False. If True, the indented lines run; if False, the else lines run instead. Try scores either side of 50.","controls":[{"id":"score","label":"Score","min":0,"max":100,"step":1,"value":62}],"actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"elif","eyebrow":"More than two paths","title":"elif for extra choices","body":"elif means 'else if'. Python checks each condition in order and runs only the FIRST one that is True, then skips the rest. That's why the order of conditions matters.","controls":[{"id":"mark","label":"Mark","min":0,"max":100,"step":1,"value":71}],"actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"bnd","eyebrow":"Testing boundaries","title":"The exact value where it changes","body":"Most selection bugs hide at the boundary. A pass mark of 50 must pass. Run the tests on this code, find the failing one, then fix the comparison.","actions":[{"id":"run","label":"Run the tests"},{"id":"fix","label":"Fix the code"}]},{"mode":"andor","eyebrow":"Combining conditions","title":"and, or, not","body":"A ride needs riders aged at least 12 AND at least 140 cm tall. 'and' needs both conditions True; 'or' needs at least one. Move the sliders and read the truth table.","controls":[{"id":"age","label":"Age","min":5,"max":18,"step":1,"value":11},{"id":"height","label":"Height (cm)","min":100,"max":190,"step":5,"value":145}]},{"mode":"ticket","eyebrow":"A real example","title":"Ticket price calculator","body":"Children under 12 pay $5, people aged 65 or over pay $7, everyone else pays $12. Run it, then test the boundary ages 11, 12, 64 and 65.","controls":[{"id":"age","label":"Age","min":1,"max":90,"step":1,"value":30}],"actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"quiz","look":"andor","eyebrow":"Check yourself","title":"Which branch runs?","body":"x = 7\nif x > 10: print(\"A\")\nelif x > 5: print(\"B\")\nelse: print(\"C\")","question":"What is printed?","options":["A","B","C"],"answer":1,"right":"Correct. 7 > 10 is False, 7 > 5 is True, so B is printed and else is skipped.","wrong":"Not quite. Test each condition in order and stop at the first True one."}],"more":[{"type":"cards","id":"worked","eyebrow":"Worked examples","title":"Code to read","intro":"Indentation (4 spaces) shows which lines belong to each branch.","items":[{"tag":"Python","title":"Odd or even","body":"n = int(input(\"Number? \"))\nif n % 2 == 0:\n    print(n, \"is even\")\nelse:\n    print(n, \"is odd\")","code":true},{"tag":"Python","title":"Nested if","body":"if age >= 18:\n    if has_ticket:\n        print(\"Come in\")\n    else:\n        print(\"Buy a ticket\")\nelse:\n    print(\"Adults only\")","code":true},{"tag":"Pseudocode","title":"IF … ELSE … ENDIF","body":"INPUT Temp\nIF Temp > 30\n  THEN\n    OUTPUT \"Hot\"\n  ELSE\n    IF Temp < 10\n      THEN OUTPUT \"Cold\"\n      ELSE OUTPUT \"Mild\"\n    ENDIF\nENDIF","code":true},{"tag":"Pseudocode","title":"CASE for many fixed choices","body":"CASE OF Day\n  \"Sat\" : OUTPUT \"Weekend\"\n  \"Sun\" : OUTPUT \"Weekend\"\n  OTHERWISE OUTPUT \"School day\"\nENDCASE\n\n(Python 3.10+ has match … case)","code":true}]},{"type":"cards","id":"mistakes","eyebrow":"Common mistakes","title":"Watch out for these","items":[{"tag":"Mistake","title":"= instead of ==","body":"= stores a value; == compares two values. if x = 5: is a syntax error in Python."},{"tag":"Mistake","title":"Conditions in the wrong order","body":"If you test mark >= 50 before mark >= 80, an 85 gets a C, because Python stops at the first True condition. Put the strictest test first."},{"tag":"Mistake","title":"> when you mean >=","body":"'At least 50' means >= 50. Using > 50 makes 50 fail. Always test the boundary value."},{"tag":"Mistake","title":"Missing colon or indentation","body":"Every if, elif and else line ends with a colon, and the lines under it must be indented."}]},{"type":"cards","id":"method","eyebrow":"Set out your answer","title":"Writing selection in tests","items":[{"tag":"1 · One condition per branch","title":"Keep them simple","body":"Write each test clearly, from most specific to least. Use else to catch everything left."},{"tag":"2 · Choose test data","title":"Normal, boundary, extreme","body":"For a pass mark of 50, test 30, 49, 50, 51 and 100. Write the expected result before running."},{"tag":"3 · Use the right operators","title":"Six comparisons","body":"== equal, != not equal, < less, <= less or equal, > greater, >= greater or equal."}]},{"type":"bars","id":"ops","eyebrow":"Data","title":"Comparison operators","intro":"Each comparison gives True or False. Here with x = 7:","items":[{"label":"x == 7","value":100,"display":"True","color":"#1f9d55"},{"label":"x != 7","value":30,"display":"False","color":"#e0463e"},{"label":"x < 10","value":100,"display":"True","color":"#1f9d55"},{"label":"x >= 8","value":30,"display":"False","color":"#e0463e"},{"label":"x > 5 and x < 10","value":100,"display":"True","color":"#1f9d55"},{"label":"not (x > 5)","value":30,"display":"False","color":"#e0463e"}],"facts":[{"title":"Comparing strings","body":"\"apple\" < \"banana\" is True: strings compare letter by letter using character codes. But \"Zebra\" < \"apple\" is also True, because capitals have smaller codes than small letters."},{"title":"Python allows chains","body":"In Python you can write 12 <= age < 18. Most languages need age >= 12 and age < 18."}]},{"type":"cards","id":"deeper","eyebrow":"Go deeper","title":"More than the lesson","items":[{"tag":"History","title":"George Boole's logic","body":"In 1854 George Boole wrote a book turning logic into algebra with just True and False. Nearly a century later his ideas became the basis of computer circuits, and every if statement uses Boolean logic."},{"tag":"Real world","title":"Decisions everywhere","body":"Thermostats (if temperature < 19 turn heating on), games (if health <= 0 game over), banking apps (if balance < amount refuse payment) and recommendation systems all run on selection."},{"tag":"Careers","title":"Who writes decision logic?","body":"Game developers, app developers, automation engineers and data scientists building decision trees all spend their days writing and testing conditions."},{"tag":"Coming next","title":"Where this leads","body":"Selection plus repetition (loops) and sequence are the three building blocks of every algorithm. Loops come next."}]},{"type":"cards","id":"challenge","eyebrow":"Challenge yourself","title":"Stretch your brain","intro":"Try each one before opening the card.","items":[{"tag":"Challenge","title":"Write code that decides if a year is a leap year","body":"A leap year divides by 4, except century years, which must divide by 400.\n\nyear = int(input())\nif year % 400 == 0:\n    print(\"Leap\")\nelif year % 100 == 0:\n    print(\"Not leap\")\nelif year % 4 == 0:\n    print(\"Leap\")\nelse:\n    print(\"Not leap\")\n\n2024 leap, 1900 not, 2000 leap.","code":true},{"tag":"Challenge","title":"Rewrite without 'and': if a > 0 and b > 0: print(\"both\")","body":"Use a nested if:\n\nif a > 0:\n    if b > 0:\n        print(\"both\")\n\nThe inner test only runs if the outer one is True, which is exactly how 'and' behaves.","code":true}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Code decisions","items":[{"title":"Quiz machine","time":"25 minutes","need":["Python or an online editor"],"steps":["Ask three questions with input().","Use if to check each answer and add 1 to a score variable when correct.","At the end use if/elif/else to print a message for 3, 2, or 0–1 correct.","Test it with a friend."],"why":"Combines input, variables and selection. Comparing strings needs exact matches, so try .lower() to accept 'Paris' and 'paris'.","safety":"No safety risks."},{"title":"Decision flowchart at home","time":"15 minutes","need":["Paper and pens"],"steps":["Choose a daily decision, like what to wear for the weather.","Draw it as a flowchart with diamonds for Yes/No questions.","Translate it into if/elif/else code on paper.","Check every path by imagining different weather."],"why":"Flowchart decisions and if statements are two ways of writing the same selection logic.","safety":"No safety risks."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Eight more questions","questions":[{"question":"Which operator tests if two values are equal?","options":["==","=","!="],"answer":0,"right":"Correct. = stores; == compares.","wrong":"Not quite. One of these stores a value, one compares."},{"question":"age = 12. What is age >= 12?","options":["False","True","12"],"answer":1,"right":"Correct. 12 is equal to 12, so >= is True.","wrong":"Not quite. >= means greater than OR equal to."},{"question":"What does elif mean?","options":["end if","else loop if","else if"],"answer":2,"right":"Correct. It is checked only if the previous conditions were False.","wrong":"Not quite. It is short for two words."},{"question":"a = True, b = False. What is a or b?","options":["True","False","Error"],"answer":0,"right":"Correct. or needs at least one True.","wrong":"Not quite. or is True if either side is True."},{"question":"What is not (5 > 3)?","options":["True","False","5"],"answer":1,"right":"Correct. 5 > 3 is True, and not flips it.","wrong":"Not quite. Work out the bracket, then flip it."},{"question":"Which test data is a BOUNDARY test for 'age must be 13 to 19'?","options":["16","25","13"],"answer":2,"right":"Correct. 13 is the edge of the allowed range.","wrong":"Not quite. Boundary data sits exactly at the edge."},{"question":"temp = 25. if temp > 30: A  elif temp > 20: B  elif temp > 10: C. What runs?","options":["B and C","B","C"],"answer":1,"right":"Correct. Only the first True branch runs.","wrong":"Not quite. Python stops after the first True condition."},{"question":"Why must every if, elif and else line end with a colon?","options":["Python syntax requires it to start a block","It makes the code faster","It is optional"],"answer":0,"right":"Correct. The colon starts the indented block.","wrong":"Not quite. Leaving it out gives a syntax error."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can…","items":["explain what selection is and why programs need it","write if, if/else and if/elif/else in Python","use the six comparison operators correctly","explain why the order of elif conditions matters","combine conditions with and, or and not","use a truth table for and and or","write selection in pseudocode with IF … ENDIF and CASE","choose normal, boundary and extreme test data for a condition"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"selection","meaning":"Choosing which code runs, depending on a condition."},{"word":"condition","meaning":"An expression that is either True or False."},{"word":"branch","meaning":"One of the paths a program can take."},{"word":"comparison operator","meaning":"==, !=, <, <=, >, >=."},{"word":"Boolean operator","meaning":"and, or, not: combine or flip conditions."},{"word":"nested selection","meaning":"An if inside another if."},{"word":"indentation","meaning":"Spaces at the start of a line that show which block it belongs to."},{"word":"boundary data","meaning":"Test values at the edge of what is allowed."},{"word":"CASE statement","meaning":"Pseudocode for choosing between many fixed values."},{"word":"truth table","meaning":"A table showing the result for every combination of True and False."}]},{"type":"sources","id":"sources","title":"Sources","items":["Python documentation: if statements; match statements","BBC Bitesize, GCSE Computer Science: Selection","MacTutor History of Mathematics: George Boole","Isaac Computer Science: Selection and Boolean logic"]}]},"overlay":null}