Selection Is How a Program Makes a ChoiceSelection means the program picks one path out of several, based on a condition that is either true or false.An if runs its block only when the condition is true and skips straight past it otherwise.An else catches everything the if missed, so exactly one of the two blocks always runs.An else if, written elif in Python, tests a second condition only when the first one turned out false.A chain is tested from the top and stops at the first true condition, so nothing below a matching branch ever gets a look in.ExamplePython: if temp > 28: print("Fan on")elif temp < 16: print("Heater on")else: print("Comfortable")Order and Nesting Decide What Actually RunsPut the narrowest condition first, because a chain that checks score > 50 before score > 90 never reaches the 90 branch at all.A nested condition is an if sitting inside another if, for when the second question only makes sense if the first was true.A door lock asks "is a card present" and only then asks "is this card allowed", since asking the second one on its own is meaningless.Nesting more than three deep usually means the logic should be split into functions or joined with and.In Python the nesting is the indentation, so a block indented one step too far belongs to the wrong if and runs at the wrong time.Common MistakeAn if with no else does nothing at all when the condition is false, which on a screen looks exactly like the program being broken.When every case must produce something, finish the chain with an else that says so.Iteration Repeats Without You Repeating YourselfDefinitionIterationRepeating a block of instructions, either a set number of times or until a condition is met.Iteration is repeating a block of instructions rather than typing it out again.Reading a sensor 100 times takes one loop of three lines instead of 300 lines you would have to fix 300 times.Every loop has three parts: a starting state, a condition saying whether to carry on, and something inside that changes the state.Leave out the third part and the loop never ends, which is the fault behind most frozen micro:bit programs.Loops are what turn a one shot script into a device that keeps working the whole time it is switched on.While Loops Run Until Something ChangesA while loop checks its condition, runs the block, then checks again.Use it when you do not know how many repeats you need, such as keeping a pump on while the tank is below the fill line.Because the check comes first, a while loop can run zero times, which is what you want for anything acting as a safety check.Something inside the block has to push the condition towards false, whether that is a counter going up or the sensor being read again.Python: while moisture < 400: pump_on() then moisture = read_sensor() on the next indented line.Forgetting that second indented line is why a pump that should stop at 400 keeps running until the tray overflows.For Loops Run a Known Number of TimesA for loop repeats a set number of times, or runs once for each item in a list.Python's range(5) counts 0, 1, 2, 3 and 4, so the loop runs five times and stops before it reaches 5.The loop counter is the variable holding where you have got to, and you can use its value inside the loop.So for i in range(5): print(i * 2) prints 0, 2, 4, 6 and 8.Use for when the count is known before the loop starts and while when it depends on what happens during it.MakeCode calls these the "repeat 4 times" and "while" blocks, and they behave exactly the same way.TipCounting from 0 trips up everyone at first, so remember range(5) hands you five numbers ending at 4.For the numbers 1 to 5, write range(1, 6), because the second number is the one it stops before.Infinite Loops: Useful on Purpose, Fatal by AccidentAn infinite loop is one whose condition never becomes false.On a microcontroller, while True is deliberate, because the device should keep checking its sensors for as long as it has power.An accidental one happens when you forget to change the variable the condition depends on.Another common cause is changing the wrong variable, such as adding one to count while the condition is testing total.To find one, print the condition's variable inside the loop and watch whether the number moves.On a micro:bit, a frozen display with no error message is nearly always an infinite loop rather than a crash.Choosing Between Selection and IterationSelection asks a question once and moves on, while iteration asks the same question again and again."If the button is pressed, buzz" is selection, and "keep buzzing while the button is held" is iteration.Real programs nest the two, usually an if sitting inside a loop that runs forever.A packing checklist that runs through five items is iteration with a selection inside it for each item.Writing the same if three times with different values is a loop asking to be written instead.Active recallWhy does an if chain never reach a branch that sits below a condition already true?Name the three parts every loop needs.When would you choose a while loop over a for loop?How many times does range(5) run, and what is the last value of the counter?Give one infinite loop that is deliberate and one that is a bug.