• 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.

Three small programs
ProblemFormula usedSteps
Simple interestSI = P x R x T / 100READ P, R, T; SI = P * R * T / 100; PRINT SI
Celsius to FahrenheitF = C x 9 / 5 + 32READ C; F = C * 9 / 5 + 32; PRINT F
Area of a circleA = 3.14 x r x rREAD 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. 1. In the statement total = a + b, which variable receives the value?

    1. a
    2. b
    3. None of them
    4. total
    Show answer

    Answer: D. The variable on the left of the assignment sign, total, stores the result.

  2. 2. If x = 4 and the statement x = x + 3 runs, what is x afterwards?

    1. 4
    2. 7
    3. 12
    4. 3
    Show answer

    Answer: B. The new value is the old value plus 3: 4 + 3 = 7.

  3. 3. A program reads P = 1000, R = 4 and T = 2 and computes P R T / 100. What does it print?

    1. 80
    2. 8
    3. 800
    4. 40
    Show answer

    Answer: A. 1000 x 4 x 2 / 100 = 8000 / 100 = 80.

  4. 4. To exchange the values of two variables you normally need

    1. A spare variable
    2. An extra loop
    3. A long array
    4. A new function
    Show answer

    Answer: A. A temporary third variable holds one value while the other is copied.

  5. 5. What is the order of the basic steps in a simple program?

    1. Output, process, input
    2. Process, output, input
    3. Input, process, output
    4. Input, output, process
    Show answer

    Answer: C. A simple program reads input, processes it and then gives output.

Privacy · Terms · Refunds · Delivery