Presentation is loading. Please wait.

Presentation is loading. Please wait.

SE1H421 Procedural Programming LECTURE 2 Variables (1)

Similar presentations


Presentation on theme: "SE1H421 Procedural Programming LECTURE 2 Variables (1)"— Presentation transcript:

1 SE1H421 Procedural Programming LECTURE 2 Variables (1)

2 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 "Oranges only, no rubbish, please"
Variables Variables are containers for specific types of things. They have a name and can only store one specific type of object: "Oranges only, no rubbish, please" I am a container for things of type Oranges My name is orangeBox I currently hold 10 oranges

4 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

5 2 Example Types of Variables
In computing we don't want to store oranges. We want to store numbers. Here are examples of two different types of numbers called 'float' and ' int'. Spot the difference... float int

6 2 Types of Variables (cont.)
10 Only int's will fit in here! I am a container for things of type int My name is noOfStudents I currently hold the value 10 I am a container for things of type float My name is temperature I currently hold the value 28.4 Only float's will fit in here!

7 Why Different Types ? We need to put int's and float's into different containers because the computer can't store them in the same way; can't calculate with them in the same way. Within a program we therefore have to tell the computer (i.e. we DECLARE) what is the name of the container we are using (the "variable name"); what type of container are we using (int or float).

8 STEP 1: Declaring a Variable
I am a container for things of type int My name is noOfStudents I currently hold the value absolutely no idea, could be anything This is called "Declaring a variable" semi-colon? If we are not sure yet what to put into the variable we just leave the value away. In the programming languages C, Java, C++ & C# it is done like this: int noOfStudents; type name

9 STEP 2: Initialising a Variable
I am a container for things of type int My name is noOfStudents I currently hold the value 10 Once we have declared a variable type (i.e. define which type, int or float its is and given it a name) the next thing to do is to initialise it (i.e. enter a value). There are two ways of doing this: 1st option: Declare and initialise at the same time: int noOfStudents = 10; semi-colon! type name value Is this really an 'equal' sign?

10 Initialising a Variable, 2nd option
int NoOfStudents; Declare first type name value I am a container for things of type int My name is noOfStudents I currently hold the value absolutely no idea, could be anything Then initialise I am a container for things of type int My name is noOfStudents I am now holding the value 10 noOfStudents = 10;

11 Check your knowledge now:
1. Declare a variable of type int and call it 'myVar': int myVar; 2. Now initialise 'myVar' to the value 20: myVar = 20; 3. Declare and initialise a float variable called 'yourVar' to 20.4 in a single line: float yourVar = 20.4;

12 Calculating with Variables
After declaration and initialisation we can manipulate the content (the value) of the variable as we like. Example: add 5 more to the 10 already there: noOfStudents = noOfStudents + 5; I am a container for things of type int My name is noOfStudents My previous value was 10. 5 have been added. That makes: 15 5

13 Calculating with Variables
Likewise we can subtract: Or multiply: Or divide: noOfStudents = noOfStudents - 5; noOfStudents = noOfStudents * 5; noOfStudents = noOfStudents / 2; 1. Compute the right hand side of the “equation” 2. Copy (assign) the result to the left hand side “Order of service”:

14 Check your knowledge now:
Spot the mistakes in the code: int counter = 2.57; float Hello int my Counter; myFloatVariable = 2.7; int 5RedRoses; int good, morning, to, you; 2.57 is a float not an int ; missing, capital H not a single word type decl. float is missing don't start with number correct !

15 Setting to a New Content
Setting a variable to a new content is the same as the process of initialisation. Example: setting the noOfStudents variable to 100 I am a container for things of type int My name is noOfStudents My previous value was 15. Now setting to 100. The previous value disappears: 100 15 Step 1: old value out Step 2: new value in noOfStudents = 100;

16 Copying Variables Contents
We can also copy the content of one variable into another variable. But caution: they need to be of the same type ! Before copying: I am a container of type int My name is tempValue I am holding the value 100 I am a container of type int My name is noOfStudents I am holding the value 15 I am a container of type float My name is floatingValue I am holding the value 100.0

17 The Copying Process The copying process: noOfStudents = tempValue;
destination source After copying: I am a container of type int My name is noOfStudents I am holding the value 100 tempValue

18 Notes on Copying The old value (the 15) has disappeared completely and has been replaced by the new one (the 100). This is a copying process NOT a transfer since tempValue still is 100. noOfStudents = tempValue; I am a container of type int My name is noOfStudents I am holding the value 100 tempValue 15

19 Calc 'n' Copy The copying process can involve more than one other variable of the same type: Example: If we have declared and partially initialised the following variables then this calculation will put a '50' into NoOfStudents. destination source (calc. first, then copy) int totalStudents = 5000; int classRooms = 100; int noOfStudents; noOfStudents = totalStudents / classRooms ;

20 Calc 'n' Copy cont. The copying process can also mix variables, numbers and mathematical operations. Example: destination source (calc. first, then copy) all floats, don't mix types! float pounds ; // how much we get in £... float euros = 50.0; // ...from unspent holiday dosh float comPerc = 0.95; // 5% commission and ... float fee = 2.0; // ….a fee on top of that, arrgh pounds = euros / 1.22 * comPerc - fee;

21 Check your knowledge now:
Declare two float variables called pencePerLitre and inTank and initialise them to 1.35 and 45.2 respectively. Declare a third variable called toPay. Calculate the amount to pay and store it in toPay float pencePerLitre=1.35, inTank=45.2; float toPay; toPay = inTankNow * pencePerLiter ; Spot the 2 mistakes in this line!

22 To Infinity and Beyond ? How full is full?
How much can a variable hold then? I am a container for things of type int My name is noOfStudents I am now: completely full !!!! How full is full?

23 To the Limits…. How much can we squeeze into an int? Quite a lot but there is a limit. Usually *: 4,294,967,296 values (that's 2 32) int's come as positive and negative values (e.g. 100 or, say, -5000), so we have to split this value into two equal halves and arrive at a range between - 2,147,483,648 and 2,147,483,647 This may look a bit unfair for the positive side (there is one less) but since '0' is included on the positive side, it is in reality nicely balanced. * ”Usually” means: it depends on C compiler and hardware ability

24 Variable Types and their Limits
Example program: SizeOfVariables.c A lab exercise

25 Accuracy of float and double
floats are stored in 4 bytes (32 bits) which is enough to hold approximately 7 significant decimal places (sum of those before AND after the dot) e.g.: doubles are stored in 8 bytes (64 bits) and can hold approximately 15 significant digits. e.g.: More on this next week

26 Reading Input from the Keyboard
A lab exercise

27 Revision This was a lot ! You need to: revise the material
study more examples practice Visit the SE1H421 Blackboard ‘Learning Schedule' page and use the 'Self Study' material for week 2.

28 NOW: Demo Session Variable basics Using variables Good coding style
Allowed names Declaration, initialisation Using variables Calc. using assignments Size of variables: sizeof( ) Capacity of variables Reading from keyboard: cin >> Good coding style Comments: multi & single line Indentation Meaningful variable names

29 Summary Today we have learned how to... declare a variable
initialise a variable declare and initialise a variable in one line declare (&initialise) multiple variables in one line copy one variable into another one calculate with variables read input from the keyboard

30 We also know where we can revise all this!
END OF LECTURE 2


Download ppt "SE1H421 Procedural Programming LECTURE 2 Variables (1)"

Similar presentations


Ads by Google