Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1S467 GUI Programming Lecture 3 Variables 2.

Similar presentations


Presentation on theme: "CS1S467 GUI Programming Lecture 3 Variables 2."— Presentation transcript:

1 CS1S467 GUI Programming Lecture 3 Variables 2

2 Lecture Slides Lecture Slides are unsuitable for learning how to program! You just get an illusion of learning! Programming is a craft. You have to do it to learn it.

3 Today Recap of lab session
Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

4 Review of last Lab Session (Demo on Laptop)
Code layout & comments Properties of Controls Events of Controls Looking ahead to today: The strange behaviour of int and float when adding/subtracting or dividing/multiplying

5 Today Recap of lab session
Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

6 Last Lecture: How To…. declare a variable int anIntOfMine;
float aFloatOfMine; initialise a variable anIntOfMine = 5; aFloatOfMine = 5.25f; declare and initialise a variable in one line declare (&initialise) multiple variables in one line int anIntOfMine = 5; float aFloatOfMine = 5.25f; copy one variable into another one int anIntOfMine = 5, anotherInt = 3; float aFloatOfMine = 5.25f, anotherFloat; calculate with variables anIntOfMine = anotherInt; anotherFloat = aFloatOfMine; anIntOfMine = 5 * anotherInt ; anotherFloat = aFloatOfMine + 7.0f - 3.2f;

7 Variable Names Programmers can use any name for a variable, provided these rules are observed: the name must be one word only, no spaces helloWorld hello World always start with a letter not a number counter YearsOld avoid special signs such as £ ^ & % + etc. twoAndFour two&Four Try to start with a lowercase letter, then "uppercasing" goodMorning Goodmorning

8 Check your knowledge now:
Spot the mistakes / problems in the code: int counter = 2.57f; float hello=2.1f, you, there; there = hello + you; int mine=3; float theirs=4.5f; mine = mine + theirs; myFloat = 2.7f; 2.57 is a float not an int you not initialised Mixing different types. Could lead to problems type decl. float is missing

9 Check your knowledge now:
Declare two int variables called noOfStudents and roomsAvailable and initialise them to 100 and 3 respectively. Figure out how many students fit into a room and store the result in a float called studsPerRoom. int noOfStudents=100, roomsAvailable= 3; float studsPerRoom; studsPerRoom = noOfStudents / roomsAvailable; What is the content of studsPerRoom now? 33.0 (not …)

10 Today Recap of lab session
Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

11 Converting float int Sometimes it is necessary to temporarily convert ints to floats and vice versa. This is done by a process called 'type casting' int var1; float var2 = 3.99f; var1 = (int) var2; // (int) type casting here float int: stuff after the 'dot' gets chopped of ! var1 is now 3, NOT 4 !! It doesn't get rounded.

12 Type Casting Can be Useful:
Declare a double called penceAmount and initialise it to the value 255. Declare two ints called pounds and pence. Split penceAmount into Pounds and Pence and store the result in the respective variables. double penceAmount=255.0; int pounds, pence; pounds = (int) (penceAmount / 100.0); pence = (int) penceAmount - pounds * 100; textBox1.Text = "Pounds=" + pounds + " Pence=" + pence;

13 The Modulus Operator An alternative way to split a pence amount into its Pound and Pence components makes use of the modulus operator %. The modulo operator calculates the rest after division: 10%3 is 1, 20%4 is 0 The last line, however, will create an error message by the compiler double penceAmount=255.0; int pounds, pence; pounds = (int) penceAmount /100; pence = penceAmount % 100;

14 Why the Error Message? (in the last line: pence = penceAmount %100;)
C provides implicit ("automatic", "built-in") type casts (e.g. float to int) in normal assignments. This is different from Java, C++, C# (and many other languages) who require explicit ("deliberate") type casting. Hence: float x=10.0f; int y; y=x; float x=10f; int y; y= (int) x; pence = (int) penceAmount %100;

15 Therefore….. Try to get used to explicit type casting
Most programming languages use it. Makes it clear that you are aware of the potential loss of precision

16 Today Recap of lab session
Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

17 More on Variable Limits
This is how the number 14 is represented in binary in a 'short‘ integer: 16 bit 1 byte 1 byte 1 1 bit 16,384 128 16 8 4 2 1 214 27 22 21 20 = 14

18 More on Variable Limits
If we add 1 to the 14, the resulting 15 is stored like this: 16 bit 1 byte 1 byte 1 16,384 128 16 8 4 2 1 214 27 22 21 20 = 15

19 More on Variable Limits
Adding 1 to the 15 results in 16. All '1' bits flip to '0' and bit 4 flips to '1' 16 bit 1 byte 1 byte 1 1 16,384 128 16 8 4 2 1 214 27 22 21 20 = 16 = 15

20 More on Variable Limits
And this is the highest positive number that can be stored in a 'short' 16 bit 1 byte 1 byte Really? What about this one? 1 16,384 128 16 8 4 2 1 214 27 22 21 20 16, , = 32,767

21 More on Variable Limits
What happens if we add one more?

22 More on Variable Limits
Remember? Adding '1': All '1' bits flip to '0' and the next highest bit flips to '1' 16 bit 1 byte 1 byte 1 1 The Sign Bit 16,384 128 16 8 4 2 1 -32, …… = - 32,768 214 27 22 21 20 16, , = 32,767

23 More on Variable Limits
Therefore -1 is represented as: 16 bit 1 byte 1 byte 1 The Sign Bit 16,384 128 16 8 4 2 1 -32, , …… = -1 214 27 22 21 20

24 More on Variable Limits
And -2 is : 16 bit 1 byte 1 byte 1 The Sign Bit 16,384 128 16 8 4 2 1 -32, , …… = -2 214 27 22 21 20

25 The 'unsigned' Type Modifier
If we declare a short like this: ushort x = 40000; 16 bit This is a normal bit now 1 byte 1 byte 1 32,768 4,096 2,048 1,024 64 4 2 1 215 212 211 210 26 22 21 20 32, , , , = 40,000

26 The 'unsigned' Type Modifier
Likewise: ushort x = 40000; uint y = ….; ulong z= ….; Now, what about float and double ?

27 Limits of 'float' & 'double'
Floating point numbers are stored in two parts: mantissa and exponent 32 bit 'float' Mantissa (23 bits) Exponent (8 bits) Sign of mantissa (1 bit) From:

28 Limits of 'float' & 'double'
Example: 1 x 10-3 Sign of Mantissa Mantissa Exponent From:

29 Limits of 'float' & 'double'
This process is complex. Details here Full knowledge not required but understanding is. Rough guide: Floating point numbers may be 100% accurate but usually they are not (you never know). The more bits (i.e. doubles), the more likely it is that the number is accurate (size matters….)

30 Integral vs. Floating Point
Integral Types Floating Point Types e.g. short, int, uint, long,... float, double precise rounded fast to compute slow to compute "sparse" "all numbers" limited range

31 Today Recap of lab session
Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

32 A Strange Type: char char is schizophrenic (split personality)
single quotes!

33 The ASCII* Table *ASCII: American Standard Code for Information Interchange

34 The Extended ASCII* Table
*ASCII: American Standard Code for Information Interchange

35

36 Some special chars Some chars have a special meaning and can control screen output:

37 Some special chars (cont.)
Another char control characters is:

38 Today Recap of lab session
Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

39 Strings Strings are "lots of characters together"

40 Using Strings

41 Using Strings Strings are OBJECTS in C# and have Properties and Methods. Properties are just that while Methods can do things. For example:

42 Today Recap of lab session
Review of variable basics (declaration, initialisation) Type casting More on variable limits Chars Strings Constants

43 Constants Constants are variables that can't change
any more once they have been initialised. Constants are declared and initialised like normal variables - but there is something else: the keyword 'const' Look! All uppercase !

44 Why Constants ? Imagine a CAD package that uses 'inches' to
output measurements. At the beginning of the code you would write: const float MEAS_UNITS = 2.54; // 2.54 cm = 1 inch In the program 'MEAS_UNITS' is now used 400 times for calculations. 1. MEAS_UNITS can not be changed by accident in the code 2. If you want to sell the program to France where 'cm‘ is used, only 1 line needs to change.

45 Summary Today we have learned … about the limits of variable capacity
the internal bit representation of numbers about unsigned variable types (a bit) about the mantissa & exponent format how to use chars how use strings how to declare and initialise constants

46 Revision This was a lot (again) !
AT HOME Revise, then program, program, program,…

47 END OF LECTURE 3


Download ppt "CS1S467 GUI Programming Lecture 3 Variables 2."

Similar presentations


Ads by Google