Teaching Myself C Sharp Lesson 3 Storing Information with Variables

Lesson 3. Wow!
I seem to be steadily progressing through this self taught series at a nice pace. The information is sinking in but not quite fast enough nor is it still at the level I desire to be but honestly, I am on my way and gain confidence as each lesson is completed. I just hope and pray that my efforts are rewarded with understanding and knowledge.
This is now Lesson 3 and all my notes and research come from:
Sams Teach Yourself C# in 21 Days
2002 Bradley L. Jones

Storing Information with Variables
In this lesson I will learn:
– What is a variable.
– How to create variables.
– Using different types of numerical variables.
– Know the differences between character and numerical variables.
– How to declare and initialize variables.

Variables and Variable Names
A variable is a named data storage location in your computers memory. By naming the variable and using the assigned variable means we will always be referring to that stored value. Variables can store numerical and character information.
Variable names follow these rules:
♦ Can contain letters, digits and underscore character (_).
♦The first character must be a letter. The underscore can also be used first byte can be    hard to read and discern.
♦ C# is case-sensitive so the use of capitals becomes important and must be entered      exactly as variable was first entered.
♦ C# keywords cannot be used as variables.
Variablenamesandlegality.PNGWhen naming variables it is important and handy to name them logically and in a way so that you can understand what they mean at a glance. For example when creating a variable to calculate the area of a circle could be named – Circlearea, circle_area, Circle_Area.
The standard for naming variables is usually the Pascal notation style which means that one capitalizes the first character as in Circlearea.
C# supports a Unicode character set. Any language or Unicode character can be used but must be exactly the same throughout the code.

Using Variables
Before using a variable it must first be declared. This tells the compiler that the variable named will be used to store information as well as tells the computer to set aside memory for it.

Assigning Values to Variables
A variable must first be declared before you can use it but a variable can be declared at almost any place in the listing.
You can change the value of a variable within the listing. In this example the variable ‘my_variable’ is assigned the value of ‘5’, it is then assigned a new value of ‘1010’.

Setting Initial Values in Variables
You can set a value to the variable at the same time that you declare it.
setting intial variale values.PNG
In the example above. The my_variable was given an initial value of 8 but was then given a new value of 5 further in the code. The your_variable was given the value of 1000 but then that was changed to 990.

Numerical Variable Types
Out of all these terms I can only recall mainly using ‘int’, ‘float’ and ‘bool’ whilst in class and in the assignments.
It is interesting to find out that a ‘float’ or a ‘double’ could return small errors due to the rounding up rules. Using ‘decimal’ will return exact results and no errors.
The integral data types are all to do with the amount of memory needed to store the number entered.
Decimal should be used for financial and other precise calculations.

Literals Versus Variables
Literal values stand on their own in the code.
For example:

int x = 10; 

myStringValue = “Camo is a nerd! Yeah I wish!!”;

Numerical Literals Defaults
Numerical literals are by default either an ‘int’ or a ‘double’.
Floating Point Literals
When using a ‘float’ a ‘f’ or ‘F’ must follow the number. For example ‘my_float = 3.3f;’
String Literals
A group of characters is called a string.

Sorry no relevance here, just thought I would throw in a pic to break up the words. Carry on………

Constants are used when you want to assign a value that will not change and will prevent others from changing it. For example: ‘const float PI = 3.1459’ or ‘ const float AREA = 33
By entering the names in CAPITALS will help identify the variable as constant. These variables can be used in a program but the values will not be able to be changed.

Conclusion Lesson 3
I learnt how to assign values to variables and what types to use.
I learnt that by using the right type will make the code run more efficiently and use less computer memory.
A lot of this lesson was a bit of gobbedly gook to me still but I assume that I will not use most of these other types and best leave that to those crazy programmers.

Thank you for your time.
Peace and blessings.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s