Contents

Variables and Operations

Now you know how to make a C# program accept inputs and produce outputs, let’s make it do something more complicated.

Back to school

Computers are all about data. They store data, they read data, and they can produce data. However, raw data by itself doesn’t necessarily have meaning - the interpretation of data is what gives a piece of data it’s meaning. Let me give an example…

In a classroom there is written on a blackboard a number:

65

The teacher asks everyone in the class ‘What is the value of this number?’

‘Simple!’ You think. You raise your hand and shout ‘Sixty-Five!’

The teacher smirks, and says ‘Correct… Now can anyone else tell me the value of this number?’

‘But surely ‘65’ has no other value?’ You think to yourself. As you mull this over, Charlie raises his hand and says ‘A, Uppercase A to be precise’.

‘Correct!’

How can this be? Suddenly Freya pipes up and says ‘Sixty-Five-Point-Zero’!

To add to the confusion, Sam then says ‘Miss, I can’t read what’s on the board…’

‘Oh, of course Sam’. The teacher then surrounds the number in double quotes, to read:

"65"

‘Ah!, Now I can read it!’ Sam says, ‘Sixty-Five!’

‘Correct! All correct!’ The teacher exclaims excitedly.

How chaotic! All the students look at the same number, 65, and think it has different values. What is going on here?

Well what’s happening is that each student is interpreting the data in a different way. In a sense, they’re all correct. They simply have different, yet equally valid, ways of interpreting the same data.

For instance, why did Charlie give ‘Uppercase A’ as his answer? Well, Charlie is interpreting the data as an alphabet character using something called ‘ASCII encoding’. ASCII encoding works by associating a number with an alphabet character, number, or symbol. In ASCII encoding, 65 means ‘A’, 66 means ‘B’, and so on. Therefore Charlie was correct to interpret ‘65’ to mean ‘A’.

Freya interpreted the data using something called ‘floating point’. She considers all numbers to have fractional values, even whole numbers. So ‘65’ becomes ‘65.0’ in her mind.

Finally, Sam struggled to read the board at first. This is because Sam can only cope with words, not numbers. When the teacher put double quotes around 65, Sam could interpret it as a word with no numerical value. Sam needed the double quotes because he can only work with words, not numbers.

If you asked Sam to do simple maths question, like 65 - 1 - it would be the same as asking him to do Ham - Jam - it’s not a concept that makes sense in his head. Despite Sam’s peculiar way of thinking about numbers, he seemed to give the same answer as you.

So what’s the point of this analogy? Well, it’s that in computer systems, the interpretation of data is just as important as the data itself. Each student in the class represents a way of interpreting the data - known generally as a ‘type’.

Freya’s type is a ‘Floating Point’ - which means a number with a decimal point value. Charlie’s type is ‘Character’, which means an alphabet character, number, or symbol. Sam’s type is ‘String’, which means a number of Characters strung together - a word or sentence.

You’re probably thinking, ‘What a frustratingly odd set of students’. Well, odd as they are, these are going to be your peers throughout your career as a computer programmer, so you’d best get used to them!

Variables

In programming languages, a common way of storing data is with a variable. Variables are used to store information under a unique name They’re among the most fundamental ways of storing information in a computer system.

In C# a variable needs a name and a type in order to work. The name must be unique, and the type says how the data must be interpreted. A C# variable can be created, or ‘declared’ like so:

string userName;

First you state the type, in this case string. Next you provide the name, in this case userName.

In C#, like most programming languages, variables names cannot have spaces. That’s why we write userName instead of user name.

userName can then be assigned a value, like so:

userName = "Mikiel Agutu";

Assignment occurs from right to left. One way to think of it is that the information on the right moves into the name on the left. So "Mikiel Agutu" is stored under the name userName.

When the code is run, userName will be replaced with "Mikiel Agutu". So if I wrote:

Console.WriteLine(userName);

When I ran the program, the output would be

Mikiel Agutu

Types

A type tells you how a piece of information can be used in a computer system. As I said before, in C# all variables must have a type.

There are many types in C#, but the most commonly used are:

  • int integer - a whole number
  • char character - a single letter, word, or symbol
  • string a ‘string’ of characters - a word or a phrase
  • double a fractional number - with a decimal point
  • bool a type that must be either true or false

Don’t worry about learning these all now - you’ll see how they work more clearly as you use them for yourself in the Katas.

Operations on variables

Variables can be manipulated through the use of operators. There are many operators in C#, but the most commonly used are addition, subtraction, multiplication, and division. These are represented by the symbols: +, -, *, /.

The behavior of operators depends on the types of the data involved. For instance, if I use the integer type - int I can do something like this:

int x = 5;
x = x + 5;

What will the value of x be? Well on the first line I declare x as an int. I also assign it a value of 5 on the same line.

On the next line, I say x is now equal to the value of x + 5. That might be a little confusing to understand at first. Break it down:

Code Explanation
x x starts off being equal to 5
x = Make x equal to the thing on the right…
x = x Make x equal to x - which is 5
x = x + Make x equal to x (still 5) - plus something…
x = x + 5 Make x equal to x (5) plus 5

To help you reason further, you can then substitute in the values:

Code Explanation
x = 5 + 5 Substitute in the value of x
x = 10 Make x equal to 10!

However if I used the string type, things might look a bit different:

string x = "5";
x = x + "5";

What happens now? Well first notice how I had to use double quotes to assign a value to x. This is because x is a string and can’t cope with numbers - it considers everything to be a word.

What happens on line two? Well let’s break it down again:

Code Explanation
x x starts off being equal to "5"
x = Make x equal to the thing on the right…
x = x Make x equal to x - which is "5"
x = x + Make x equal to x (still "5") - plus something…
x = x + "5" Make x equal to x ("5") plus "5"
x = "5" + "5" Substitute in the values - make x equal to "5" plus "5"
x = "10" Make x equal to "10"?

Actually, x doesn’t become "10" - it actually becomes "55"! Why? Let’s look at the last few lines again:

Code Explanation
x = "5" + "5" Substitute in the values - make x equal to "5" plus "5"
x = "55" Make x equal to "55"

With strings, the + operator doesn’t do anything mathematical. Instead, it sticks one string onto another (known as ‘appending’ the strings). With the data of the string type, the + operator behaves very differently compared to what it does used with an int.

Kata 4 will provide a practical explanation of how to use variables and manipulate them with operators. If your head is still spinning trying to understand the whole ‘classroom’ analogy, Kata 4 should start clear things up for you.

Copyright Mikiel Agutu 2019