inf1030-02-data-types-and-expressions
inf1030-02-data-types-and-expressions
and
Expressions
1
Objectives
• Examine how computers represent data
• Declare memory locations for data
• Explore the relationship between
classes, objects, and types
• Use predefined data types
• Use integral data types
2
Objectives (continued)
3
Objectives (continued)
4
Data Representation
• Bits
– Bit – "Binary digIT"
– Binary digit can hold 0 or 1
– 1 and 0 correspond to on and off,
respectively
• Bytes
– Combination of 8 bits
– Represent one character, such as the
letter A
– To represent data, computers use the
base-2 number system, or binary 5
number system
Binary Number System
Table 2-1
Binary
equivalent
of selected
decimal
values
8
Data Representation
(continued)
• Character sets
– With only 8 bits, can represent 28, or
256, different decimal values ranging
from 0 to 255; this is 256 different
characters
• Unicode – character set used by C#
(pronounced C Sharp)
– Uses 16 bits to represent characters
– 216, or 65,536 unique characters, can be
represented
• American Standard Code for Information
Interchange (ASCII) – subset of Unicode 9
– First 128 characters are the same
Data Representation
(continued)
13
14
Naming Conventions
• Pascal case
– First letter of each word capitalized
– Class, method, namespace, and
properties identifiers
• Camel case
– Hungarian notation
– First letter of identifier lowercase; first
letter of subsequent concatenated words
capitalized
– Variables and objects 15
Naming Conventions
(continued)
• Uppercase
– Every character is uppercase
– Constant literals and for identifiers that
consist of two or fewer letters
16
Examples of Valid Names
(Identifiers)
19
Types, Classes, and
• Type
Objects
– C# has more than one type of number
– int type is a whole number
– Floating-point types can have a fractional
portion
• Types are actually implemented through
classes
– One-to-one correspondence between a class
and a type
– Simple data type such as int, implemented
as a class
20
Types, Classes, and
Objects (continued)
• Instance of a class → object
• A class includes more than just data
• Encapsulation → packaging of data and
behaviors into a single or unit→class
21
Type, Class, and Object
Examples
22
Predefined Data Types
• Common Type System (CTS)
• Divided into two major categories
24
Value Types
• Primary difference
– How much storage is needed
– Whether a negative value can be stored
• Includes number of types
– byte & sbyte
– char
– int & uint
– long & ulong
– short & ushort
27
28
Examples of Integral Variable
Declarations
int studentCount; // number of students in the
class
30
Examples of Floating-Point
Declarations
double extraPerson = 3.50; // extraPerson originally set to
3.50
• Examples
decimal endowmentAmount =
33897698.26M;
32
Boolean Variables
33
Strings
• Reference type
• Represents a string of Unicode
characters
string studentName;
string courseName = "Programming
I";
string twoLines = "Line1\nLine2";
34
Making Data Constant
35
Assignment Statements
• Used to change the value of the
variable
– Assignment operator (=)
• Syntax
variable = expression;
• Expression can be:
– Another variable
– Compatible literal value
– Mathematical equation
– Call to a method that returns a compatible
value
– Combination of one or more items in this list36
Examples of Assignment
Statements
int numberOfMinutes,
count,
minIntValue;
numberOfMinutes = 45;
count = 0;
minIntValue = -2147483648;
37
Examples of Assignment
Statements
char firstInitial,
yearInSchool,
punctuation,
enterKey,
lastChar;
firstInitial = 'B';
yearInSchool = '1';
punctuation = '; ';
enterKey = '\n'; // newline escape character
lastChar = '\u005A'; // Unicode character 'Z'
38
Examples of Assignment
Statements (continued)
double accountBalance,
weight;
bool isFinished;
accountBalance = 4783.68;
weight = 1.7E-3; //scientific notation may be
used
deficitValue = -322888672.50M;
40
Examples of Assignment
Statements (continued)
string aSaying, fileLocation;
41
Examples of Assignment
Statements (continued)
43
Basic Arithmetic
Operations
45
Concatenation
48
Basic Arithmetic
Operations (continued)
52
Basic Arithmetic
Operations (continued)
• Order of operations
– Order in which the calculations are
performed
• Example
– answer = 100;
– answer += 50 * 3 / 25 – 4;
50 * 3 = 150
150 / 25 = 6
6–4=2
100 + 2 = 102
53
Order of Operations
• Associatively of operators
– Left
– Right
54
Order of Operations
(continued)
55
Mixed Expressions
• Implicit type conversion
– Changes int data type into a double
– No implicit conversion from double to int
double answer;
answer = 10 / 3; // Does not produce
3.3333333
57
Mixed Expressions
(continued)
• Explicit type conversion
– Cast
– (type) expression
– examAverage = (exam1 + exam2 + exam3) /
(double) count;
int value1 = 0,
anotherNumber = 75;
double value2 = 100.99,
anotherDouble = 100;
value1 = (int) value2;
// value1 = 100
value2 = (double) anotherNumber;// value2 = 75.0
58
Formatting Output
60
Numeric Format Specifiers
62
Custom Numeric Format
Specifiers
63
Table 2-17 Custom numeric format specifiers
Custom Numeric Format
Specifiers (continued)
64
Width Specifier
• Useful when you want to control the
alignment of items on multiple lines
• Alignment component goes after the
index ordinal followed by a comma
(before the colon)
– If alignment number is less than actual size,
it is ignored
– If alignment number is greater, pads with
white space
• Negative alignment component places spaces
on right
• Naming conventions
– Identifiers
• Spacing conventions
• Declaration conventions
66
Resources
67
Resources
68
Summary
69
Summary (continued)
70
Chapter Summary (continued)
• Constants
• Assignment statements
– Order of operations
• Formatting output
71