- Foundation
- About 12 min
- 5 practice questions
Input, output and assignment: writing your first complete programs
Write short programs that read values, calculate with them and print results, and swap two values using a third variable.
If something looks wrong, please tell us.
Almost every program follows the same simple pattern: take some input, process it and give output. Learning to write this pattern in plain steps is the base for everything else in programming. The examples use pseudocode, which is plain language that is not tied to any particular programming language.
The three basic statements
- Input: the program asks for a value and stores it in a variable. Written as READ x or INPUT x.
- Assignment: a value or the result of a calculation is stored in a variable. Written as total = a + b. The variable on the left receives the value on the right; it is not an equation to be solved.
- Output: the program shows a value or a message. Written as PRINT total.
These statements run one after another in the order written. This is called sequence. The order matters: you cannot print a value before you have stored it.
Assignment in detail
The statement x = x + 1 looks strange in mathematics but is ordinary in programming. It means: take the current value of x, add 1 and store the result back in x. If x was 4 before, it is 5 afterwards. A variable remembers only its latest value; the old value is lost unless it was saved in another variable.
| Problem | Formula used | Steps |
|---|---|---|
| Simple interest | SI = P x R x T / 100 | READ P, R, T; SI = P * R * T / 100; PRINT SI |
| Celsius to Fahrenheit | F = C x 9 / 5 + 32 | READ C; F = C * 9 / 5 + 32; PRINT F |
| Area of a circle | A = 3.14 x r x r | READ r; A = 3.14 * r * r; PRINT A |
Worked example 1: simple interest
The program reads P = 2000 (rupees), R = 5 (percent a year) and T = 3 (years). SI = 2000 x 5 x 3 / 100 = 30000 / 100 = 300 rupees. The numbers are illustrative; the program works for any values typed in.
Worked example 2: temperature
The program reads C = 25. F = 25 x 9 / 5 + 32. First 25 x 9 = 225, then 225 / 5 = 45, then 45 + 32 = 77. The output is 77 degrees Fahrenheit.
Worked example 3: swapping two values
Suppose a = 7 and b = 12 and we want to exchange them. Writing a = b and then b = a fails, because after a = b both variables hold 12 and the 7 is lost. The fix is a third variable, temp, to hold one value safely. Steps: temp = a (temp is 7); a = b (a is 12); b = temp (b is 7). Now a = 12 and b = 7.
A note on messages: a program that asks for input should first print a clear message, called a prompt, such as "Enter the marks". Without a prompt the user may not know that the program is waiting for something. Output should be labelled too, for example "Total = 350", rather than a bare number that the reader has to interpret.
Input, process, output; the variable on the left of = receives the value on the right; swapping needs a third variable.
Key takeaways
- A program usually reads input, calculates and prints output.
- In x = x + 1 the new value is based on the old value.
- Check the order of statements: store first, then use.
Check your understanding
-
1. In the statement total = a + b, which variable receives the value?
- a
- b
- None of them
- total
Show answer
Answer: D. The variable on the left of the assignment sign, total, stores the result.
-
2. If x = 4 and the statement x = x + 3 runs, what is x afterwards?
- 4
- 7
- 12
- 3
Show answer
Answer: B. The new value is the old value plus 3: 4 + 3 = 7.
-
3. A program reads P = 1000, R = 4 and T = 2 and computes P R T / 100. What does it print?
- 80
- 8
- 800
- 40
Show answer
Answer: A. 1000 x 4 x 2 / 100 = 8000 / 100 = 80.
-
4. To exchange the values of two variables you normally need
- A spare variable
- An extra loop
- A long array
- A new function
Show answer
Answer: A. A temporary third variable holds one value while the other is copied.
-
5. What is the order of the basic steps in a simple program?
- Output, process, input
- Process, output, input
- Input, process, output
- Input, output, process
Show answer
Answer: C. A simple program reads input, processes it and then gives output.
Sign in to keep track of the lessons you finish.