Upcoming SlideShare
×

# Spf Chapter4 Variables

786 views

Published on

> 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)

Published in: Education
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
786
On SlideShare
0
From Embeds
0
Number of Embeds
231
Actions
Shares
0
16
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Spf Chapter4 Variables

1. 1. Chapter 4<br />C# .NET: Variables<br />
2. 2. Variables: value and reference types<br />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<br />
3. 3. Chapter 4 topics<br />Variables in details<br />Number – byte, int and long<br />Decimal – float and double<br />Character<br />Boolean<br />String<br />Implicit and explicit conversion <br />Numbers, Decimals and their operations (=, +, -, *, / and others)<br />Strings and operations (assigning value, concatenation)<br />
4. 4. Value reference types<br />Number<br /> Types not covered: short, unsigned number such as ulong (unsigned long integer), etc<br />Decimal<br /> Type not covered: decimal (12 bytes)<br />
5. 5. How to use them - Numbers?<br />byte numberInByte1 = 64; // OK: auto conversion<br />byte numberInByte2 = 256; // Error<br />intnumberInByte = 64; // Default is Integer<br />long numberInLong1 = 64L; // L for long number<br />long numberInLong2 = 64 // OK: auto conversion<br />
6. 6. How to use them - Numbers?<br />byte numberInByte1 = 64; // OK: auto conversion<br />byte numberInByte2 = 256; // Error<br />intnumberInByte = 64; // Default is Integer<br />long numberInLong1 = 64L; // L for long number<br />long numberInLong2 = 64 // OK: auto conversion<br />Why cannot use small letter L ( l )?<br />
7. 7. How to use them - Numbers?<br />byte numberInByte1 = 64; // OK: auto conversion<br />byte numberInByte2 = 256; // Error<br />intnumberInByte = 64; // Default is Integer<br />long numberInLong1 = 64L; // L for long number<br />long numberInLong2 = 64 // OK: auto conversion<br />// Default type is Integer<br />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. <br />Auto (implicit) conversion for numberInByte1 but error for numberInByte2<br />
8. 8. Declare and initialize<br />// Declare and initialize one variable in one line<br />int number1 = 5; // number1 variable is<br /> // assigned with a value 5<br />int number2 = 10;<br />// Declare and initialize more than one variable <br />// in one line<br />int number1 = 5, number2 = 10;<br />
9. 9. Declare and initialize<br />// Declare and initialize one variable in one line<br />int number1 = 5; // number1 variable is<br /> // assigned with a value 5<br />int number2 = 10;<br />// Declare and initialize more than one variable <br />// in one line<br />int number1 = 5, number2 = 10;<br />When a variable is declared and set with a value at the same time => declare and initialize<br />When it is set with value subsequently => assign with value<br />
10. 10. How to use them - decimals?<br />float numberInFloat1 = 8.0F; // F for floating number <br />float numberInFloat2 = 8.0f; // OK: Small letter F<br />double numberInDouble1 = 8.0; // Default<br />double numberInDouble2 = 8.0f; // OK: Auto conversion<br />
11. 11. How to use them - decimals?<br />float numberInFloat1 = 8.0F; // F for floating number <br />float numberInFloat2 = 8.0f; // OK: Small letter F<br />double numberInDouble1 = 8.0; // Default<br />double numberInDouble2 = 8.0f; // OK: Auto conversion<br />Do you think the following is OK? Why?<br />float numberInFloat3 = 8.0; <br /> //Hint: Default is double<br />
12. 12. Try it out!<br /><ul><li>Create a new WinForm project: SpfChapter4
13. 13. Drag and drop a button onto the form
14. 14. Double click on the button and add the codes to button1_Click event:</li></ul>/* Number Type */<br />byte numberInByte1 = 64; // OK: auto conversion<br />byte numberInByte2 = 256; // Error: Too big for byte<br />intnumberInByte = 64; // Default is Integer<br />long numberInLong1 = 64L; // L for long number<br />long numberInLong2 = 64 // OK: auto conversion<br />long numberInLong3 = 64l; // Error: Small letter L <br />int n1 = 5, n2 = 10; // Declare more than one var<br />// Next page<br />
15. 15. Try it out!<br />/* Decimal Type */<br />float numberInFloat1 = 8.0F; // F for floating number <br />float numberInFloat2 = 8.0f; // OK: Small letter F<br />double numberInDouble1 = 8.0; // Default<br />double numberInDouble2 = 8.0f; // OK: Auto conversion<br />// Default for decimal is double (8 bytes)<br />float numberInFloat3 = 8.0; // Error: Too small (4 bytes ) <br /> // to store the double <br /> // precision (accuracy) <br />
16. 16. Explicit conversion<br />// Implicit conversion => Automatic conversion<br />// Explicit conversion => do it explicitly<br />// i.e. tell the compiler that you want it to convert and<br />// that you know what you are doing<br />float numberInFloat4 = (float) 8.0; // No more error<br />
17. 17. Explicit conversion<br />// Implicit conversion => Automatic conversion<br />// Explicit conversion => do it explicitly<br />// i.e. tell the compiler that you want it to convert and<br />// that you know what you are doing<br />float numberInFloat4 = (float) 8.0; // No more error<br />// But still give error if the value is too big<br />// For floating type: max value is 3.4e38<br />float numberInFloat5 = (float) 3.5e38; // Error<br />
18. 18. Value reference types<br />Other common value reference types<br />
19. 19. Special type<br />String<br />string is a reference type but behaves like value type <br />Memory usage is reference type<br />Behave like value type<br /> string str = “a new string”; // No need to use New keyword<br />Reason: Microsoft wants to make string in .NET safe and fast for programmer to handle sequence of characters. <br />Good tutorial on C# string: http://alturl.com/r4qa<br />
20. 20. How to use them?<br />boolisMoving = true; // Use true or false<br />boolhasCompleted = false;<br />char answer = ‘ Y ’; // Between ‘ ’<br />string str = “my name”; // Between “ ”<br />
21. 21. Try it out!<br /><ul><li>Continue from previous project and add the codes to button1_Click event:</li></ul>boolisMoving = true; // Boolean use true or false<br />boolhasCompleted = false;<br />char answer = 'Y'; // Between ‘ ’<br />string str = "my name"; // Between “ ”<br />
22. 22. There is a specific relationship between where a variable is defined and where it can be used. <br />This is known as the scope of the variable. <br />apple only exists in Class2V while june only exists in Class2W. mrPuahexists in CampusMP, Class2V and Class2W.<br />CampusMP<br />mrPuah<br />Scope of variable<br />Class2V<br />apple<br />Class2W<br />june<br />
23. 23. Scope of variable<br />A variable once declared, exist only within the code block : { .. }<br /> button1_click( .. )<br /> {<br /> string apple = “ABC”; // declared here: apple only <br /> // exist here<br /> }<br /> button2_click(.. )<br /> {<br /> apple = “DEF”; // Error: apple not defined<br /> }<br />
24. 24. Scope of variable<br />string mrPuah = “I am here!”; // Declared on<br /> // outer { .. }<br /> button1_click( .. )<br /> {<br />mrPuah = “ABC”; // OK<br /> }<br /> button2_click(.. )<br /> {<br />mrPuah = “DEF”; // OK<br /> }<br />
25. 25. Try it out!<br /><ul><li>Continue from previous project and add the codes to button1_Click event:</li></ul>string str = "my name"; // Previous code<br /> { // Add an inner code block<br />str = "change name"; // No error, within inner { .. }<br /> string str2 = “your name";<br /> }<br />str2 = "change name"; // Error: str2 only exist in <br /> // inner { .. }<br />
26. 26. Operators<br />Symbol to perform on expression (part of a statement)<br />For numbers and decimals: =, +, -, *, /, %, ++, -- and<br /> +=, -=, *=, /= <br />For string: =, + (concatenate) and += <br />For the full list of operators, refer to:<br /> http://alturl.com/bokx<br />
27. 27. Operators for numbers/decimals<br />
28. 28. Operators for numbers/decimals<br />For complex expression like w + x / y - z<br />Use brackets ( .. ) to tell compiler which portion to evaluate first. Eg (w + x) / (y – z)<br />Otherwise, compiler will use operators precedence rule. Acronyms like BPODMAS<br /> Refer to: http://alturl.com/9b8r<br />
29. 29. Operators for numbers/decimals<br />
30. 30. Operators for string<br />
31. 31. Exercise 4.1<br />Textbook from page 52 – 71:<br />Part 1 String Variables in C#.NET<br />Part 2 Assigning Text to a String Variable <br />Part 3 Concatenation in C#.NET <br />Part 4 Comments in C#.NET <br />
32. 32. Exercise 4.2<br />Textbook from page 71 – 83:<br />Part 5 Integer Variables <br />Part 6 Double and Float Variables <br />Part 7 Double Variables in C# .NET <br />
33. 33. Exercise 4.3<br />Textbook from page 83 – 92:<br />Part 8 Addition in C# .NET <br />Part 9 Subtraction in C# .NET <br />Part 10 Multiplication and Division in C#.NET <br />
34. 34. Summary<br />Variables in details<br />Number – byte, int and long<br />Decimal – float and double<br />Character<br />Boolean<br />String<br />Implicit and explicit conversion <br />Numbers, Decimals and their operations (=, +, -, *, / and others)<br />Strings and operations (assigning value, concatenation)<br />