Transcript for:
Key Concepts in Programming Theory

Hello, we're looking at some programming theory now. This is obviously primarily for an exam, but this will improve your practical programming. If you're someone who's not very confident with programming, learning the concepts behind your code and learning the terms and how they work and why it's advantageous to do certain things in programming is so, so important. It's often the difference between an average programmer and a very talented programmer. You know, I've worked with people who, once you examine their code, you realise they actually don't really know what's going on.

It might work, but they've copied examples, they've adapted it from... books or the internet and they don't really understand what's going on, how they can improve their code. So if you want to improve programming, learning about the theory is important generally but of course we're going to tailor this exactly to the exams.

Let's start then by talking about data types which are a means of classifying different types of data. It's a bit awkward to define because you have to use types really, isn't it? But data types, oops, the data type of a piece of data will determine properties like what operations can be conducted on it and how the data will be stored. When we look at a type of data as in an integer or a real or a letter or a character, we know instinctively what we can do with it.

With an integer you can add it to another integer whereas with a letter you can't add it to another integer. But a computer needs to have very clear definitions of what each type of data is which is what the concept of data types are. So a few you need to know about the basic primitive ones.

You can define your own data types in programming but these are the ones that are kind of built in and the basic ones. So first of all we've got an integer which is just a whole number of some definition as in maths. So 8, 35, negative numbers are integers too if the whole number's haven't got a decimal part.

A real is a number with a decimal part so it's got the integer part and a fractional part too. So usually something like 2.0 would be treated as a real, not an integer. There might be some casting involved but effectively that's a real or also called a float in some languages. A boolean is just one of two values, either true or false. representing 1 or 0 respectively so either one or two values.

A character is just a letter, number or symbol in the given character set which we'll talk about in that video. So it can be anything, a number can be a character if you usually enclose it in quotes and finally a string is just a set of characters, some of them stored as an array. I mentioned the term casting, this is just changing the type of a variable or just a literal. So for example changing 3 which is an integer back into a string just enclose it in quotes and it's no longer an integer and you can do the reverse of this changing the character or string back into or back into an integer and also changing this string 3.14 into a float version so you can do certain operations you can do mathematical operations on it as a float but you can't do it as a string so you have to convert it first these three programming constructs are the Basically the building blocks of all program code. The first is sequence where all lines of code are executed from start to finish.

So we've got a print statement here, we've got an assignment of an input to a variable, then we've got another print statement. These are all executed individually, line by line, very simple. Next we have selection which is where decisions are made that determine the execution of the program.

So here if statements are the main case statements as well. So if test equals 5, this line of code were executed, if it doesn't, it will get skipped over completely and instead test fail will get printed out. So the execution is based on decisions and comparisons really.

And the third construct is iteration which is looping. So code is just repeated until a condition is met. So these are just the loops, while loops, for loops, do while loops and so on. So here while the user input not equals the password it's going to keep asking them.

So if the user input equals the password straight away, this line of code doesn't get repeated. We need to look at iteration a little bit more in terms of two types, two classes of loops. First is count control loops, also known as definite loops.

So make sure you know they're the same thing. So count control loops or definite loops, you know exactly how many iterations are going to be, or how many times it's going to loop. So four loops are an example of count control loops.

So you know in your condition how many times it's going to loop. So here we are First of all we've got three components, three statements within this for loop. So we have our initial value, we're setting i to 1 to start with.

It's going to keep looping until this termination expression equals false. So until i is 11, so it's going to stop when i equals 11 really. And the final one is the increment expression, so this is just adding 1 to i each time. So this is quite, this is a Java loop which is a little bit more complicated than Python I think.

But the effect is it prints 1 to 10, so you know how many times it's going to iterate just by looking at the loop. Whereas a condition control loop, you don't know how many times it's going to iterate necessarily because it iterates dependent on a condition and this is also called an indefinite loop. With a count control loop you have to specify your conditions before iterative structure before the actual code that goes within the loop. Whereas you can actually, you can put the code before, so a while loop is an indefinite loop. You can also, you can put the condition before, a bit like a for loop, but you can also put the condition after the iterative structure where your statements go.

So a while loop, this iterates until this condition is false, so while it's true it repeats what's in here. Whereas with a do while loop it iterates at least once. and then we'll evaluate the expression or the condition.

So if this condition is false the first time it gets to this point, this code in the actual loop won't get executed because the condition is already true, it doesn't need to be executed. So this can be executed zero times depending on the condition. Whereas a do while loop will get executed at least once because it has to, to get to the expression it has to execute the statements first, so there's a subtle difference that you have to make sure you recognise.

We're now going to look at variables for a little bit. So variables are references to locations in the memory containing single values. So if we look at the first one, a little bit of a dodgy definition there, it does change slightly, this is taken more literally in some languages than others.

So really this memory location is paired with an identifier, so an identifier is what the variable is in a way. A variable is a construct that exists to help programmers. You could deal literally with memory values, some programming languages allow you to directly manipulate the values, the addresses in the memory, but variables are there to make things easier for us because they give a nice descriptive name.

to this location of memory where you can put some data, a single bit of data. And so we give it an identifier which is your new name. An identifier is a general term for any kind of name that refers to a memory location, like a constant or a subroutine as well.

So usually with variables the name, type and the actual location of memory remains fixed. So usually the compiler will give you a memory location and you'll specify the name of it and the type. Often not all languages do this. Like Python you don't have to say the type. You don't have to declare a variable, it's kind of implied.

But the point of variables is that the values you store to them can change during the programming execution. So you can change variables as you would have done in your programming. So you may have to, as I say it's not always the case, declare the type before assigning values. So this line here is telling the compiler basically to give some memory for this variable.

It hasn't got any data yet, but we know it's going to be an integer and know it's going to be called maxHeight. Next. declare it just to assign something for when it gets used a bit later. You can sort of declare and assign, i.e. initialize, put some data there for the first time in the same step.

So this is declaring name as a string and assigning John to it as well. So to clarify declaration is giving it a data type, assignment is storing some data in that memory location and initialization is assignment for the first time and it's really good practice to initialize your variables as soon as you declare them. with just default values like 0 or an empty string. Constants are very similar to variables except they remain fixed during the program's execution so you can't change the value of a constant during execution.

So once you assign it, initialise it, you're done, you can't change it. Not all programming languages have constants by the way, but if your language does support it and you had a program that calculates the weight of an object for example, you might have the following variable, weight equals mass times 9.81. 9.81 is g. in physics as you'll probably know. So to convert from weight to mass you need to multiply by 9.81.

So you can just, if you could, if you're supported, you could declare a constant g and you often use a keyword to say it's a constant. And often by the way by convention constants are written in capital letters but g is so ubiquitous that you might as well leave it. But if you see a variable with capital letters usually that means it's a constant or it's a clue that it's a constant. And then you can replace g, you can replace 9.81 with g in this case.

So this seems like a bit of a futile task, why would you actually do this? So reason number one is readability, how easily code can be understood. Not everyone looking at the code will understand the relevance of the random seeming value 9.81, whereas a named constant makes it clearer that this random value, this literal value, a literal is just a value not encased in a variable or constant.

A constant named g is quite clear what that number is, so that's quite important as well. Ability to update is important because If you have this value scattered about your program, so if you have 9.81, which will know all of your programming to change it for some reason. So 9.81 is the value on Earth, but on the Moon it's 1.62.

If you were to change this arbitrary program to calculate the weight on the Moon, you'd only have to change the one initial value right at the top of your code instead of having to change all the instances throughout. And on a very large project, this could be quite important. A third example, which is...

Maybe less important, especially for you to talk about in the exam, is compiler optimization, because compilers are quite technical. So one of the things they can do is something called constant folding, which is where during compile time, expressions involving constants can be evaluated. Instead of doing this during runtime, that improves performance.

Also to reduce memory space, something called constant propagation, which is where the literal values replace the constant names in the code during... the compiling process in order to save on memory space. It doesn't have to keep referring to memory location when it's running the code.

But if you do get asked this, I'll just stick to the first two points.