Search code examples
variableslanguage-agnostic

Real world examples of variables


I'm gonna teach kids computer programming. I'm gonna teach the children about variables.

I want to tell the subject through real life examples to better understand the variables, but there is no example in my mind.

How do you teach variables to a child?

Can you give examples by making real life similes for children to understand the subject of variables?


Solution

  • The simpiest example I can think of, is the calculation of the sum of all numbers from 1 to n, where n is a number the user can enter. It gives the following piece of pseudo-code:

    int input_number, counter, sum;
    string output_sum;
    
    show_on_screen("Enter a natural number: ");
    get_input(input_number);
    sum = 0;
    for (counter = 1 to input_number):
      sum = sum + counter;
    next counter;
    
    output_sum = convert_number_to_string(sum);
    show_on_screen("The sum equals " + output_sum);
    

    This example contains following items:

    • variables and their types (why the difference between integer and string)
    • using variables as placeholders
    • using meaningful names for variables
    • using variables as counters (for-loop, you can also use a while-loop, and explain the similarities, differences)

    Good luck