You are on page 1of 28

CS115C - COMPUTER

PROGRAMMING
Primitive
Types and Variables
TOPIC OUTLINE
• Variables
• Primitive Data Types
• Major sets of Data Types

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 2


WHAT IS VARIABLE?
• A typical program uses various values that change during its
execution.
• When a user enters a new value that will be used in the process of
calculation, we can preserve it (temporarily) in the random access
memory of our computer.
• The values in this part of memory change (vary) throughout
execution and this has led to their name – variables.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 3


WHAT IS VARIABLE?
• A variable is an item of data used to store state of objects.
• A variable has a data type and a name. The data type indicates the
type of value that the variable can hold. The variable name must
follow rules for identifiers.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 4


WHAT IS VARIABLE?

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 5


WHAT IS VARIABLE?

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 6


DATA TYPES
Are sets (ranges) of values that have similar characteristics.

Data types are characterized by:


• Name – for example, int;
• Size (how much memory they use) – for example, 4 bytes;
• Default value – for example 0.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 7


TYPES
Basic data types in C# are distributed into the
following types:
• Integer types – sbyte, byte, short, ushort, int, uint, long,
ulong;
• Real floating - point types – float, double;
• Real type with decimal precision – decimal;
• Boolean type – bool;
• Character type – char;
• String – string;
• Object type – object.
• These data types are called primitive (built-in types)
08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 8
08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 9
INTEGER TYPES
Integer types represent integer numbers and are
• sbyte,
• byte,
• short,
• ushort,
• int,
• uint,
• long and
• ulong

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 10


INTEGER TYPES
Signed Integral
• These data types hold integer values (both positive and negative).
• Out of the total available bits, one bit is used for sign.

sbyte

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 11


INTEGER TYPES
Signed Integral
short

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 12


INTEGER TYPES
Signed Integral
int

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 13


INTEGER TYPES
Signed Integral
long

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 14


FLOATING POINT
These data types hold floating point values i.e., numbers
containing decimal values.
float

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 15


FLOATING POINT
Double-precision floating point type.
Double

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 16


DECIMAL
Decimal type has more precision and a smaller range as compared to
floating point types (double and float). So, it is appropriate for
monetary calculations.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 17


CHARACTER (CHAR)
It represents a 16-bit Unicode character.

https://www.rapidtables.com/code/text/unicode-characters.html
08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 18
BOOLEAN
• Boolean data type has two possible values: true or false
• Default value: false

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 19


LITERALS

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 20


OPERATORS
AND EXPRESSIONS

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 21


OPERATORS
Operators allow processing of primitive data types and objects. They take
as an input one or more operands and return some value as a result.

Categories

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 22


08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 23
MODULUS (%)
• The modulus operator - or more precisely, the modulo operation - is a
way to determine the remainder of a division operation.

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 24


08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 25
08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 26
ARITHMETIC
• Write a C# program that will calculate the sum and product. Find the
difference of the first and last number. After that, calculate the its average.

Expected output:
The sum of 1, 2, 3, 4, 5, 6, 7, 8,9 and 10 is: 55
The product of 1, 2, 3, 4, 5, 6, 7, 8,9 and 10 is: 3,628,800
--------------------------------------------------
The difference of 1 and 9 is: -8
--------------------------------------------------
The average is: 5.5

08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 27


Join our online GDB online Compiler
https://www.onlinegdb.com/
https://www.programiz.com/csharp-programming/
variables-primitive-data-types
https://bit.ly/2BSeCEA
08/07/2023 COLLEGE OF ENGINEERING AND INFORMATION TECHNOLOGY 28

You might also like