CategoryImplement program flow

Operators

So far we have covered few basic operators and now would be good time to expand our knowledge of them. All of the operators will be tested using if statements:

Certainly there are some more operators, but we will explain them as we go!

Value Types Part 1

In this post we will cover some basic value types that we use in our everyday work, such as int, double,and bool.
Before we begin with data types you should be familiar with variables. Variables are nothing more than name given to a memory that our programs can manipulate. Value types store values inside variables that are stored in side stack. Stack and heap will be covered in post where we will compare Value and Reference types. For now, all you need to know is that every value type variable has its value stored in its own place inside memory, and when you assign value from one value type to another you create copy of that variable.
By now you must have heard that you must Assign variable, Initialize variable and declare variable. What do these terms stand for?

First of, variable can be initialized

Secondly, variable can be declared

Lastly, variable can be assigned value

Basic mathematical operations with value types

(Addition, subtraction, multiplication, and division)

Be warned that it is recommended to use same data types  when performing these kind of operations, soon you will find why.

Addition

Lets start by adding two integers together.

Try running this code and result will be “3”.

Now try changing secondNumber value to 2.2, you will get compiler warning “Cannot implicitly convert type ‘double’ to ‘int’.” Following actions can be performed depending on your desired outcome:

I want my result to be an integer:

Difference between these two approaches is that first one will always throw Exception but second one will throw exception only if value is greater than Int32.MaxValue (2,147,483,647) or less than Int32.MinValue (-2,147,483,648).

I want my result to be an double

Subtraction, multiplication, and division

Same as addition, but instead of “+” operator we use “-“, “*” and “/” operators.

Thank you for following me this far! Our next lesson will be Eumerations.

 

© 2019 LearnC#WithDarko

Theme by Anders NorenUp ↑