3-CSharpPart1.ppt

advertisement
Values & Variables
1
Properties of variables





Should have a type
Stores data
Case sensitive
Their names can not start with a
number
Reserved keywords can not be used as
variable names
2
Keywords
3
How to define a variable
Type
Value
int x = 3;
Variable
Semicolon: End of
statement
4
Ways to define a variable
int nInteger;
string sString;
int nInteger = 42;
string sString = "This is a string!";
int nInteger;
string sString;
...
nInteger = 42;
sString = "This is a string!";
Double quotes
represents a string
5
Necessity to set a value to a
variable
string sValueless;
MessageBox.Show(sValueless);
Error!
6
Variable Types

Simple types




Integers
Floating point numbers
Characters
Strings
7
Integers

short





(0 <-> 65,535)
int nval
(–2,147,483,647 <-> 2,147,483,647)
= -12500;
4 bytes
(0 <-> 4,294,967,295)
uint nval = 12500;
long 8 bytes


4 bytes
uint

2 bytes
ushort sval= 12;
int

(–32,768 <-> 32,767)
short sval = -12;
ushort

2 bytes
long lVal = -548444101;
ulong

8 bytes
Ulong lVal = 548444101
8
Floating Point Numbers

float



float fVal = -1,2;
double

8 bytes
double dVal = -3.565;
decimal

4 bytes
8 bytes
decimal dVal = -3456.343;
9
Expressions



Expressions are used for performing
operations over variables.
Return a value of known type.
Two types of expressions


Operators
Functions
10
Arithmetic operations


They need more than one variable.
Performs mathematical operations






+ (addition operation)
- (subtraction operation)
* (multiplication operation)
/ (division operation)
% (modulus operation)
….
11
Arithmetic operations

Abbreviations
int m = 5;
int n = 4;
m = m + n; equals
m += n;
In other words in the end of both expressions
m will have value of 9 and the value of n will
not be changed.
12
Increment and decrement
operations


They operate on one variable
++ is increment operator


i = i + 1;
-- is decrement operator


i++;
i --;
i = i – 1;
Prefix and postfix operators will yield to
different results.

i.e. “i++” and “++i” are not same.
13
Increment and decrement
operations




++k The result of the operation is the value
of the operand after it has been incremented.
k++ The result of the operation is the value
of the operand before it has been
incremented.
--k The result of the operation is the value
of the operand after it has been
decremented.
k-- The result of the operation is the value
of the operand before it has been
14
decremented.
Example
int k=0, m;
Values of m and k will be 1
m = ++k;
int k=0, m;
m = k++;
int k=5, m, n=2;
m = --k + n;
int k=0, m, n=7;
m = k++ + --n;
m will be 0 and
k will be 1
m will be 6 and
k will be 4
m will be 6 and
k will be 1 and
n will be 6
15
Exercise

What will be the values of the variables
after code piece below is executed?
int i, j, k;
i
j
k
i
i
= 2;
= 3 + i++;
= 3 + ++i;
*= ++k + j--;
/= k-- + ++j;
16
Exercise

Assuming that line of codes are
independent, what will be the value of
variable m after each line is executed?
int i = 0, j = 6, k = 4 , m = 5;
•m = k-- + ++i;
•m *= j % 4;
•m += k++ + (j-- * ++i);
17
Order of Operations



Rules that defines which procedures should
be performed first.
In C# language some operators have
execution privilege over others.
To predict the result of an expression first we
should know the order of operations.
18
Example

PEMDAS phrase may help to remember the order.
1 + 2 * 3 - 4 / 5 = ?
1 + (2 * 3) – (4 / 5)
6.2
P
E
M
D
A
S
Parenthesis
Exponent
Multiplication
Division
Addition
Subtraction
19
Example(result)

If we use all numbers in integer type then the result
will be integer(In other words fraction will be
removed)
1 + (2 * 3) – (4 / 5)
7
4/5 = 0
(integer division)
20
Exercise

Different data types may yield different
results in same operations.


Write and execute the codes in the next
slides.
Explain the difference between results.
21
Exercise (continues)
22
Exercise (continues)
23
Characters

char
1 byte
'a' 'z' 'A'
'Z'
0-256
'?'
'@'
'0'
'9'
Special characters are represented by using “\” prefix.
'\n'
'\t'
'\''
'\\'
: new line
: tab
: single quote
: backslash
24
Strings (Character Arrays)

Sequence of characters.

Example:



“Hello!”
“first line\n second line \n third line”
“”
Empty string
25
Strings



“string” Class
Unicode – 16 bit
Example:


string myString = “Hello!”;
Verbatim strings

string myString = @“2.5”” disk”;
26
string operations

Appending two strings
Result: “Hello world!”
27
string operations

Searching within a string

int IndexOf ()
Result: 1
Exercise: Find the usage of LastIndexOf() function and write an example by
using this function.
28
string operations

Retrieve a substring from a string

string Substring()
Result : “llo”
29
Exercise



Put your name and surname into two
string variables.
Concatenate two strings.
Write the result to the console.
30
DateTime


C# language has built-in “DateTime”
structure to represent date and time
values.
We can store “year, month, day, hour,
minute, second” values in a DateTime
structure.
31
Creating a DateTime Object
Type

Creating a
new object
DateTime dt = new DateTime(year, month, day);
Variable
name
Initial
values
32
DateTime Fundamentals

Functions and Properties




AddDays, AddMonths, AddYears
DateTime.Now
DayOfWeek
TimeSpan
33
Example
A new DateTime
object is created
34
Constants





Their values can not be changed.
They have types.
We can use them in expressions bur can not
alter them.
Defined by using “const” keyword before
variable type.
Their values can be set only during definition
line.

const int nVar = 34;
35
Example
36
Download