spf chapter4 variables

Post on 30-Jun-2015

477 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

> Variables in details . Number – byte, int and long . Decimal – float and double . Character . Boolean . String > Implicit and explicit conversion > Numbers, Decimals and their operations (=, +, -, *, / and others) > Strings and operations (assigning value, concatenation)

TRANSCRIPT

CHAPTER 4C# .NET: Variables

VARIABLES: VALUE AND REFERENCE TYPES

Recall what we covered in week 1 using pictures to explain the difference between value types and reference types. This chapter will cover value types in detail

CHAPTER 4 TOPICS

Variables in details Number – byte, int and long Decimal – float and double Character Boolean String

Implicit and explicit conversion Numbers, Decimals and their operations (=,

+, -, *, / and others) Strings and operations (assigning value,

concatenation)

VALUE REFERENCE TYPES

Number

Types not covered: short, unsigned number such as ulong (unsigned long integer), etc

Decimal

Type not covered: decimal (12 bytes)

C# Type Size in byte

byte 1

int (default) 4

long 8

C# Type Size in byte

float 4

double (default) 8

HOW TO USE THEM - NUMBERS?

byte numberInByte1 = 64; // OK: auto conversion

byte numberInByte2 = 256; // Errorint numberInByte = 64; // Default is

Integerlong numberInLong1 = 64L; // L for long

numberlong numberInLong2 = 64 // OK: auto conversion

HOW TO USE THEM - NUMBERS?

byte numberInByte1 = 64; // OK: auto conversion

byte numberInByte2 = 256; // Errorint numberInByte = 64; // Default is

Integerlong numberInLong1 = 64L; // L for long

numberlong numberInLong2 = 64 // OK: auto conversionWhy cannot use

small letter L ( l )?

HOW TO USE THEM - NUMBERS?

byte numberInByte1 = 64; // OK: auto conversion

byte numberInByte2 = 256; // Errorint numberInByte = 64; // Default is Integerlong numberInLong1 = 64L; // L for long numberlong numberInLong2 = 64 // OK: auto conversion

// Default type is Integer If a data is given as 64 without L => data is an

integer. Since numberInLong1 is large enough to store integer 64, there is an auto (implicit) conversion.

Auto (implicit) conversion for numberInByte1 but error for numberInByte2

DECLARE AND INITIALIZE// Declare and initialize one variable in one lineint number1 = 5; // number1 variable is // assigned with a value 5int number2 = 10;

// Declare and initialize more than one variable // in one lineint number1 = 5, number2 = 10;

DECLARE AND INITIALIZE// Declare and initialize one variable in one lineint number1 = 5; // number1 variable is // assigned with a value 5int number2 = 10;

// Declare and initialize more than one variable // in one lineint number1 = 5, number2 = 10;

When a variable is declared and set with a value at the same time => declare and initialize

When it is set with value subsequently => assign with value

HOW TO USE THEM - DECIMALS?

float numberInFloat1 = 8.0F; // F for floating number

float numberInFloat2 = 8.0f; // OK: Small letter F

double numberInDouble1 = 8.0; // Defaultdouble numberInDouble2 = 8.0f; // OK: Auto

conversion

HOW TO USE THEM - DECIMALS?

float numberInFloat1 = 8.0F; // F for floating number

float numberInFloat2 = 8.0f; // OK: Small letter F

double numberInDouble1 = 8.0; // Defaultdouble numberInDouble2 = 8.0f; // OK: Auto

conversion

Do you think the following is OK? Why?float numberInFloat3 = 8.0; //Hint: Default is double

TRY IT OUT!

/* Number Type */

byte numberInByte1 = 64; // OK: auto conversion

byte numberInByte2 = 256; // Error: Too big for byte

int numberInByte = 64; // Default is Integer

long numberInLong1 = 64L; // L for long number

long numberInLong2 = 64 // OK: auto conversion

long numberInLong3 = 64l; // Error: Small letter L

int n1 = 5, n2 = 10; // Declare more than one var

// Next page

Create a new WinForm project: SpfChapter4

Drag and drop a button onto the form Double click on the button and add the

codes to button1_Click event:

TRY IT OUT!/* Decimal Type */

float numberInFloat1 = 8.0F; // F for floating number

float numberInFloat2 = 8.0f; // OK: Small letter F

double numberInDouble1 = 8.0; // Default

double numberInDouble2 = 8.0f; // OK: Auto conversion

// Default for decimal is double (8 bytes)

float numberInFloat3 = 8.0; // Error: Too small (4 bytes )

// to store the double

// precision (accuracy)

EXPLICIT CONVERSION

// Implicit conversion => Automatic conversion

// Explicit conversion => do it explicitly

// i.e. tell the compiler that you want it to convert and

// that you know what you are doing

float numberInFloat4 = (float) 8.0; // No more error

EXPLICIT CONVERSION

// Implicit conversion => Automatic conversion

// Explicit conversion => do it explicitly

// i.e. tell the compiler that you want it to convert and

// that you know what you are doing

float numberInFloat4 = (float) 8.0; // No more error

// But still give error if the value is too big

// For floating type: max value is 3.4e38

float numberInFloat5 = (float) 3.5e38; // Error

VALUE REFERENCE TYPES

Other common value reference types

C# Type Size in byte

bool 1

char 2

SPECIAL TYPE

String string is a reference type but behaves like value type

Memory usage is reference type

Behave like value type string str = “a new string”; // No need to use New

keyword

Reason: Microsoft wants to make string in .NET safe and fast for programmer to handle sequence of characters.

Good tutorial on C# string: http://alturl.com/r4qa

HOW TO USE THEM?

bool isMoving = true; // Use true or false

bool hasCompleted = false;char answer = ‘ Y ’; // Between ‘ ’string str = “my name”; // Between “ ”

TRY IT OUT!

bool isMoving = true; // Boolean use true or false

bool hasCompleted = false;

char answer = 'Y'; // Between ‘ ’

string str = "my name"; // Between “ ”

Continue from previous project and add the codes to button1_Click event:

There is a specific relationship between where a variable is defined and where it can be used.

This is known as the scope of the variable.

apple only exists in Class2V while june only exists in Class2W. mrPuah exists in CampusMP, Class2V and Class2W.

CampusMP

mrPuah

SCOPE OF VARIABLE

Class2V

apple

Class2W

june

SCOPE OF VARIABLE A variable once declared, exist only within the code

block : { .. } button1_click( .. ) { string apple = “ABC”; // declared here: apple

only // exist here } button2_click(.. ) { apple = “DEF”; // Error: apple not defined }

SCOPE OF VARIABLE

string mrPuah = “I am here!”; // Declared on // outer { .. } button1_click( .. ) { mrPuah = “ABC”; // OK } button2_click(.. ) { mrPuah = “DEF”; // OK }

TRY IT OUT!

string str = "my name"; // Previous code

{ // Add an inner code block

str = "change name"; // No error, within inner { .. }

string str2 = “your name";

}

str2 = "change name"; // Error: str2 only exist in

// inner { .. }

Continue from previous project and add the codes to button1_Click event:

OPERATORS

Symbol to perform on expression (part of a statement) For numbers and decimals: =, +, -, *, /, %, ++, --

and +=, -=, *=, /= For string: =, + (concatenate) and +=

For the full list of operators, refer to: http://alturl.com/bokx

OPERATORS FOR NUMBERS/DECIMALS

Operand Description Usage

+ Add n3 = n1 + n2;

- Subtract n3 = n1 - n2;

* Multiply n3 = n1 * n2;

/ Divide n3 = n1 / n2;

% Remainder after divide

n3 = n1 % n2;

++ Increment by 1 n3++; or ++n3;

-- Decrement by 1 N3--; or --n3;

OPERATORS FOR NUMBERS/DECIMALS

For complex expression like w + x / y - z Use brackets ( .. ) to tell compiler which portion

to evaluate first. Eg (w + x) / (y – z)

Otherwise, compiler will use operators precedence rule. Acronyms like BPODMAS

Refer to: http://alturl.com/9b8r

OPERATORS FOR NUMBERS/DECIMALS

Operand Description Usage

= Assignment n1 = 2;

+= Addition assignment

n1 += n2; same as n1 = n1 + n2

- = Subtraction assignment

n1 - = n2; same as n1 = n1 - n2

*= Multiplication assignment

n1 *= n2; same as n1 = n1 * n2

/= Division assignment

n1 /= n2; same as n1 = n1 / n2

OPERATORS FOR STRING

Operand Description Usage

+ Concatenate str = "my name" + " " + "xxx" ;

= Assignment str = "xxx" ;

+= Concatenation assignment

str += str2; same as str = str + str2;

EXERCISE 4.1

Textbook from page 52 – 71: Part 1 String Variables in C#.NET Part 2 Assigning Text to a String Variable Part 3 Concatenation in C#.NET Part 4 Comments in C#.NET

EXERCISE 4.2

Textbook from page 71 – 83: Part 5 Integer Variables Part 6 Double and Float Variables Part 7 Double Variables in C# .NET

EXERCISE 4.3

Textbook from page 83 – 92: Part 8 Addition in C# .NET Part 9 Subtraction in C# .NET Part 10 Multiplication and Division in C#.NET

SUMMARY

Variables in details Number – byte, int and long Decimal – float and double Character Boolean String

Implicit and explicit conversion Numbers, Decimals and their operations (=,

+, -, *, / and others) Strings and operations (assigning value,

concatenation)

top related