Вы находитесь на странице: 1из 4

SA-OY, MA. JOYCE A.

TA4-BSIT

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 computer's 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 variable's 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 hasn't been
declared, your code won't 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;

Unlike humans, a computer does not know the difference between "1234" and
"abcd." A data type is a classification that dictates what a variable or object can
hold in computer programming. Data types are an important factor in virtually all
computer programming languages, including C#, C++, JavaScript, and Visual
Basic. When programmers create computer applications, both desktop and web-
based, data types must be referenced and used correctly to ensure the proper result
and an error-free program.
Common examples of data types
Boolean (e.g., True or False)
Character (e.g., a)
Date (e.g., 03/01/2016)
Double (e.g., 1.79769313486232E308)
Floating-point number (e.g., 1.234)
Integer (e.g., 1234)
Long (e.g., 123456789)
Short (e.g., 0)
String (e.g., abcd)
Void (e.g., no data)

Depending on the programming language, there may also be many more data types
that serve a specific function and store data in a particular way. Understanding
the different data types allows programmers to design computer applications more
efficiently and accurately.

 Boolean data type is a data type that has one of two possible values (usually
denoted true and false) which is intended to represent the two truth values of
logic and Boolean algebra. It is named after George Boole, who first defined
an algebraic system of logic in the mid 19th century.
 A character is any letter, number, space, punctuation mark, or symbol that
can be typed on a computer. The word "computer," for example, consists of
eight characters. The phrase "Hi there." takes up nine characters.
Each character requires one byte of space, so "computer" takes up 8 bytes.

 A floating-point is a variable type that is used to store floating-point


number values. A floating-point number is one where the position of the
decimal point can "float" rather than being in a fixed position within
a number. Examples of floating-point numbers are 1.23, 87.425, and
9039454.2.

 An integer is a whole number (not a fraction) that can be positive, negative,


or zero. Therefore, the numbers 10, 0, -25, and 5,148 are all integers. Unlike
floating point numbers, integers cannot have decimal places. Integers are a
commonly used data type in computer programming.

 A string is a data type used in programming, such as an integer and floating


point unit, but is used to represent text rather than numbers. It is comprised
of a set of characters that can also contain spaces and numbers. For example,
the word "hamburger" and the phrase "I ate 3 hamburgers" are both strings.
Even "12345" could be considered a string, if specified correctly. Typically,
programmers must enclose strings in quotation marks for the data to
recognized as a string and not a number or variable name.
A tuple may include zero or more elements. To indicate how many elements
it contains, it may be referred to as an n-tuple, where "n" is the number of
elements. Often, a tuple is represented as a comma-delimited list of the
elements, enclosed in parentheses. For example, "(5, 9, 11, 3, 22, 14)" is a "6-
tuple."

Case Statement example.

Вам также может понравиться