Lecture 2, COMP1511 - Programming Fundamentals 21 Term 3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
do hello everyone 1205. nothing's crashed i think i think we might have some success today in getting off at the right time um hope everyone's been well um and i'm super excited to get back into it so we're gonna start where we left off yesterday uh a quick recap so yesterday we had some welcome and introductions we talked about our course admin um and talked about where to get help as well we talked about how comp 1511 works and where we oh i am right on top of what's going on i'm sorry let me move myself it's not really that important that you see me on camera um so yeah okay that's better okay we also talked about uh the best approach to learning programming we talked about what is programming and we also talked about what is linux and working in linux then we kind of started uh going over our first look at c um before it changed to 12 o'clock and my computer decided it had had enough and just kind of crashed and burned so hopefully we can now come back to that program i know that a few of you would have gone off and done a lot of that sort of introductory hello in your labs yesterday so yep let's let's talk let's talk let's talk about the progress sorry i just saw someone comment if there's an audio and i thought oh everything i've talked about is now missing um okay what we're going to talk about today today is going to be a super exciting day we're going to do a quick hello.c and then we're going to get into it we're going to talk about some variables we're going to we're going to talk about how we're storing them maybe some maths in c because who doesn't love maths on a wednesday and then we're going to talk about conditional statements so running our code that should say hour not out running our code based on some sort of condition being met or us asking some sort of question and based on the answer we're going to run a particular piece of code or not so we're going to talk about if statements and how they come together to give us more you know flexibility in our code as well and choosing what code we want to look at okay also when we do code in the lecture you can find it on this link so any live code in the lecture will also appear in here which means that you can you know work alongside us in the lecture or you can look at what we're doing so every time i save it will update at that link and you should hopefully be able to see that piece of code and that's really as we go on you know onwards to the subsequent weeks the code will get a little bit harder there are going to be a little bit more code so sometimes you might miss something that i've said you might want to just update your code from that but it also allows you to go back and have a look at what the code looks like that we've done in the lecture okay let's have a look at what we started discussing yesterday so yesterday we started to look at our first program it was hello.c or whatever you want to call it it really doesn't matter what we call it and this is really you know your hello world is the standard way to introduce you to almost any programming language um and so basically um so we've got a little piece of code here this one will print out into the terminal um and we will look at it in a second what it will print out so we've got up the top in blue we've got our lovely header and i think yes i went into it so the little header the header is for humans to read remember that anything with two slashes is something that the compiler just skips over it says this is not for me this is for a human i don't want to read things that are for a human and basically what it does is that it gives a little title of what the program is perhaps what it does it says your name maybe your student number and the date so it's exactly the same as doing a title for an essay you're doing a title for your piece of code it's because you don't expect that everyone knows what on earth you're doing just from um you know just just because they can read your mind because no one really can read your mind okay and then after your head up comment which we include at the top of every file that we write we've got our hash include tag and the hash include tag is a special tag that we use for our compiler um and what it does is it includes something so it's like you've headed out to the library and you've borrowed some functions from that library so if we have a hash include um a hash include there's a whole bunch of standard libraries that come with c and those standard libraries have in them things that we can use that we don't have to write the code for ourselves so things like printing out to our terminal or scanning from our terminal which we will talk about later as well all of those things are included in those special libraries and what that means is that we don't have to write the code for it we're just borrowing that code so it's really it it really allows us to you know write less code and also allows us to um use this code that's already available we know it's got no issues why wouldn't we use it it's something that is sort of well known well out there and we're going to look at a few different standard libraries that are available to you but the one that is the most basic one is this standard input output.h that's what stdio is and it really allows you to do input and output um okay so when you hash include stdio.h on top of your file you are telling c to take the code from that standard library that you're going to be using it in your code as well and if you try to compile without it which we will do in a second you will see that it will not be able to do it oh my video is in front of the box i just feel like all i have to do is just keep moving myself around apologies um okay okay then after that we go into the main function so c starts reading at the main function so even if you have i know you you can hear me referring to function function function function and you're going what on earth is a function it's just basically different little pieces of code and each function or a different piece of code is like a different it will perform a little a little bit of the code that you need to do it just helps us to break things apart so it's not in a huge in a huge 1000 line essay it's like a chapter of a book almost so you're breaking up um so you're breaking up your book into chapters um so c always starts reading in the main function so whatever says this main bit that's where it will go into and you will always see that each function or each chapter starts with uh something here and then it will start with a word here and then something else in brackets and what it's doing is it's telling you a few little things when it does that uh the main or the item that you'll see in black when you're doing it in your g edit is the name of the function and so every piece of code will have a main and then your int the thing that you write at the start it doesn't have to be an int what it is saying to us is that the return type will be an integer and we're going to learn more about the different variable types in this lecture so you will understand more that you can return different things so what that's saying is that our function will give us back an integer when it finishes when it finishes running and then inside here you can see this lovely thing a void uh it literally is what it means in english as as well in this case um so void is like a you know space of nothing and what it means in this case is that you're not taking any inputs into this function so you don't need anything to run this function with there are no inputs into it so the first word is the output what you get from it then you get the name of it and then you get what you input into it then you see these lovely curly brackets okay and the curly brackets are fairly important in that whatever is between the curly brackets is our piece of code or a set of our program instructions okay so what we get is this printf function this is a function that we borrowed from that standard library that we included at the top of the file and what printf does is it's able to take whatever is in your apostrophes and it takes it out onto the terminal you'll see that this lovely slash n is here and we'll talk about that in a second because it's in a different color as well um and then we finish each statement that we make with a semicolon and this is going to be the bane of your existence if you write many lines of code sometimes you'll try to compile and you will almost certainly always be missing a semi-colon what it will also do is if you go on to write reports after this or any sort of document you will over litter them with semicolons because you just you know get so used to putting semicolons at the end of everything and then because we've said that our function returns something this function is going to return a zero okay now as a standard sort of consistency thing in c when you return zero that means that the function has completed successfully and it has run successfully and so uh all it's saying is that i'm done uh everything finished well things are going well okay and so when you specify something here a data type that's when you're going to return something so don't be confused i'm about to go to the screen and actually type it out so that you can see what's going on as well okay and we'll talk about it soon so don't worry um we introduce things and then i will go on to type them out as well so let's let's have a look at it typed out so i'm going to switch my screens to my vlab okay so something that you and someone asked can we replace zero with any integer you absolutely can you could have anything in there as long as it's an integer but it depends what your function is doing as well in that what you're going to return the xero is something it doesn't have anything to do with binary it's just kind of like a standard way to say this has finished successfully okay and usually you will return a one if it hasn't run successfully but for now we will just use zero and i'll show you how to use or return different things as well so return zero just really means you've it's finished and it's all finished successfully there were no issues in finishing it okay so i'm going to go inside here and i'm going to start a file called hello.c i'm going to use my g edit and i'm going to type in g edit to open the editor and i'm going to give the name of the file i've called it hello.c you can call it anything you want if you're doing a c file you have to have the dot c extension just like a word document has a dot doc and then our ampersand which means that my um the ampersand means that i will get continue to have control of the terminal even after i open my g editor so as you can see i can still have control and can still do things in terminal even though i'm running my my g editor simultaneously so first things first i'm going to write my header file and i'm going to say what the program is doing i'm going to say my name and perhaps i will even say the date september 2021 okay then the first thing i will usually do is i will hash include some libraries and in this case because i want to use a print f i want to print something out to the terminal uh i'm going to use the standard input output library now if you're wondering why it's printf and not just print printf just means print formatted okay and there are some really strange sort of you know ways that we call things so don't much worry about it but that's what it you know kind of does okay then i'm going to go into my int main void so i'm creating a main function and what i've done here is i have the words int which means i'm returning an int i've named my function main because that's where i'm starting out and i'm telling it that there is nothing going into this function okay then in between my curly brackets is where i'm going to write the code so i want to print out to the library so i'm going to use the printf function okay now the printf function inside it if you put in apostrophes what you would like to actually print out i think i'll print out hello uh close my apostrophe and don't forget the semicolon and now i'm going to return zero to say that yep that's probably run successfully that looks okay okay so what i can do now is i want to try it out to see if it actually does work or if anything is you know or if anything's the matter with this so if we run it we're going to use a compiler to run it because we want to translate what we have in our language c here into a language that the computer can understand so into ones and zeros and we want to create a file that we can then run um i haven't included a new line yet but i will next time i want to see what it does without the new line and then i'm going to include a new line after that okay so our compiler is called dcc so i'm going to call dcc and then what i will do is i will use this little flag here called dash o and what that flag does the dash o is it's telling um is it's telling the compiler that i want to name my output something okay otherwise the output has a really yucky name and it's all the same for everything so using that dash o allows me to tell the compiler that i want to call my output something and i'm going to call it hello so what i've told my terminal is that i want to run dcc which is the name of the compiler i'm going to use the flag dash o to say i want to name my output something i'm going to name my output hello and then i've got to tell it obviously what file to compile and my file is called hello.c so that's the file that i'm going to compile so if i run this now you can see that it's produced absolutely no um it hasn't given me anything um no errors so what it means is that it's compiled successfully now if you want to see what happens if you do have an error i'm going to comment out this standard library and now i'm going to compile it and if you notice if i press the up arrow it's just going to recall the command that i used last it doesn't mean that i can type that fast if i run it like that with my standard library commented out what you will see straight away is that i get an error message because it actually does not know what printf is without referring to this library that you include and your warning or your error message is actually telling you what's going on so it's saying this printf is not is not recognized we don't know what on earth it is and it actually even tells you to include stdio.h so it's telling you which standard library to include uh when you're running this piece of code to make it work so sometimes those error messages are quite useful and we'll talk more about error messages as we go okay so i've compiled it again and what i am going to do now is i want to be able to um actually i want to be able to run what my output is to see to check that it is actually working um as i want it to work okay so then to run a piece of code after you have um after you have compiled it you know that you've called the code hello so to run it you do a dot slash and then you type the name of the program that you want to run okay so this is what lets us run that program but it has to be in the current directory okay so if i run it now you can see that it's printed out this hello with an exclamation mark which is really neat but then quite um in an aesthetically unpleasing way what's happened is that it's it's sitting here with my you know everything is happening all at once so i really want to be able to i don't know i want to be able to put an enter or a new line after this hello to move my terminal prompt to the next line to make it look a lot neater oh my video again is in the way i wish i could just turn this stupid video i could just turn the video off you know what i'm going to turn the video off just whilst i'm here because my face is blocking the whole thing which is you know and you want to see the code not me so i've just i've just turned my video off whilst i do that okay um there we go it's turned off so now you can see what's going on okay so we want to be able to move this terminal prompt onto the next line which means that we really want something to follow her something to follow that hello and this is where i'm going to tell this computer to please give me a new line to do a new line you do this slash n okay and that's what gives it that's what tells it please go to a new line slash n is is making it go to a new line there is other things that you can also do you know there's there's you know tabs and other breaks that you can install but the one that we mostly focus on um is that we um do the slash n which is the new line so now if i have the slash n in there and i recompile my code again i'm just going to clear this so that it goes back to the top of the line and what i'm going to do is i'm going to recompile it now okay and it's compiled successfully no errors so now i'm going to do dot slash hello because i have told the compiler to name my output hello so to name my program hello and then i'm going to run it and now it says hello and as you can see my terminal prompt has moved to the next line so what that new line has done is it allowed me to move things around okay um okay so that is a very simple hello world okay and i see there's a lot of um you know a lot of questions being asked and i know people are uh well people tom and shrey are doing really well answering all of those questions so i'm going to uh tom and trade you have anything to add to hello world no okay um nothing to add very good okay so then let's maneuver on uh it's worth noting that this is just a very standard introduction to the uh sasha we're a few seconds behind you because we can't hear you in our call currently um but it's just worth noting the the in main void you can just pretty much ignore the syntax for now um and just imagine that when the compiler sees the word main it sees that all right this is where we start reading the code from um don't worry about the specifics for now uh just worry about what's inside the brackets for now i'm going to hang on i'm going to give you code um i was trying to mute tom before because he was punching loudly on his keyboard and i forgot forgot to unmute him okay let me yeah well yeah i muted myself so as not to hear tom okay let me let me give you the access to that piece of code hang on cool don't um don't take to heart what i'm writing now so if you're wondering this is just making the code accessible to all of you and you definitely don't need to know about anybody yeah don't look at don't look at any of this ignore this ah and my bad typing as well ignore it um dcc dash oh hello hello see that is just saying all right we have a file called hello.c hello.c is our human code right this is where we write the code that we can understand when we use the compiler we need to turn it into we need to translate it into code the computer can understand um and so dtc will translate from the hello.c and output to hello so that dash o means output to a file name whatever you put after the dash o so in this case we're saying dash o to hello so we're going to output the translated machine you know program to hello then when we dot slash hello we'll say okay now run hello so you can think of dot slashes as run and you can think of hello as the machine program which we told dcc to put the machine instructions into and it's also worth noting uh there's a hello.c tilde so that little squiggly line that's a backup file which get it makes and you can completely ignore it you can remove it if you want to if you want to leave it around that's fine but you don't need to worry about it at all yeah okay so uh hopefully you should now be able to see the hello.c um and that's a very simple program that basically come you know that basically says hello and outputs it to our terminal okay so that was really exciting you've done something you've exited to terminal you've given it a little bit of a terminal so it'd be good if you guys can try it yourself and also what i want you to do is to really practice making mistakes in there don't be scared to you know you know return to see what it does you know return nothing leave out return try out different permutations and combinations because you can see what kinds of errors it spits out at you and what it's actually doing yeah so and yeah okay let's um let's go on okay so now i'm going to talk about a few things um and what i want to do is i want to introduce you to a few uh concepts so don't worry if you don't quite understand all of these this is just a little overview to introduce you to these to this type of thinking but it's not it's not yet important that you understand exactly what i'm talking about so basically uh you know when we're writing these programs they're not just magically living uh you know in somewhere in fairyland um it's they are living somewhere in memory of that computer because if the computer can't remember things then obviously it can't really do anything for us because it needs to keep things in memory to actually run things so the computer memory is actually just a big pile of on and off switches it's made up of ones and zeros which is exactly what it is and those switches are bits and a bit is the smallest possible unit in computing so it's a choice between a zero or a one okay and that's uh that's the the smallest choice that you can make in computing now that's a very small piece really so what we do is we often collect these together into a bunch into eight bits and that's called a bite um so you know always if you're feeling peckish you want eight bits you get a bite um but so eight of these on and off switches give us a bite and we will talk more about how that actually impacts into memory um as we go and as we move through the course as well so what does this look like okay so there's a few things and and please don't be alarmed uh by you know this uh sort of don't be alarmed by seeing this diagram as well um it's not you know you don't need to understand it right now but i think uh sometimes seeing it like that i don't know it can be can be nice so what happens is when we execute code um there are two important pieces of hardware that play a part okay so i don't know if you guys have heard of ram which is your random access memory so when you when you uh run your code the code loads into the sort of fast data handling it runs into it it goes into the ram which is like your short term memory that you have as a person and what happens is the cpu actually processes the instructions it performs the basic arithmetic but the ram is responsible for keeping track of the data that you need to run that piece of code and what that data includes is it includes your actual code instructions any sort of global variables and that includes a whole bunch of variables that you create in your code and those variables might go into something called a stack or a heap you don't need to worry about it for now but just know that there are sort of you know different places where things can go in memory and if you've ever heard of the website stack overflow it kind of gets its name from from this sort of stack overflow business and so what happens is when you know when we have variables when we create variables they get loaded up on here either stack of the heap so as you can see the more variables you have in your program the more that stack might grow or the heap sometimes they will crash and they will have a bit of an accident which means you will overflow the computer's memory and there is not enough to go around and that is something that we will look at as well as we keep going so don't worry if you are not quite understanding this diagram yet that's okay i will keep bringing up this diagram i just wanted to give you a little bit of a first look at what this diagram looks like okay so what is actually a variable now i would imagine that most of you have done maths at school and you all have done algebra and you all use letters to hold some value that might or might not change over time when you did your functions and that is kind of a similar way in what a variable is a variable is something that changes its value over time it can change its value it doesn't have to but it can and we'll talk about what happens if you do have a variable that doesn't change its value and each time we have a variable we use up a little bit of space okay so we use up a certain amount of bits to represent something and how much space we're using up depends on what kind of variable we have you know is it an integer or is it a floating point number so is it a decimal what is happening so each variable will have a different amount of memory that it needs to to live so today we're going to learn about three types of variables we're going to start out with three different data types so they're called data types because they define what kind of data there is stored in that and what c does is when you have a variable you say what data type it is because then when the computer runs your code it knows how much memory to allocate to a particular variable because of the type that that variable is so the three types of variables are an int and an int stan stands for integer uh an integer is a whole number so anything that doesn't have any decimal points okay zero one two three keep counting on and on and on okay we will also look at a char okay a char is a single character so it can be something other than letters and i'll show you all the different things that it can be but the easiest thing to remember it as is it's just one single character and a single character will usually associate with something as a simple letter and then we're going to look at doubles okay so a double is a floating point number and i'm blocking you again honestly i really need to stop blocking things with my face um so a double is a floating point number and what that means is that it's got a a whole bunch of decimals behind it okay now each of these has a different number of bytes that are allocated in memory once the program is run and how we write code also depends you know on using these in the right ways so that we're not taking up a huge amount of space that we don't need to have um or that we are sort of trying to understand what we actually need from the problem that we're encountering okay so three types of variables ins chars and doubles let's look a little bit more about on variables and how to work with our variables as well and you would have seen that int as well when we had the int main void the int there is saying what data type is to be returned so when we are returning 0 0 is an int and that's the data type that you have returned so naming our variables is quite important okay it's it's you have to remember that you're writing your code for you know for humans basically so someone else will come along and add an industry as well you're not really just you're not a single man team or a single woman team you are writing your code usually as part of a team someone else has to be able to come and look at your code you might leave the job someone else will come onto the job and what that means is they need to be able to look at your code and hopefully understand it without um you know seeing you know a game of thrones esque chapter book of you know documentation of what that program does so we really want to have really nice uh variable names because they make our programs a lot easier to read so a name should have a quick description of what that variable actually is okay so instead of using just single letters say a b c d x y um use the actual descriptor of what that variable is holding if it's holding the answer have it as answer if it's holding a diameter have it as you know the variable name as diameter because what that does is it makes your code easier to read it means that i don't need to refer to different things to be able to understand what's going on if someone tells me diameter is equal to 3 instead of b is equal to 3 i'll know straight away diameters there is a circle somewhere and this is the diameter of off of it because if i see b is equal to 3 i don't really know what that means where does b fit into fit into the whole thing so we always use uh lowercase letters to start our variable names okay and these are these are sort of important for style we always use lowercase letters to start our variable names and c is also case sensitive that means if you have answer with a capital w inside it and answer with no capital letters that's two different variables okay so you have to be a little bit a little bit careful with what you know with how you are doing it as well there are also some words that you cannot use as variables and that is because they are reserved word so things like return as you can see that i use return in a function to return some sort of value and that means i cannot use it because it's already used by c for something else i can't use int i can't use double i can't use char and so on and so forth because these are all already taken up by they are reserved by something that c does itself as well if you have multiple words together you can split those with underscores okay so you can say long underscore answer and keep it going as that so it is okay to have longer variable names when you're writing code because those longer variable names play an important part in making your code easier to understand okay so someone else has to be able to come along and read your code someone else has to be able to come along have a glance at your code and and get an idea of what your code is doing okay so let's talk a little bit more about each of these different types of variables that we have just kind of introduced you to so first of all we have first of all we have an integer and the data type is called an int okay and int is a whole number it's got no fractions it's got no decimals and it most commonly uses 32 bits that's four bytes okay however you cannot guarantee this okay because on different system and smaller embedded systems it might use less than 32 bits so you have to be careful in what you are doing um but if you have those four bytes if you have those 32 bits going um you will have exactly 232 different possible values the maximum is very large but it's not infinite okay you will because you have this restriction in the amount of memory that an integer can take you also then have a restriction in how many numbers you have so even though okay even though numbers really can go to infinity and beyond they can't when you are programming and this can lead to you know a lot of issues as well um so what happens is uh that you can go from oh my gosh i'm sorry see didn't do superscripts um it should be superscripts yes uh it didn't i said 2 to the power of 32. so it should it should be giving us superscripts um but i will fix it in the in the lecture slide and upload the new lecture slide so i did it on the on here but not on the actual slide and so here as well that should be 2 to the power of 31 and then minus 1 okay so we've got our integers are going from 2 to the power of 31 onwards to 2 to the power of 31 and -1 okay you don't need to remember this okay you don't need to memorize that it's that's the range of your integers but it's important to know that your integers have a range okay so there is a limit to how many integers you can specify now does anyone know the reason why it would have a minus one here instead of just being more symmetrical so instead of being from minus 2 to the power of 31 to the power of 2 to the 31 yeah so someone has answered yet because of the zero the zero gives us an extra value that we need to account for well done ah i didn't mean to go i need to go forwards okay so uh let's do characters okay characters are quite funny in um in c because characters are actually stored uh as integers so just to you know confuse you in this beautiful love story of characters and integers that go hand in hand basically what happens is that in c characters in order to represent characters computer maps each integer with a corresponding character using a numerical code and that numerical code that is used is ascii um i don't know if you've ever heard of ascii ascii is the american standard code for in information interchange and it kind of came about uh quite a while ago um a bit of trivia and there is a whole bunch of things in here that really no longer we don't really need anymore but um it still is the way that characters are mapped in c um so if you guys know you can't see because my face is in the way again let me move my face out of the way there we go um and so what happens is that you have this lovely ascii table you can look at it whenever you want and by typing ascii in your terminal i've got a flag here i've got a dash d what that does is it only gives me the decimal values for the ascii so it creates a neater table if you type ask here we'll give you if you give you the hexadecimal values as well for ascii so it's not as you know it creates a longer table but as you can see each letter of the alphabet is mapped to a number so a capital a is also mapped to the number 65. so that means when i have a char and that char is a capital a what it's storing is actually the number 65. so a single character variable and this is more how well i'll talk more about why it does that i think in a further lecture so as not to throw you all off but it has to do with how our memory works as well so if you're assigning if you've got a data type char and you are assigning a value to it you will put that value into single apostrophes into single quotes rather and that allows so what will be stored is the number 97 which is associated with it but the character for it is a and you can go between one and another um and you know you can sort of uh pass around whichever way you want to do it so for a capital letter a the character is a big a and it the interstate is 65 but what it allows us to do is because they're stored as ins we can actually do maths with our chars okay so if we want to go to the next letter we can just add one to that chart and it'll take us to the number 66 or the letter b and what that means is that you know it allows us a little bit of flexibility in how we can play around with the characters and we can move around the characters by adding or subtracting from them and i believe the lab next week will allow you to practice that skill as well okay and then the last data type that we will learn about today is called a double and a double is a floating point number what it means is it's a decimal value and that means the decimal point by floating point the point can float around and it can be anywhere in the number so that's all it means okay so that that decimal place can go anywhere in that number now it's called a double because it is uh double the memory that an integer holds okay um so whereas an integer is usually four bytes then we have the double is eight bytes because it's double that size and it's worth considering also that your double even though you can have you know infinite number of decimal places actually computer memory cannot accommodate this infinity to beyond and beyond sort of aspect so you will get some issues based on that that we will talk about um in a second there are many more data types in c okay but those are the three that we're starting with today but there are many more data types because you need to be able to do a whole bunch of things and to do a whole bunch of things you need to have more data types as well so if we look at how we're going to declare variables and initialize them so when we talk about variables we need to be able to tell the computer that we need a variable so we need some space for a variable that means and we also can initialize it by giving it an initial value as well so if we have a function and we want to declare a variable to start with what we do in c is we tell we tell our computer what data type we want that variable to be and then we give that variable a name so when this program is run c knows straight away how much memory needs to allocate to this variable because it knows what data type this variable is so let's say we have an answer what that means is that we have just created we have just created a variable cold answer and it is off type int okay after that what we can do is we can give that variable a value and that's called initializing it so when we initialize it we're giving it its first value or we're giving it its initial value we no longer need to specify what data type it is because we've already specified what data type it is here and we can refer to it by name so we just say answer is equal to 42 and finish it with a semicolon and what that does is it assigns the number 42 to that variable now one thing that is worth to note is this equals sign here you guys are used to coming from maths and doing maths for a long time you're used to the equal sign meaning equal to now in this case one equal sign does not mean equal to it means that i have assigned a value to something so what happens when you assign a value is you look up here on the right hand side and you see the values 42 and you assign it to the left hand side which is the variable answer and equality is set in a different way in c which we will look at today as well okay and then you can change the name of that variable as well if you like so i can call the name of this variable again answer and i can assign the number 7 to it so now this variable holds the number 7. now when you get you know all all cool and excited you can also do it all in one line you can declare and initialize a variable together so i can say straight away i've got a data type int so something is of type integer it is going to be called answer 2 and i'm going to assign the number 88 to it so i can do it all in one go i don't need to do it over two lines i can do it all together as well and if you notice that the names of the variables again they are in small letters and they are also describing what we're getting here as well so we're getting an answer okay what we can do is we you know it's great to have a variable but what can we do with our variables okay we can do many things with our variables obviously we can do some maths with them we can print them out we can we can do a whole bunch of things with them now printing out to terminal we can print them out using that printf that you have only just used and you have used it to print out just a sentence but you can actually also print out a certain variable names so it's not just for specific messages it also allows you to print out variables as well if you want to print out a variable you use something called a format specifier okay and there's one for each one of our different data types so you can use a different one so as you can see i've got a variable here the variable name is number it's off type integer and i've assigned the number 13 to it and now if i want to print it out then i'm going to use the printf function and i'm going to say my number is i can say anything i want before it but to actually make it display the number i'm going to use this percent d and when you use this percent symbol followed by some characters you're letting the compiler know what data type you actually wanted to print so in this case you wanted to print a decimal value okay which is why you're writing percentage d and then you put a comma and you write down what the name of the variable is to finish it out so in my case the variable name is number and then as before i'll close a bracket and i'll do a semicolon to finish my statement and what this will do is it will print out the number and we will put all of this together in code after these slides don't worry we're just going over it now and then we will do it in practice as well um what's happened here i'm missing i'm missing or i'm missing an image this is very annoying um so ah i'll show you how to do this in code but basically what i was trying to say here is that the variables will match the symbols in the same order as they appear so what it means you can have three percent d's in any one line and then you can just have three variable names and it's it's all on the order that they're listed that's the order in which case they in which they will appear so for example here actually you can see there's two different variable names and there's two different variable names here okay so that was a percent d so that was just your normal integer but now let's say we want to print out a double so for a double we use a percent lf okay and the lf stands for long floating point number okay so if you use lf then you're going to get a decimal printed out okay so remember that we have to be very prescriptive when we tell the computer what to do and that you know that extends to telling it what data types we're going to be printing out in c as well so in this case i have a data type of int it's called diameter so that's my variable and i've assigned the number five to the value of diameter to the variable diameter and i also have a data type double and that means it's going to be a decimal i've given it the name pi this variable and i've given it a value as well and so now when i print them out then i can specify that 1 is a percent d so 1 the diameter is a decimal value whereas the pi value is a decimal so it's a long floating number and then in the same order that they appear in my text say percent d percent lf that's in the same order that i will use the variable names after my printf to type out okay so what about a chart and i've left the chart to the end because the char has some a little a few little specific things going for it because a char can also be a lovely int it can be expressed both ways with a percent c or a percent d okay so you can either be printed as a character with using a percent c or it can be printed out as the number that it indexes to on the ascii table so as a percent d so for example let's say i have a data type char and i've called my variable letter and then i've assigned the letter a capital letter a so i've put it in single quotes to assign it to the variable letter so then if i'm going to print out i printed out here the letter percent c has the ascii value of percent d what this will print out for me and if you notice here i've referred to it if i've got two format specifiers then i need two different variable names here but as you can see the variable name is the same the first time that variable name is going to print out a capital a for me because that's what i've assigned to it and the second time it's going to print out the index um that the letter is actually um that actually that that it indexes to on the ascii table so your character can be both a percent c and a percent d depending on what it is you're trying to do or you're trying to see in your code as well so characters are quite quite um quite tricky in that way okay so that's fantastic we've done you know we can print out to our terminal that's great you know what [Music] what you know what do we do now it would be useful to also scan in from terminal so it would be useful to take some input from the user wouldn't it it would be useful to know perhaps to change up what you know those variables are and what the user can do so there is also a function inside that standard input output library called scanf so there's printf which prints out to terminal and there's scanf which picks something up from terminal so it allows us to take in something that the user will specify to us okay so if we read an input from um from the terminal we also use the same format specifiers depending on what input we want to take in okay so either it's going to be a percent d a percent lf for a decimal or it's going to be a percent c for a char um the same way that in printf we specify what data type we want to print out in scanf we specify what data type we want to scan in so as you can see i've got a i'm declaring a variable the variable data type is int and the variable name is input and then i'm going to give a prompt to the user so please type in a number this is going to then allow the user to type something in and for me to take that number and store it in some sort of variable so i've created this variable here called input okay and that's where i want to store the input that the user gives me so what i do is i use the function scan f and i specify in apostrophes here that i'm going to scan in a decimal number so i'm scanning in a percent d and then i'm going to put a comma and the same way as in printf we specify what variable name we're printing i'm going to specify what variable i'm storing it into okay i'm storing it in a variable name called input now you'll notice that when you use scanf as well you have this lovely ampersand here the ampersand is very important in the scanf function okay you will have to put an and so an ampersand you will have to put it in front of every every variable that you're scanning into what the ampersand symbol means is that it means go to the address of that variable and put my variable in there that i've just scanned in okay so if you guys remember when we create this variable name input it goes somewhere in memory somewhere i don't know where it's gone so when i'm scanning something in i kind of need to know not just the variable name but i need to know where on earth can i find this variable to put this value into it so all that you're doing with that ampersand is you are telling the computer please find where this variable is located and plop whatever was entered in here into this variable okay now we're going to learn a lot more about that ampersand and their address off and traveling to the address of our variables later on in the course but for now just know that when you're doing a scanf it's not enough to just put in the name of the variable you need to tell it to travel to the address of that variable so you always need to have this ampersand there um okay and then the similarly if you want to read a double you will declare a double data type and you'll give it a variable name again i might type in a prompt for my user and then what i'll do is i'll scan in but this time i'm going to scan in a percent lf so i'm scanning in a decimal point and you notice again i've got the ampersand here in front of my variable name and that means that um and that means that we're you know going to that and putting the variable in there someone's asked a really good question in the chat how come we can get away without using the episode for printf um so that's a really great question and the reason for that uh there you go shrey is saying to treat it as as magic for now um but yeah it's because tom's just answered it it's because when we're doing scanf we actually want to put a value into that variable whereas if we're printing it out we know what the value is we're not changing anything we're literally just saying print out whatever it is but yeah more details coming don't worry we're not going to leave you hanging okay the lovely char our poor little char um so if you want to scan f or if you want to read in a character you'll need to declare that character by using the key word char okay so even though it's still stored as an ascii value um when you printf that variable or whatever else you need to declare it as a char for scanf to be able to read it if you want to scan in a letter or a character okay what if a variable never changes well i mean if it never changes it's not really a variable then is it if it never changes it's probably going to be a constant so if you have something that never changes throughout the course of your program you're better off putting it as a hash define okay now a hash define usually goes just after our hash include and we usually name our hash defines with capital letters so you can see if i hash define anything i'm going to use capital letters because then when i use it in my code by referring to it with this name i will know that i am actually talking about using a hash define that's listed at the top so it's a style guide that lets us remember that it's not a variable it is actually a constant okay so what we do is we hash define we write the name of whatever we want that constant to be and then we give it a value of what value we wanted to take and we will be putting all of that into practice so okay so let's quickly talk about some maths we're heading towards a little break there's only a few more slides okay to go before break time sorry i have run a little bit over i'm still getting the hang of um timing for these lectures sorry this is the first time i'm giving um this lot of lectures so i'm still kind of giving understanding where timing is going okay so um let's talk about some maths okay so there are some quirks uh sometimes with maths and in c and that's because we don't have an infinite space for our numbers and that's usually where the quirks arise but you can do some very basic maths uh you can do some very basic maths with with c um and it looks very familiar as to what we're used to doing uh you know everywhere else so we can add by using plus we can subtract by using minus we can multiply by using a multiply and we can divide by using our dividing bar and these happen in their normal mathematical order as well or we can use brackets the same way as we do in mass to force some sort of precedence you know to force something to happen before something um you know before something else say for example if i have a variable x of data type int i make it i assign it the value 5 i have a data type int variable name y and i assign it the value 10 and then i have a variable uh and then i have a variable named result and it's of data type int i can say that result is equal to and i can use brackets to make to force it to do the addition first so i'll do the 5 plus 10 first it will be 15 and then it will multiply it by 5 and then i can print it out and i can print out my result in here by using the percentage d specifier as well so maths is basic but you can do a lot of things follows very similar rules and remember that you can also do maths with your chart because they're just like ins really so what happens in this case is that we can actually move around our letters we can add one uh or we can subtract one or we can add you know 10 and it's going to move us around based on where we are in that ascii table and we will play around more with our chars um as we go as well so but this is you know a little piece of code that you can you know even put into your um into your g edit to try as well okay so there are some quirks to integers and it's because we sorry guys we will do a demonstration soon um so the quirks occur because we have integer overflow or integer underflow what happens is that there is not enough memory space to keep all this infinity and beyond exercises infinity and beyond numbers so our numbers are kind of limited so i've put in a few links if you're interested you can have a read about what can happen if you do an integer overflow or an integer underflow so you've got the a nice plane that has to be rebooted every 248 days and that happens because of an integer overflow issues what happens as well is if we add two very large integers together we might go over the maximum value and what it does it rolls back around to the minimum value it can also end up negative and you can check out this area in 5 explosion where a simple error like that led to a huge problem and i've put a link in a less destructive example if any of you know the video gangnam style it maxed out on youtube so it maxed out the views counter so that's another example of that integer being overflowed yeah and so and the other thing as well you know ins might not always be 32 bits it's really dependent on the operating system and what's happening so that's another thing to consider so that can also um you know that can also stuff up what's going on but the best way is to just make mistakes don't be scared to make mistakes it's really if you have a question you know about what will this do or what will that do just try it out you know you can't really you know it's it's so much fun um trying things out and it's even more fun breaking things i love breaking things um and then doubles as well so again there's no such thing as infinite precision because you are limited in how much memory space a double can take so you can't do you know these repeated fractions if you divide one by three you're not going to get one point three three three three three three three three you you're going to taper off sometime so if you keep you know if you keep approximating and you keep adding things or subtracting things and keep going with these repeated numbers you're going to get you're going to get some serious carry errors as well so it's kind of like it's always an approximation so in c c thinks in data types okay so things are either an int or they are a double um or they are something else depending what data types you're using so the way c operates is that if both numbers are ins the result will be an int so if you're dividing 3 by 2 it will not give you 1.5 because 1.5 is a double what it will give you is it will give you 1 because that's what that's what the int is of that division okay so if you're dividing two doubles then the result will be a double as well but if you have um if you have two ins and you're dividing them you will drop whatever fraction or whatever remainder exists okay and that's why we have a really uh cool thing called the modulus and it's the percentage symbol if you use this symbol between two numbers it will give you the remainder from a division between integers so for example 5 divided by 3 will return you 1 but if you do 5 modulus 3 it will give you 2 because 5 divided by 3 is equal to 1 with the remainder of 2 and that's what the modulus symbol is doing it's giving you whatever the value is of the remainder all right let's take a quick break right now um let's take oh gosh a five minute break or a few minutes break five minutes let's do and then we'll come back and we will talk about if statements and then i would like to do some coding as well all right hello everyone i saw someone uh you know solving the riddle properly so that's that's quite nice um and also some pretty fun solutions in there love it um okay so what we're going to do is uh i'm sorry my timing's been a little bit off um i am going to fix my timing and what i'm going to do is i'm going to go into a demo now to demo all the things that we've talked about because i think that's a little bit more valuable and so my usual style of lecture coding is doing a bit of a loose unit i like to demo everything but i've tried to try the slides more this time and it hasn't it hasn't quite worked as well as i hoped for so let's do let's let's go demo why not let's have fun so let me switch on vlab all right okay so let's let's let's have a look at different things um and we will talk about all the different things that are happening okay so i'll go back here so that was hello dot c so maybe let's do a different file that we can use maybe if i spell it correctly i'm going to call the file variables.c and i'll use my ampersand to maintain control of the terminal okay so let's talk about variables okay so the first thing i do is i include my standard input output library and then i start to do my lovely um int main void which you guys will become very well acquainted with doing and then whatever goes inside my curly brackets is you know is my piece of code so the first thing i'll do is i'll kind of work on the you know i'll work basically with just ins and doubles just to show you what i what i'm doing and then we will do some char examples as well in code let me just move my face off the screen so you can see things better and you know what i'll do i'll go like that and then that's not in the way as much okay fantastic so now i've moved yeah i've moved my video over so it shouldn't be blocking anything now okay so let's do some declarations and some initialization of variables so what you'll see me do is in the lectures i will comment the code way too much that is not what you need to do when you're writing a piece of code you will not need to comment on every single variable that you make to tell us what is going on okay because your variable names should be descriptively enough named that you do not need to do that but in the lecture what i will do is i will i will comment everything just so that you can see what i'm doing so let's talk capslock on let's talk about declaring a variable or even many variables so let's first declare an int so when we declare a variable we always have to specify the data type of the variable and let's say i'm going to do an int and i'm going to call it answer so what happens now is that i have a variable called answer and it's of data type in so what it means is that inside it i can place any type of number okay and when i assign a number i say my variable name and i use the assignment operator which is just one equal sign it means assigned to so let's say answer is equal to 13. what that now means is that answer variable has the number 13 in it now if i want to see it perhaps i'm going to print it out to my terminal and i'm going to use a format specifier percent d to tell my computer that i'm going to i'm going to take out a variable and then i'm going to also do a new line just so that it looks neat for no other reason i will place a comma and i will write down the variable name that i want close the bracket and i will use a semicolon and i will return zero to say my program has just finished so you can either do when you're declaring the variable i've done it in two lines you can do it like that this it's the same thing to also do it all in one go so you can also do it like this and then you don't need the second line and it's you know up to you how you you are doing it as long as you so you can declare and you can also initialize on the same line so now if we run this what i expect is for it to be able to print out the answer is and then print out the number 13. so let's see so dcc for my compiler i'm going to name the file variables and the name of the file that i'm compiling is variables.c now if i run it everything seems to be okay so i'll clear the terminal and i'm going to do dot slash variables because that was the name of my program and when i output it out it tells me the answer is 13. so i was able to print out and i was able to have a variable and i was able to assign a number to that variable now i can also choose to change the value of this variable at any stage okay and c runs code starting from the top and going down down down line by line unless of course you have conditional statements in which case it will skip some sections of code but we'll talk about that so if i've got int answer at this point over here it's equal to 13 but when i do answer is equal to one here at this point in time it's now equal to one so if i compile it again okay and then i run it again oh as you can see it's given me the same answer okay and you'd be wondering why on earth is it i've just said that answer should be equal to one when you're using g edit have a look up here if you see a star um next to your file name what that means is you have not saved your file so it hasn't updated it okay so save the file first then compile it and then run it and once you do that it will actually give you the correct answer so the answer is one so we were able to change our answer as we go now we might also have a second answer okay let's say i want to have another variable called answer two and this variable i'm going to make equal to 7 and what i want to do is i want to print out i want to print out both of them so i'm going to say the answer is percent d so the answer that's the first one and then i'm going to say the second one so the answer to is and then i'm going to have a percent d for my second answer so the first variable name applies to the first format specifier and then the second one i need for the second format specifier so for the second percent d so let's say i've got my answer to so now i have i'm going to be printing out two variables i'm going to save it i'm going to compile it again and then i'm going to run it again okay and this time it's going to give me the answer is 1 which is my first variable and then the answer 2 is 7 which is my second variable so i was able and i can have as many of these as i want in my printf statement okay and i can have as many here as i want as well okay so let's have a look how am i writing the command so quickly well what you didn't know is that i'm a superhero who can type so fast no it's not true i'm pressing the up arrow if you press the up arrow in terminal what it does is it brings up all your commands that you've typed in the past okay and how can you change directory in the terminal you press cd and what directory you want to change to okay so that's that's using those variables and what about now let's have a look if we have a double in there as well because that will be neat as well we want to be able to be able to see a double as well it's not just ins that we care about so let's say our answer 2 right now is going to be not an int but it's going to be a double okay that means that my answer to is now going to be a decimal so let let me give it make it a decimal i'll say it's 7.3 okay so do you guys think that everything is fine and it will compile perfectly now or will i get an error what do you reckon so i've got my two variables here and i'm printing out two variables so what happens if i compile it now do you reckon there will be an error no everyone reckons they'll go oh someone's got it yes i will get an error absolutely because that's right everyone's got it well done um because my format specifier here is still that i'm going to type out a decimal number but in fact what i have is i have a long float i have an lf i have a decimal number and as you can see my error message is actually telling me that so my format specifies type int but the argument has type double and it's even suggesting for me to change that so i need to have an lf here for it to be able to print that out otherwise it doesn't really make much sense so now if i compile it again and if i run it again now it will be able to print it out for me no problem now what you can see here is that double um also puts a whole bunch of decimal places for me um there as well okay so there are ways that you can um you know cut that you can make it to however many decimal places you wanted to be does anyone know how to do it so that it looks much neater oh very close so we've got someone in the chat suggesting if we just do 2 lf and that will be able to cut it down for us so if you think that that could be a good thing to do let's try it out let's see what it says and that's one of the best ways to do it is oh it's run and then what happens if i run my program nope nothing has happened so the only thing that's missing there is a dot two and let's compile it again and we'll run it again and now it's cut it down to two decimal places so you are saying it exactly as as you would do so i want to cut it to two decimal places so i'm saying decimal place two decimal places and that tells my computer that i want to print it to two decimal places so that was a really good guess very close and that can be really anything that you want it to be however many you know you can do dot one as well but i use dot two because that's what someone had suggested in the chat okay um so that's my printing out of integers and doubles how does everyone feel about integers and doubles is everyone super excited by the party that are integers and doubles yeah that's a very good question as well in there yes if you have zero decimal places it will also cut it off that's right and again if ever in doubt you know try out different things it's such a great way to learn when you try out different things because it really kind of lets you see what's going on so i've said i wanted to zero decimal places and boom zero decimal places um okay there is i'll actually i'll let you answer it there is a percentage float as well but we don't use it we use the percentage lf in the course um okay so that's your integers and your doubles how about we look at our characters now and play around with our characters okay so what i'm going to do is i'm going to comment out this section that we've just done and if you remember i kind of showed you you can do dash star and at the end of something i can do another star dash and what it does it just comments out a whole bunch of sections so it doesn't mean it just means that i don't have to do two slashes on every line which is a bit painful um and i'm going to i might just i might just move this out of the way okay and i'll move it back in just so it doesn't interrupt us okay so let's have a look at characters okay so you can display the ascii table and you you don't actually need to know the ascii table okay but it's useful when you're learning about chars just to be able to have a look at the ascii table so that you can see what it's doing but you don't actually ever really need to remember what number stands for what letter because that defeats really the purpose of how easy you know it it can all be ah what a mess so if i type in ascii flag so apostrophe d what it does is it shows me my ascii table let me just make it neater sorry i've increased all the fonts and so now nothing nothing quite fits on my screens but i kind of okay here we go we've got a nice neat table so if you want to type in the to have a look at the table just type ascii and then slash not slash sorry hyphen d um and that allows you to see all the different ascii combinations um so what we get is we've got our lovely letters here and i can't highlight because i can't highlight in row because it highlights columns but let's have a look at this one here so let's have a look with a capital a okay so let's say i'm going to declare a character so i'm going to use the data type char and i'm going to call that that i'm going to call it letter the variable and i'm going to assign the value a to it now remember when you are assigning a character you have to use the single quotation marks because the single quotation marks tell it that you want to assign this character to it and what happens is that you um so then what is also there is that it is the number 65 okay because the letter a indexes to the number 65 on the ascii table so what i can do is i can print out that letter and i can print it out as a character or i can print it out also as a decimal number that it indexes two okay so if i want to print it out as the character or if i want to print it out as the a i'm going to use the percent c specifier because that's that's stands for character and then i might want to see the ascii value as well okay in which case i want to use percentage d and i'm going to finish it with a new line because it makes it look neater now both times okay both times the variable name that i'm referring to is letter so it doesn't matter that the letter is you know it's a character because it's also an int so now if i run this okay it should give me that the letter is a and the number is 65. so if i compile it up what have i forgotten um i've made an error i've forgotten a semicolon this is what nightmares are made of you will be dreaming of semicolons so let's try that again so i fixed that added the semicolon i'm compiling it again and i'm going to run it now as well so it should give me that the letter is a and the number is 65 so the letter a has the ascii value of 65. so there we go so what you get is that my char even though i've done my variable and i've assigned the letter a to it it's still also storing the ascii index of it so the number 65. so each character has in it the character itself but also that the index to that character what it means is that i can move around my letters as well okay so i can do i can you know i can do some maths i can move backwards and forwards in my letters okay so should we move around why not let's move around okay so if we're going to move around our letters okay then let's have a look how we do that okay so we've got our letter a and let's say i'm going to now uh how many should we move down i don't know what should we do let's move down to the next one just for fun so i'll go another data type chart and i'm going to do that's a terrible name actually i'm going to do next letter and what i will say that my next letter is going to be is letter plus one so now let's see what happens okay so then if i print out the letter percent c has the ascii value of percent d that will give me what my initial letter is and then i might do the next slot so the next letter percent c which will give me the letter and then i'll have the ascii value of the next letter and you can see that it you can actually do some very simple maths with your letters because they are um they are also ins so i've got the letter letter for the first slot and then my variable names of the next one is next letter and next letter again so if i save that now and i run it again so i'll compile it first because i've made changes to it and then i'll run it again and what i'll get is the letter a has the ascii value of 65 which is what we saw to start with and then the next letter b has the ascii value of so i'm able to move around the alphabet just by adding one and becky you've asked a great question you can absolutely do single quotation a plus one because it grabs the index from it basically those single quotation marks mean that it's from the ascii table kind of thing so uh definitely absolutely so and why not let's let's try it out so i can do let's do what letter should we do where would you like to move i'm going to use s and i'm going to add 3 to it and then let's print that out i might do a new printf statement so it doesn't look as confusing and i'll use percent c to see what the letter is and we're using secret letter secret letter how exciting and what am i missing yes the semicolon so now if i run it again um i've got my secret letter is v because v comes three letters after s so one two three and i've got my v from my s which is 83 plus 3 is 86 amazing and my basic maths is pretty good too so that's always good to know um and you can do other characters as well as you can see it's not just it's not just letters a char can also have these wonderful things here exclamation mark hashtag you know percentage symbol and there is all sorts of little cool things i mean um this one here i've always loved the bell it's from like old school typewriters too it lets out a little ding you won't hear the ding because it's on cc machines you're right you're running a virtual lab but that's it lets out this really terrible like ding that was a terrible um ding but that's what it does yeah and there's all sorts of fun things as well um that you can look up what the whole ascii table is really okay so that's that's playing around with letters and also playing around with numbers as well so now let's look at a little bit of maths just so that you get a feel for maths and then we'll also talk about that lovely hash define as well so let me move this slightly out of the way okay and we've got 15 minutes to go which is exciting not exciting sorry i have run over time today okay so let's uh that was variables let's do some maths i think so i might do a new file again and i will tidy up these files also so that they don't look so messy when you look at them after the lecture okay so first thing we're going to include our standard library and then i'm going to have my main function all right let's do some maths okay so let's have a look at i don't know let's do two variables and we might do some adding subtracting and so on with them and then we'll look at some of the issues that happen so let's have let's have the first one say number one and i don't know i might make it five and let's have the second one and i don't know i might make it 10 which were the examples that were used in the lecture slides just so that it um and then i might have something called in result which is where i want to assign the result to so if i then say result and i'm going to use the equal sign to say assign to and then we can do a whole bunch of different operations so if i do number one plus number two obviously that's going to add them together and if i do multiplied by number one what it's going to do is it's going to multiply by number one in the current circumstances just like maths it's going to do the multiplication first so it's going to do number two times number one first and then it's going to add this number one and then what it will do is it'll assign the final answer to result if i want to force it to do the addition first i just use brackets just like we normally do in any maths that we do and then if you want to print out your result you can just print out the results so you can do printf and if i want to actually you know have that sort of uh you know actual math sentence there you can do your percent d plus no percent d and it's equal to oh well and then times five percent d and it's equal to percent d my goodness how much is happening in this one okay and then really it's going to be using all of these so number one number two so the first one is number one and the second one is number two the third thing i want is number one again so you see how you can have as many things as you want in there and then the last thing i want to finish off with is the result so then if i save that and compile it okay so now if i compile it and then run it what i get is this wonderful 5 plus 10 times 5 and it's equal to 75 again you can see the terminal prompt is also on the line which means that i could just have a new line here just to make it you know look more pleasant as well and if i do that it'll move the terminal prompt onto the next line then it will give me out the equation okay um so let's um let's have a look okay so let's say also that i always want my number one to be five okay it never changes okay i've never changed it actually i've never changed it in this piece of code so it's highly likely that this number is actually a constant okay and in that case what i can do is i can use the hash define to do it so in that case i'll use my hash define and let's say i'm going to call it still number one or number and if you notice when i do a hash define then the variable name or the constant name is going to be in capital letters and it's part of the style guide that we use so that you know that it's a hash defined number it's not just a normal number so i've defined my number and you can have as many hash defines as you want so anything that doesn't change should really be a hash define so now anywhere where there was number one what i can do is i can refer to it by using this number that i've hash defined and it will do the same thing for me but what it allows to do is it allows for your code to be a lot a lot neater because you don't actually need to have a variable if something doesn't change if something is a constant it doesn't need to be a variable so it's a lot neater to make it a hash define or a constant okay so then if we run it again and we compile it um you get the same thing even though you use the hash define instead okay and the reason we use these hash defines they really need to be outside of that int main because if you have more than one function which we will later on what happens is they need to all have access to this to this constant and for them to all have access to it it needs to happen outside of these curly brackets so the curly brackets are like the gatekeepers of where the variables live and we'll talk more about that later so what that means is this this number two only lives in these curly brackets it doesn't live outside of it and if i have anywhere else in this piece of code then it's not going to live anywhere else either so you really want to have a hash define outside of your main okay so um let's i think there's someone uh so for now uh someone asked about the advantages of constants for now really you need to understand um that if something is not varying it's not a variable it's a constant constants cannot change okay that's useful to you if you you know if you do a very complicated piece of code if you have a lot of variables there's a lot of potential for them to actually change and someone to come in and inadvertently change a variable that you do not want changed okay so when you hash define it when you make it a constant what you're saying is this is something that we don't want change so if someone comes in and looks at your code they know that that they might not inadvertently change it because it will not allow you to change that sort of constant so that is you know sort of a very useful way that we look at add constants okay so let's have a look okay i wanted to start talking about if statements today okay so if we talk about if statements okay um what what do you guys know about if statements do you know anything about if statements have you heard of if statements so if statements are these wonderful things that we use to tell the computer where to go and what to do and if statements we use them when in problems where we need to make a decision so any time where you have to ask a question and answer it it means that we really want to use an if statement so if something happens then we kind of really you know we want to do this else if something else happens we want to do that maybe another else we want to do something else you really don't want to have you know huge ifs but yeah people are saying they've used them in excel and they are basically conditions okay so what that means is that you're able to decide what to do based on what's going on in your code so it means that your program can branch out between different sets of instructions and if statements they solve decision problems so let's open a new file and we'll do just a brief intro okay so let's say i want to make some decisions in my uh lovely in my lovely code so if statements encode so whenever you need to ask you know is a number even is a number larger than 10 is the number prime those are really good contenders for using if statements okay and then you might say okay if the answer is no then i might add an else or if the answer is you know um is no again i might add an elsif so it kind of takes the format uh where you will ask you will have your if and then you will ask some sort of question in here okay so can so question in layman terms or it's called a condition so some sort of condition has to be met now this is not code over here question condition this is just me kind of writing what needs to happen now if the answer to this question is yes then what will happen is you will go into this little curly brackets here and you're going to do whatever the code is so you're going to run that code if that condition is true what you can then do if the answer is no you can tell the computer to run a different piece of code so if the answer to the first question is no to the question that we asked here then the computer will switch to this branch and it will do whatever is inside the else little bracket you can add more branches as well if you want you can do an else if and you can have another condition in here and then you can finish with an else so you can have any number of combinations you can chain them together and you can create a lot of options when you're doing your if statements um and it really depends on what you know on what you're trying to do so for example let's try so let's have two integers let's do something very simple with an if statement so let's have int number one is five and we'll have int number two is 10. okay what question can we ask we've got all of our maths operators so we can really do a lot of question asking we can use you know less than or greater than we can use a whole bunch of things to compare what's going on so for example i might want to compare number one and number two i want to ask is number one greater than number two so is number one greater than number two so i might type in here to ask this condition if number one is greater than number two then what's going to happen okay i don't know maybe we can print out [Music] let's print out then that and instead of those specifiers i've got number one and number two okay i'm gonna get rid of elsif for now because i've just got one one statement okay now oh i've deleted a bracket oh actually so i can have if number one is greater than number two then i'm going to print out that it's greater or else i'm going to print out that number well what are the different i guess they could be equal as well can't they but let's just print out they're not it's not equal just to show you the the simplest way to branch it okay so we've got a simple piece of code now that basically says i want to ask the question is number one greater than number two okay and what's going to be my answer is it going to be true is 5 greater than 10 here no it's not so because the answer to this is going to be no it should go inside my else okay so it should really head in here and it should uh create give me uh this statement percent d is not greater than percent d so it should say five is not greater than ten okay so let's have a look what it does so let's compile it first no errors yay so then we'll run it and it's telling me 5 is not greater than 10. so that's good that has allowed me to you know be able to do some basic comparison what are some other things that i can ask well i can also check if they're equal to each other can't i so perhaps i can say else if and then i can have another condition here where it checks whether they're equal to each other so else if number one and to check equivalence and this is where we'll do that where we talk about one equal sign being a sign and two equal sign is to test for equivalence so we'll use two equal signs to test for the actual whether they're equal to each other we will ask if number one is greater than number two sorry if number one is equal to number two and then in this case i will write that they're equal to each other so i might type in a statement printf ascent d is equal to percent d and again i might have my number one here and number two and obviously it's not going to branch out to here because they're not equal to each other but if they were equal to each other if i were to change that to say that they're equal then it should print out for me now that those two are actually equal so if i run it now then it's going to give me that five is equal to five um okay so it's two o'clock um so what we'll do is we'll finish there for today i'm sorry about the timing today um i will do better timing next week i've kind of got a bit more of a feel and we're going to do focus more a bit on the demos next next week as well um thank you so much for your patience and for tuning in um appreciate it um and we will talk more about if statements as well next week and we'll really kind of talk more about everything that's going on and if you've noticed also um you can see that i've been indenting you can't see it very nicely because my fonts are so big so it's curling everything around but i keep indenting things and the reason for that is it's got to do really with style it makes the code easier to read for the person so that's always very important so let's um we'll reconvene on tuesday and we'll talk a lot more about what's going on so have a really wonderful rest of the week and i'll see you next week bye guys you
Info
Channel: UNSW COMP1511
Views: 1,029
Rating: 5 out of 5
Keywords:
Id: SHRjHdSQIE0
Channel Id: undefined
Length: 127min 24sec (7644 seconds)
Published: Wed Sep 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.