Question

I'm going through an introductory programming book and it lists a simple example in pseudocode:

Start
  input myNumber
  set myAnswer = myNumber * 2
  output myAnswer
Stop

Why can't we omit creating another variable called myAnswer and just put the operation into the output command, like this:

Start
  input myNumber
  output myNumber * 2
Stop

Why is the former correct and latter not?

Was it helpful?

Solution

You could but the other is so you see what is going on and so you can use myAnswer later in the program. If you use your second one, you cannot reuse myAnswer.

So later down in the program you might want:

myAnswer + 5
myAnswer + 1
etc.

You might have different operations you want to use it for.

Consider swapping numbers:

Start
  input myNumber
  set myAnswerA = myNumber * 2
  output myAnswerA
  set myAnswerB = myNumber * 3
  output myAnswerB
  set temp = myAnswerA
  set myAnswerA = myAnswerB
  set myAnswerA = temp
  output myAnswerA
  output myAnswerB
Stop

That would be difficult without variables. Computer books start real basic, and most programming is easy until you see complexity. Most everything is trivial in tutorials, and it is only in complexity do you see where things do or do not make sense.

OTHER TIPS

Another reason, The assignment set myAnswer = myNumber * 2 gives the resulting value a name. A person reading the two line version of your code only knows that it prints out the value of myNumber * 2. A person reading the three line version can see that myNumber * 2 is the answer.

It may not seem important in such a trivial example, but sometimes, assigning a result value to a variable with a meaningful name can make it much easier for other programmers to read and understand your code.

That's pseudocode. It's not supossed to be any particular implemented language.

Some programming languages don't support evaluating an expression and then outoputting the result in the same line of code. For example most assemblers do not support that. Perhaps the author of the book wanted to show things in a low-level fashion.

Other answers have addressed the specific mechanic details and examples of when one or the other form would be better, but I want to mention a little further background, sort of philosophical:

You're learning a language.

A language is something in which ideas can be expressed and understood (communicated). A computer programming language has the additional property that it can be mechanically parsed by a machine designed to take action (execute) based on ideas (decisions) that are specified and fed in using that language.

In ANY language that's at all useful, there is more than one way to express nearly any idea expressible in that language.


Consider the wide variety of nuance available in the English language. Even a simple sentence, such as

The cat jumped onto the box.

can be varied to express slightly different ideas or place emphasis on different parts of the scene while referring to the same exact physical universe action.

First are grammatical variations:

The box was jumped onto by the cat.

Onto the box jumped the cat.

Then are wider and wider variations, still referring to the same physical action:

The box shook under the impact of the cat.

The cat came down with a thud upon the top of the box.

The feline leaped lightly into the air and landed neatly on a nearby box.

Just look at the implications of the word "nearby" in that last sentence. Its inclusion conveys a whole new range of concepts not otherwise present.


There is always more than one way to do it, Python Zen to the contrary.


Of course, there will be ONE way which perfectly expresses your intention and is most suitable, just as you would choose only ONE of the English sentences above depending on exactly what you wished to communicate. That's what the Zen of Python is about.

But in an introductory programming course or an introductory English course, you must first learn the various ways (wordings, code snippets) in which you can couch an idea before you will develop the judgment to choose which is most perfectly fitting.

You are asking only about the myAnswer variable that seems to be redundant. Other answers already explaind some of the why and when it would make sense to omit or use it but here's one more: how about this?

Start
  output input * 2
Stop

or even that

Start output input * 2 Stop

In most languages this would still work but can you read it? It's difficult so we often use helper variables because computers are not the only ones who read the code. We need to maintain it and understand it in few months and it's even harder to write code that you can still unterstand later then a working one... usually after only a few days you won't know why you did something in a specific way.

You can do both variants (in this simple case), but the first variant becomes more readable and structured for more complex cases. The first variant shows the IPO model with one line for each step (two of those already with the right name):

Start
  input myNumber                       // Input
  set myAnswer = myNumber * 2          // Process
  output myAnswer                      // Output
Stop
Licensed under: CC-BY-SA with attribution
scroll top