Thursday, October 18, 2018 20:03

Variables

If you want your program to perform useful actions, it has to store information. As you know, every time you run a program, the operating system loads the programs instructions into the computer’s memory. While running, your program stores values in the memory locations by using variables.

For instance, let’s say you have a program that prints a document. Every time you run the program, it will display a message that will ask you for the name of the file and the number of copies you want to print. When you input those information, the program stores the values you entered in some memory locations. To help the program find the memory locations where this data was stored, every memory location has a unique address. Because there can be billions of such addresses, keeping track of every location can be very difficult.

To simplify information storage, software programs define variables. These are names that the program associates with some locations in the memory. As the name variable suggests, the value that the program stores in these locations can be modified during the execution of that program. Every variable is defined by its type, which presents to the computer how much memory it needs in order to store that particular type of data and what operations can the program perform on that data.

In our previous example the program that prints the document should have a variable named filename (which stores the name of the file you want to print) and another variable named numberOfCopies (which stores the number of copies you want to print). Inside your codes you will refer to your variables by their name. That’s why you should always give your variables significant names (unlike math, where they are always called X or Y.. who is that damn X?!).

Usually, in our console applications, we will declare our variables immediately after the beginning of our Main function, before the instructions of our programs, like this:

The following program shows you how you should declare three variables of the type integer (variables that can store integer values, like 1, 3, 4, 11, etc):

Each variable is defined by its type, which defines the quantity of memory that that variable needs. To define an integer variable, C# programs uses the type int. After you declare a variable (which means indicating to the program its type and its name), you can assign a value to this variable (in other words, you can store data in it).

In the academic language, a variable is defined as a name that your program associates with a memory location.

Comments

comments

Tags: ,

Leave a Reply