4-Function Calculator

Input Value

Is your first line (after the greeting) an input or output? If it is input, how does the user know what to type in?

Let's assume your first two lines (after the original greeting) tell the user what to type, and then accept that input. What does your program do with whatever they typed? Did you tell them to type in a single letter command? If they typed in "A" because they want to add, what exactly will your program add to what?

Most arithmetic takes two numbers and combines them in a specific way to product a single result. Where are these two numbers in your program? A variable is a place in the computer where a single value is remembered. The computer has no way to remember anything at all, except in variables. The value can be a number or a letter or a whole word, or even a combination of values that go together, but it has a name, and when your program uses that name, it is to do something with the value stored in that variable, or else to put a new or different value there. The variable name is a single word containing letters and maybe digits, but starting with a letter. Some computer languages consider capital and lower-case letters different, but in English capitals are used only at the beginning of sentences and proper names (and for SHOUTING), but otherwise have no significance.

So let's change your input to name a variable as where whatever the user types in goes:

Input operation
"Input" is the command that tells the computer (or a person reading your program) what to do, and "operation" is the name I chose for a variable that when the user types something in, it goes into that variable. You can name your variables anything you like, but I usually try to give it a name that reflects what value I think it will hold, in this case whatever operation, (A)dd or (S)ubtract or whatever, the user wants your program to do.

Let's review what the program does so far. The user turns the caluclator on ("Run This Program") and your program displays a zero. Why zero? What does that zero mean? In a real calculator, that zero is just the current result, which is zero only because it got initialized when you (the user) turned it on, but you haven't done anything to change it yet. Maybe we should do the same thing.

When the store-bought calculator adds two numbers, one of them is the previous result. What do you think you need to do in your program to keep a result to display and add to? In plain English (the language we speak) we might say,

result is a variable
but in Tom's Kitchen computer, each line must begin with a distinctive word that tells the computer what to do. If we put "result" first, the computer cannot know what we mean until it gets to the end of the line. This is not very good English, but much better for the computer:
variable result
Besides, in this form we can give it an initial value -- and then it also reads better as English:
variable result = 0

Then (still part of the turn-on sequence) we can replace the original output line with

variable result = 0
Show "Current result: " result

What's next? If the user wants to (A)dd, where are the numbers they want your program to add? Think about that, then add some lines to your program to deal with those two numbers. Then turn the page.

Five Basic Concepts
Sequence
Iteration
Conditional
Variables
Input/Output

 

<<Previous | ToC | Next >>

[2021 May 12]