Chapter 2 class slides
Chapter 2 class slides
Number
systems
Electronic storage media are only capable to store single
computers
bits. use
the
binarnumber system where the two distinct states are represented
Therefore, . In the same
as 0 and
way
y that
1 1 and 1 use the same digits but represent different2numbers,
and 2
use
504 the same
0 450bits0 but
1100represent two different 1001
numbers.
Think back to your grade 2 days and remember that your teacher taught you about the place
values of digits in a
number: 50 = 51 2 +0 x1 1 +4 x1 0
4 x= 50
0 + 00 +0
0 4
Data representation
X10 X2 X16
Binary numbers are difficult toandread
even small values will occupy many
digits. Thehexadecimal system provides a human friendly representation
of
binary numbers. Eachhexadecimal digit represents four bits
. For example:
1011001110
2 = 0010 1100 1110
= 2 C E
= 2 x162 + 161 + 14 x160
12 x
= 512 + 192 + 14
= 718 (Decimal, Base10)
7 0111 7
8 1000 8
9 1001 9
10 1010 A 11
1011
B 12
1100
C 13
1101
D 14
1110
E 15
1111
F
Representation of
characters
Inorder to store characters such as 'A', '*', ' ' (space), etc. in a digital format, the American
Standards Association developed a coding scheme, the ASCII (table American
in 1968
Standard Code for Information Interchange). Since196 the standard has been revised
and extended to accommodate various characters 8 in many of the world's
. languages
Currentl , theUT-8 ( -bit Unicode Transformation Format)
standard is steadily becoming
the
y preferred F encoding
8 scheme
. In the table below,first
the four bits of every byte is
shown in the left hand column and the second four bits in the top row. Thus, the character
'A' is represented with 0100 0001or(binary)
41hex.
Understanding Variables and Types
Primitive types
Variables are "things" like objects but they are saved differently in the computer's memory.
Variables are saved on the stack and the variable name points to the value itself. Objects are
saved on the heap and the object name points to an address where the object is saved.
The word type is used to indicate the kind of variable or object that is referred to.
There are three categories of types: Value types are used for variables and
reference types are used for objects and strings. Pointer types are beyond the
scope of this book. Classes and strings are reference types.
Understanding Variables and Types
variables
char contain
variables a series
contain single of alphanumeric
charactersand string
The most common variable types are:
characters.
char
int, double and decimal variables contain numeric
string values. int variables contain whole numbers, i.e.
values with no decimal part. double variables
int contain values which may or may not have a decimal
part. decimal variables also contain values that may
Double have a decimal part, but they have higher precision
and are therefore more appropriate for monetary
decimal values. (RULE OF THUMB, DEC IS FOR CURRENCY)
bool variables contain either the word true or the
bool word false. That is, there are only two possible
values for bool variables. These values indicate if a
specific condition holds or not.
Understanding Variables and Types
The most common variable types are:
char values are written between single quotes
char and string values are written between double
quotes.
string Values of
in other
types are
tDouble not
written
decimal between
quotes.
bool
As for controls, variables are named such that:
1. the name reflects the purpose of the variable and 2. indicate the type of the variable.
Note the prefixes in the examples above and identify the type of each.
C# is case sensitive. This means that iAge and iage are two different variable names.
Assignment of values
strinsName;
g
to declare and instantiate the variable
.
declaration; initialisation That is, we don't need to assign a value
instantiation to the variable upon declaration and
we can always, at a later stage, write
Assignment of values
sName = "Jay";
The '=' in the code above is called an assignment operator. This simply means that a value is
assigned to a variable or that the memory cell gets content. The variable name is always at
the left hand side of the assignment operator and the value always at the right hand side.
Assigning a starting value for a variable is referred to as initialisation.
Note that '=' does not mean "equals"!! The line of code above should be read as: "Assign the
value "Jay" to sName.".
As an example of the possible danger that could happen if '=' was read as "equals", consider
the following:
int iAge = 21;
iAge = 29;
If '=' was "equals" the second line above would mean that 21 = 29! Instead, it means that
the initial value of 21 is replaced by 29. In other words, the previous value was kicked out of
the memory cell and now the memory cell contains the value 29.
Chapter 2
Handling Data Part 2
We use the word literal to refer to fixed values. For example 17, "Jay", 12.5 are literals.
Input in a Console Application
Input in a Console Application
The line
stringsName = Console.ReadLine();
The line
stringsNameSurname= sName + " + sSurname;
"
also does several things at once:
1. It concatenates (join head-to-tail) the string
variables sName and sSurnamewith a space
between them.
2. It declares a string variable
, sNameSurname.
3. It assigns the concatenated character string to
the string variable.
Input in a Console Application
The line
int iAge = int.Parse(Console.ReadLine() );
The line
Console.WriteLine("Good day, " + sNameSurname+ "! );
"
1. displays a character stringthat consists of a greeting
message and the contents of the variable
sNameSurname.
2. The text between double quotes is displayed exactly as it
is typed while the content of thevariable sNameSurname
is concatenated with it.
Input in a Console Application
The line
Console.WriteLine("You are " + iAge .ToString() + " years old."
);
In C#, type casting refers to converting a value from one data type to another.
Type casting can be done implicitly or explicitly.
Explicit type casting, also known as casting or type conversion, is when you
explicitly tell the compiler to convert a value from one type to another using a cast
operator. For example, when you convert an integer value to a string, you use the
ToString() method:
int myInt = 10; string myString =
myInt.ToString();
Here, the integer value 10 is explicitly cast to a string using the ToString() method.
Implicit type casting, also known as type coercion, is when the compiler
automatically converts a value from one type to another without you having to
specify it. This happens when the value being assigned to a variable or passed as
an argument is compatible with the destination type. For example, when you
convert a smaller integer type to a larger integer type, such as byte to int, no cast
operator is required: Byte myByte = 10; int myInt = myByte;
Here, the byte value 10 is implicitly cast to an int, because an int can hold a larger
range of values than a byte.
There are two methods that we can use for formatting output. ToString() is a non-static
method that must be applied to a numeric variable. It converts the numeric value to a
string and formats it according to given parameters. The Format() method is a static
method of the String class. This means it must be called with reference to the class and
not a variable.
The ToString() method
Arithmetic
Formattingoperators
output and operands
The ToString() method may take a string parameter between the brackets that indicates
the way in which the output must be formatted.
Console.WriteLine("Retail price: " + mRetailPrice.ToString("0"));
Console.WriteLine("Retail price: " + mRetailPrice.ToString("0.0"));
Console.WriteLine("Retail price: " + mRetailPrice.ToString("0.00"));
If we call the method without any parameters, no formatting is done.
Arithmetic operators and operands