1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their...

36
1 Values & Variables
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    221
  • download

    1

Transcript of 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their...

Page 1: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

1

Values & Variables

Page 2: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

2

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

Page 3: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

3

Keywords

Page 4: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

4

How to define a variable

int x = 3;

Type

Variable

Value

Semicolon: End of statement

Page 5: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

5

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

Page 6: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

6

Necessity to set a value to a variable

string sValueless;MessageBox.Show(sValueless);

Error!

Page 7: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

7

Variable Types

Simple types Integers Floating point numbers Characters Strings

Page 8: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

8

Integers short 2 bytes (–32,768 <-> 32,767)

short sval = -12; ushort 2 bytes (0 <-> 65,535)

ushort sval= 12; int 4 bytes (–2,147,483,647 <->

2,147,483,647) int nval = -12500;

uint 4 bytes (0 <-> 4,294,967,295) uint nval = 12500;

long 8 bytes long lVal = -548444101;

ulong 8 bytes Ulong lVal = 548444101

Page 9: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

9

Floating Point Numbers

float 4 bytes float fVal = -1,2;

double 8 bytes double dVal = -3.565;

decimal 8 bytes decimal dVal = -3456.343;

Page 10: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

10

Expressions

Expressions are used for performing operations over variables.

Return a value of known type. Two types of expressions

Operators Functions

Page 11: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

11

Arithmetic operations They need more than one variable. Performs mathematical operations

+ (addition operation) - (subtraction operation) * (multiplication operation) / (division operation) % (modulus operation) ….

Page 12: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

12

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.

Page 13: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

13

Increment and decrement operations They operate on one variable ++ is increment operator

i++; i = i + 1; -- is decrement operator

i --; i = i – 1; Prefix and postfix operators will

yield to different results. i.e. “i++” and “++i” are not same.

Page 14: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

14

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 decremented.

Page 15: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

15

Exampleint k=0, m;int k=0, m;

m = ++k;m = ++k;

Values of Values of mm and and kk will be 1

int k=0, m;int k=0, m;

m = k++;m = k++;

m m will be 0 andkk will be 1

int k=5, m, n=2;int k=5, m, n=2;

m = --k + n;m = --k + n;

mm will be 6 and will be 6 andkk will be 4 will be 4

int k=0, m, n=7;int k=0, m, n=7;

m = k++ + --n;m = k++ + --n;

mm will be 6 and will be 6 andkk will be 1 and will be 1 and n will be 6

Page 16: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

16

Exercise

What will be the values of the variables after code piece below is executed?

int i, j, k;int i, j, k;

i = 2;i = 2;j = 3 + i++; j = 3 + i++; k = 3 + ++i; k = 3 + ++i; i *= ++k + j--; i *= ++k + j--; i /= k-- + ++j; i /= k-- + ++j;

Page 17: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

17

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;int i = 0, j = 6, k = 4 , m = 5;

•m = k-- + ++i; m = k-- + ++i; •m *= j % 4;m *= j % 4;•m += k++ + (j-- * ++i);m += k++ + (j-- * ++i);

Page 18: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

18

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.

Page 19: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

19

Example PEMDAS phrase may help to remember the

order.

P P ParenthesisParenthesisE E ExponentExponentMM MultiplicationMultiplicationDD Division Division AA AdditionAdditionSS SubtractionSubtraction

P P ParenthesisParenthesisE E ExponentExponentMM MultiplicationMultiplicationDD Division Division AA AdditionAdditionSS SubtractionSubtraction

1 + 2 * 3 - 4 / 5 = ?1 + 2 * 3 - 4 / 5 = ?1 + 2 * 3 - 4 / 5 = ?1 + 2 * 3 - 4 / 5 = ?

1 + (2 * 3) – (4 / 5)1 + (2 * 3) – (4 / 5)1 + (2 * 3) – (4 / 5)1 + (2 * 3) – (4 / 5)

6.26.26.26.2

Page 20: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

20

Example(result) If we use all numbers in integer type then the

result will be integer(In other words fraction will be removed)

4/5 = 04/5 = 0 (integer division)(integer division)

4/5 = 04/5 = 0 (integer division)(integer division)

1 + (2 * 3) – (4 / 5)1 + (2 * 3) – (4 / 5)1 + (2 * 3) – (4 / 5)1 + (2 * 3) – (4 / 5)

77 77

Page 21: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

21

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.

Page 22: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

22

Exercise (continues)

Page 23: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

23

Exercise (continues)

Page 24: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

24

Characters

char 1 byte 0-256

'a' 'z' 'A' 'Z' '?' '@' '0' '9''a' 'z' 'A' 'Z' '?' '@' '0' '9'

Special characters are represented bSpecial characters are represented byy using using ““\” \” prefix.prefix.

'\n''\n' : new line : new line'\t''\t' : tab : tab'\'''\'' : single quote : single quote'\\''\\' : backslash : backslash

Page 25: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

25

Strings (Character Arrays)

Sequence of characters.

Example: “Hello!” “first line\n second line \n third line” “” Empty string

Page 26: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

26

Strings

“string” Class Unicode – 16 bit Example:

string myString = “Hello!”; Verbatim strings

string myString = @“2.5”” disk”;

Page 27: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

27

string operations

Appending two strings

Result: “Hello world!”

Page 28: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

28

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.

Page 29: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

29

string operations

Retrieve a substring from a string string Substring()

Result : “llo”

Page 30: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

30

Exercise

Put your name and surname into two string variables.

Concatenate two strings. Write the result to the console.

Page 31: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

31

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.

Page 32: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

32

Creating a DateTime Object

DateTime dt = new DateTime(year, month, day);

Type

Variable name

Creating a new

object

Initial values

Page 33: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

DateTime Fundamentals

33

Functions and Properties AddDays, AddMonths, AddYears DateTime.Now DayOfWeek

TimeSpan

Page 34: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

34

Example

A new DateTime object is created

Page 35: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

35

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;

Page 36: 1 Values & Variables. 2 Properties of variables Should have a type Stores data Case sensitive Their names can not start with a number Reserved keywords.

36

Example