Monday, March 2, 2020
What Are Variables in Computer Programs
What Are Variables in Computer Programs A variable is a way of referring to a storage area in a computer program. This memory location holds values- numbers, text or more complicated types of data like payroll records. Operating systems load programs into different parts of the computers memory so there is no way of knowing exactly which memory location holds a particular variable before the program is run. When a variable is assigned a symbolic name like employee_payroll_id, the compiler or interpreter can work out where to store the variable in memory. Variable Types When you declare a variable in a program, you specify its type, which can be chosen from integral, floating point, decimal,à boolean or nullable types. The type tells the compiler how to handle the variable and check for type errors. The type also determines the position and size of the variables memory, the range of values that it can storeà and the operations that can be applied to the variable. A few basic variable types include: int - Int is short for integer. It is used to define numeric variables holding whole numbers. Only negative and positive whole numbers can be stored in int variables.à null - A nullable int has the same range of values as int, but it can store null in addition to whole numbers. char - A char type consists of Unicode characters- the letters that represent most of the written languages.à bool - A bool is a fundamentalà variable type that can take only two values: 1 and 0, which correspond to true and false.à float, double and decimal - these three types of variables handle whole numbers, numbers with decimals and fractions. The difference between the three lies in the range of values. For example, double is twice the size of float, and it accommodates more digits. Declaring Variables Before you can use a variable, you have to declare it, which means you have to assign it a name and a type. After you declare a variable, you can use it to store the type of data you declaredà it to hold. If you try to use a variable that hasnt been declared, your code wont compile.à Declaring a variable in C# takes the form: data_type variable_list; The variableà list consists of one or more identifier names separated by commas. For example: à int i, j, k; à char c, ch; Initializing Variables Variables are assigned a value using an equal sign followed by a constant. The form is: data_typeà variable_name value; You can assign a value to a variable at the same time you declare it or at a later time. For example: à int i 100; à or à short a;int b;double c; à /*actual initialization */a 10;b 20;c a b; About C#à C# is an object-oriented language that does not use any global variables. Although it could be compiled, it is almost always used in combination with the .NET framework, therefore applications written in C# are run on computers with .NET installed.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.