All free lessons / Computing / Programming II: selection, variables and inputs and outputs
Computing · FSCE
Programming II: selection, variables and inputs and outputs
A free preview from the LearnAhead Learning Library — the same lesson registered students learn from.
In this lesson: Use selection (if...then...else) to make programs branch, variables to store changing values, and inputs and outputs to connect a program to its user and the world.
The idea
Sequence and loops give a program a fixed path. Selection lets it choose. An if...then...else instruction tests a condition — is the score greater than 10? is the light sensor dark? — and runs one block if the answer is true, another if it is false. Games use selection constantly: if the sprite touches the edge, then bounce; else keep moving.
A variable is a named store whose value can change while the program runs — score, lives, speed, timer. When we say 'set score to score + 1', the variable keeps the new total. Tracing a program with variables means keeping a running table of each variable's value after every line; the final value is whatever the last change left there.
Inputs bring information in — a key press, a mouse click, a button, a microphone, a temperature or light sensor. Outputs send information or action out — screen displays, sounds, lights, motors. A program that waters plants when the soil sensor reads dry uses all three ideas together: input (sensor), selection (if dry), variable (watering count), output (pump motor). FSCE questions present little systems like this and ask you to predict, complete or debug them.
Worked example
A program runs: set points to 0. Repeat 3 times: if the player catches the star, set points to points + 2; else set points to points + 1. The player catches the star twice and misses once. What is the final value of points? A: 3. B: 4. C: 5. D: 6.
- Track the variable: start 0. Catch 1: +2 = 2. Catch 2: +2 = 4. Miss: +1 = 5.
- A counts only the catches as 1 each; B forgets the miss adds 1; D adds 2 for the miss as well.
Answer: C — points ends at 5 (2 + 2 + 1).
Selection: programs that decide
Every if statement splits the program into two possible paths, and the condition decides which one runs. Conditions compare values: equals, greater than, less than, or test the world through sensors. A common design is the traffic-light rule: if the light is green, then go; else stop. Nesting — an if inside an if — handles finer choices, but each added branch doubles the paths a tester must check, so programmers keep selections as simple as the job allows.
Worked example
A greenhouse controller runs: if temperature > 28, then open the window; else if temperature < 15, then switch on the heater; else do nothing. The temperature is 12. What happens? A: The window opens. B: The heater switches on. C: Nothing happens. D: Both the window opens and the heater switches on.
- Test in order: 12 > 28 is false, so no window. 12 < 15 is true, so the heater switches on — B.
- C would need both conditions false; D is impossible because the branches are alternatives.
Answer: B — the second condition is true, so the heater switches on.
Inputs and outputs in real systems
Naming the input and output is the first step in designing any system. A washing machine takes inputs from dials, buttons and water-level sensors, and produces outputs — motor movement, heated water, beeps. A floor robot takes inputs from buttons and bump sensors, and outputs movement and lights. If you cannot say what goes in and what comes out, you cannot yet write the program in the middle.
Worked example
A night-light system switches a lamp on when it gets dark and a person is detected in the room. Which list correctly names its parts? A: Input: lamp. Output: darkness. B: Inputs: light sensor and movement sensor. Output: the lamp switching on. C: Input: the person. Output: the room. D: There are no inputs because it is automatic.
- Sensors gather information (inputs); the lamp acting is the output — B.
- A reverses them; C names objects, not information or actions; D is wrong because automatic systems still take inputs — from sensors instead of people.
Answer: B — light and movement sensors are the inputs; the lamp is the output.
Watch out
- Losing track of variables — keep a written table and update it line by line.
- Reading 'set points to points + 1' as maths that cannot work — in programming it means 'increase the stored value by 1'.
- Testing only the true branch of an if — check what happens when the condition is false, too.
- Confusing inputs with outputs — sensors and buttons are inputs; screens, sounds, lights and motors are outputs.
Quick check
Try these on your own first — then reveal each answer.
1. What is a variable?
2. In 'if score > 10 then show the trophy', what is the condition, and when does the trophy appear?
3. Give one input and one output for a simple games controller.
Was this lesson helpful?
Practise this concept
Reading is the first step — the concept sticks when your child answers questions on it. Registered students practise graded questions for this lesson, with instant marking and worked explanations, and the lesson is marked complete in their progress.