Link Search Menu Expand Document

Calculations with Numbers

Calculations as such are done with same operators in code as they are in mathematics: addition with +, substraction with -, multiplication with * and division with /. The order of calculation is also traditional. From left to right taking into account any brackets and multiplication and division before addition or substraction.

int first = 2;
Console.WriteLine(first); // prints 2
int second = 4;
Console.WriteLine(second); // prints 4

int sum = first + second; // variable "sum" is set with the value of the sum from "first" and "second"
Console.WriteLine(sum); // prints 6

Calcuation Order with Brackets

You can change the order of calculation with the round brackets.

int calcWithBrackets = (1 + 1) + 3 * (2 + 5);
Console.WriteLine(calcWithBrackets); // prints 23

int calcWithoutBrackets = 1 + 1 + 3 * 2 + 5;
Console.WriteLine(calcWithoutBrackets); // prints 13

Statement and Expression

Expression is a combination of values that is changed into a value during a calculation or evaluation.

int calcWithoutBrackets = 1 + 1 + 3 * 2 + 5;

This contains an expression of ‘1 + 1 + 3 * 2 + 5’ which is evaluated before it is assigned to the variable. The evaluation happens always before the assignment so the calculation is done before the value is set to the variable.

The evaluation is done in the same order as the rest of the code, line by line, left to right. The evaluation can also be done during a print if the expression is used as a parameter for printing.

int first = 42;
int second = 2;

Console.WriteLine(first + second); 
// The expression "first + second" is evaluated when this line is called. 
// Prints "44".

An expression does not change the value of a variable, unless the result of the expression is set as a value or given as a parameter to a method such as printing.

int first = 42;
int second = 2;

first + second; 
// Does not work because the expression is not assigned to a variable or used as a parameter.

Calculations and Printing

SNow let’s combine the basics of printing and calculations. You can print the value of a variable with Console.WriteLine(). If you want to concatenate the value to a string it is done with + and having the string inside quotation marks.

int truth = 42;

Console.WriteLine("The magic number is " + truth + "."); 
The magic number is 42.

In this example we combined a string with the value of our variable and another string. Notice that the first string ends in whitespace, so there is space between the word “is” and the value. The dot in the end does not have whitespace, so it is right next to the number.

Console.WriteLine("The magic number is " + 42); 
Console.WriteLine(42 + " is the magic number."); 
The magic number is 42.
42 is the magic number.

If one side of the addition is a string the other side is also changed to a string when the program is run. In the next example the integer changes into a string and is combined with a string. In the upper case we demonstrate again the calculation order and brackets and how they change the case.

Console.WriteLine("Four: " + (2 + 2));
Console.WriteLine("But! Twenty-two: " + 2 + 2);
Four: 4
But! Twenty-two: 22

With all this information we can create an expression that contains text and a variable which is evaluated during printing.

int x = 10;

Console.WriteLine("value of x is: " + x);

int y = 5;
int z = 6;

Console.WriteLine("y is " + y + " and z is " + z);
value of x is 10
y is 5 and z is 6

Multiplication and Division

So far we have only done very simple additions. Multiplication is also quite simple from mathematical point of view. It is just a special case of addition. For example 3*2 is the same as 2+2+2. In code it could be something like this.

Console.WriteLine(3*2);
Console.WriteLine(2+2+2);
6
6

Let’s not go deeper than that into the multiplication. Division is more interesting.

When dividing in code the type of the variables that are divided determines the type of the answer. For example if you divide integers the result will be an integer.

For the operands of integer types the result of the / operator is of an integer type and equals the quotient of the two operands rounded towards zero.

Console.WriteLine(13 / 5);    // output: 2
Console.WriteLine(-13 / 5);   // output: -2
Console.WriteLine(13 / -5);   // output: -2
Console.WriteLine(-13 / -5);  // output: 2

If we change one (or both) of the numbers to be double the result will also be a double.

Console.WriteLine(13 / 5.0);       // output: 2.6
int x = 13;
int y = 5;
Console.WriteLine((double)x / y);  // output: 2.6

To calculate an average of values we could do something like this.

int first = 13;
int second = 6;
int third = 42;
double average = (first + second + third) / 3.0; //divide by the amount of numbers
Console.WriteLine(average);  // prints 20.333333333333332

Notice that the divider for the average is 3.0 which is a double. Even though we declare average as a double if all the operands are integers the value for average would evaluate as 20.

Common Misconceptions with Variables

When a computer runs source code the code is run one command at a time progressing exactly as the code says. When a value is set to a variable the same event happens every time: The right side of the equals sign is copied as the value of the variable on the left. The most common misconceptions.

  • The value is thought to be moved instead of copied.
    first = second;
    

    You might think that ‘second’ does not have a value any more and the value has been transferred to ‘first’. This is incorrect for when the ‘first = second’ is evaluated the value that variable ‘second’ refers to is copied as the value for ‘first’.

  • Seeing the variable declaration as a dependency rather than copying.
    first = second;
    

    One might think that any change into ‘second’ affects ‘first’ as well. That is not correct. Once the line of code has been run there is no affection between first and second. The copying of value is done once and only once.

  • Third and most common misconception is the order of copying. Often in the beginning of coding it easy to mix up the direction on assignment. This doesn’t work.
    42 = int truth;
    42 = 21;
    

Row by Row Example

To understand what is happening in the code for variables you might want to pick up pen and paper. Write down what happens in the code line by line.

row 1: int first = (1 + 1);
row 2: int second = first + 3 * (2 + 5);
row 3:
row 4: first = 5;
row 5:
row 6: int third = first + second;
row 7: Console.WriteLine(first);
row 8: Console.WriteLine(second);
row 9: Console.WriteLine(third);

In the next console you can see what happens row by row. The program does not print all that is explained.

row 1: create variable first 
row 1: evaluate the calculation 1 + 1 and assign the value to the variable first
row 1: the value of variable first is now 2  
row 2: create variable second  
row 2: calculate 2 + 5 -> 7  
row 2: calculate 3 * 7 -> 21  
row 2: calculate first + 21  
row 2: copy the value of the variable first into the calculation
row 2: calculate 2 + 21 -> 23  
row 2: assign the value for variable second as 23  
row 2: the value of variable second is now 23  
row 3: empty, do nothing  
row 4: assign the value for variable first as 5  
row 4: the value of variable first is now 5  
row 5: empty, do nothing  
row 6: create variable third  
row 6: calculate first + second  
row 6: copy the value for variable first into the calculation
row 6: calculate 5 + second  
row 6: copy the value for variable second into the calculation
row 6: calculate 5 + 23 -> 28  
row 6: assign the value for variable third as 28  
row 6: the value of variable third is now 28  
row 7: print variable first  
row 7: copy the value for variable first arvo to be printed 
row 7: print 5  
row 8: print variable second  
row 8: copy the value for variable second arvo to be printed
row 8: print 23  
row 9: print variable third  
row 9: assign the value for variable third arvo to be printed 
row 9: print 28  

You can now do the exercises for Calculations.


Copyright © 2021 Markus Räisänen for Helsinki Business College. More information about the licensing.