Five Minute C# — Lesson 3

Handling dynamic data by updating variables

Alexander Laurence
Five Minute C#
4 min readOct 28, 2018

--

Lessons: 12345678910 ・ 11 ・ 12 ・ 13 ・ 14

What else can we do?

In our previous lesson, we touched upon making use of strings, ints, and floats through concatenation and basic arithmetic operators such as +, -, *, and /. Let’s take this a step further!

As we already know, we can place a variable inside a variable such as

int a = 1;
int b = a;

Therefore, b will equal to 1 in this example. But what if we put the same variable inside itself?

int a = a;

While this is perfectly fine, it doesn’t really do anything… It only begins to get interesting when we start to play around with mathematical operators.

int a = 1;
a = a + 3;

In this example, a is adding 3 to itself (i.e. incrementing by 3). If we tell the console to print a, it will give us:

4

Alternatively, we can express this exact same code using a shorthand method… (but it’s up to you which way you want to use).

int a = 1;
a += 3;

This works exactly in the same way as the code above it. The integer a is being incremented by a value of 3.

What about subtraction? That’s simple…

int a = 1;
a -= 3;

All the other mathematical operators we learnt work in the same way. For multiplication:

int a = 1;
a *= 3;

And for division:

int a = 1;
a /= 3;

Just remember, when dividing you may get decimal answers and so you probably want your variable to be a float type rather than an integer.

Just as a bit of side note as we are using the / operator, becareful not to include a double slash //. In C# a double slash tells the compiler to ignore this part of the code. It is often used for comments and notes!

//this is my code
int a = 1;
//a is equal to a divided by 3
a /= 3;

Let’s do a real world exercise!

int number_apples = 10;
int apples_eaten = 2;

And therefore, we can say the number of apples is the number of apples minus the ones that I’ve eaten.

number_apples = number_apples - apples_eaten;

Which can be simplified to

number_apples -= apples_eaten;

Makes sense right? There’s one more thing before we end this lesson…

Let’s take a look at the remainder operator! It can be quite useful. Mathematicians like to call this ‘modulus’ .

Let’s consider two integers, number1 and number2.

int number1 = 20;
int number2 = 4;

And let’s apply the remainder operator to both of those integers and store that into a separate integer, called leftOver. We can print the result to the console window:

int leftOver = number1 % number2;
Console.WriteLine(leftOver);

Which will give us:

0

Before I go over that result, let’s compare it with another result. For this, let’s change the values a bit…

int number1 = 22;
int number2 = 4;
int leftOver = number1 % number2;
Console.WriteLine(leftOver);

This will instead give us:

2

I hope you can begin to see what is going on. We are dividing number1 by number2, and whatever is being leftover (i.e. the remainder) is returned as an integer value. This is quite useful for figuring out odd/even numbers.

int number = 15;
int
oddNumberCheck = number % 2;
Console.WriteLine(oddNumberCheck);

We know that 15 is an odd number because when we divide 15 by 2 we are left with a remainder of 1.

1

This is just an example, so if you don’t immediately understand this, don’t worry!

So by virtue of the remainder operator, we can figure out if numbers are divisible or not.

int number1 = 133;
int number2 = 7;
int divisibleCheck = number1 % number2;
Console.WriteLine(divisibleCheck);

Since 133 divided by 7 has no remainder, the two numbers are indeed divisible. Here’s the output:

0

Whereas, 134 divided into 7 is not divisible.

int number1 = 134;
int number2 = 7;
int divisibleCheck = number1 % number2;
Console.WriteLine(divisibleCheck);

Since the output isn’t 0:

1

Let’s take a break before moving on to the next lesson!

--

--

Alexander Laurence
Five Minute C#

Alex graduated from the University of Edinburgh with a Masters degree in Neuroscience. He spent 3 years teaching and now runs his own tech venture.