Full transcript of “Introduction to Programming and Computer Science - Full Course”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.
0:01 Welcome to Introduction To Programming. My name is Steven And my name is Sean. Over the next 90 minutes, we’ll be taking you through this series consisting of 21 different segments that hope to cover the basics of computer programming, which can apply to any and all programming languages you might want to learn. We’ll be starting with the simplest question of what is programming, and from there will be working our way up as we talk about common features of computer science such as loops and arrays. We’ll discuss how to read and write code, debug code you’ve written, some strategies to help plan out your code, and much, much more. The complete list of topics that are going to be covered in this lecture-style video are shown on the screen now. Additionally, there will be time-stamps in the description, so feel free to skip around if you are already proficient in some areas of computer science, or just want to know about a specific topic we will be covering. Hopefully, by the end of the series, you’ll have a basic understanding of what computer science is, along with an armory of useful skills that will help you unravel whichever programming language you decide to learn first. We’ll only be covering the major key points that apply to all programming languages, so we’ll be shying away from topics such as object-oriented coding and command line navigation,
1:07 as those are things which are language-specific. Additionally, there will be no software required for you to download in order to follow along with this tutorial as we won’t be writing any code to keep things simple and concentrated. This video is meant for those who are interested in computer science and programming but have no idea where to start and have little to no background information on coding, and so if that sounds like you, then strap Sean and I work our way through the wacky world of computer science, starting with the biggest question probably on your mind, which is what even is programming? Well, the dictionary defines it as the process of preparing an instructional program for a device, but that’s a really confusing definition, so in layman’s terms what exactly does that mean? Essentially, it is attempting to get a computer to complete a specific task without making mistakes.
1:57 Imagine this, for example: you want your less-than-intelligent friend to build a lego set, except he has lost the instructions and can only build based on your command. Remember though, your friend is far from competent, and so If they are not given very specific instructions on how to build the set, there are many mistakes he could make. If he thinks like a computer, then if there is even one piece that you have not told him specifically where to place and how to place it, the entire lego set will be ruined and he will be left to suffer a complete mental breakdown causing the whole goal of the project to be corrupted. Giving instructions to your friend is very similar to how programmers code. Instead of a less-than-intelligent friend, you have a less-than-intelligent computer, and instead of instructions on how to build a lego set, we are feeding it information on how to complete a program like a game or
2:50 web application. An important thing to note is that comptuers are actually very dumb. We build them up to be this super sophisticated piece of technology, when in actuality, a computer’s main functionality comes from how we manipulate it to serve our needs. Now, programming isn’t as simple as giving your friend instructions since in a programmers case, the computer doesn’t speak the same language as you, the computer only understands machine code, which is a numerical language known as binary that is designed so that the computer can quickly read it and carry out its instructions. Every instruction fed to the computer is converted into a string of 1’s and 0’s and then interpreted by the computer to carry out a task. Going back to the lego example, this process would be like if he was not only less-than-intelligent, but to make matters worse, he could not understand english and only speaks in mandarin chinese.
3:43 In order to speak with him, you have to convert the instructions that you understand in english into the language that your friend understands. This process is essentially what you must do for your computer in order to make it understand the instructions you give it. The big difference between the two examples, however, is that it is very difficult for people to understand machine code and binary. Directly translating what you want the computer to do into machine code is extremely difficult, in fact almost impossible, and would take a very long time to do if you could. Each program is composed of millions upon millions of those 1’s and 0’s, so how, exactly, are we supposed to translate our instructions into machine code? This is where programming languages come into play. Programming languages are fundamentally a middle man for translating a program into machine code. These languages are much easier
4:34 for humans to learn than machine code, and are thus very useful for programmers. Going back to our lego example, a programming language would be sort of like an interpreter, that’s able to take the instructions you give them in english, and translate them into instructions your non-english speaking friend can understand. This makes programming languages extremely useful and the backbone of almost any good program. Think of programming languages as not english, and not machine code, but somewhere in the middle. There are many different programming languages out there that each have their own unique uses. Languages such as Python and Java act as general purpose languages that can perform a variety of computational tasks, while RobotC or HTML/CSS are languages designed for more specific purposes such as moving a robot or constructing a website. Languages can also
5:24 vary in how powerful they are. For instance, JavaScript is a scripting language that is designed for smaller tasks while java or python can carry out much more computationally taxing processes. We measure a programming language’s power, or level, by how similar it is to machine code, the series of 0’s and 1’s we talked about earlier. Low-level programming languages such as assembly or C are closer to binary than a high-level programming language such as Java or python. The basic idea is that the lower the level of your programming language, the more your code will resemble what the machine can interpret as instructions. Aside from the different purposes that each language fulfills, choosing a programming language typically comes down to a matter of preference, as there are usually many languages that accomplish similar tasks. Try different languages, and decide which one’s rules, interface, and level of simplification you like best.
6:20 So now that we know what programming is, how do we actually write code? It’s not like we can simply type words into a text document and automatically assume that the computer can translate it into machine code, read it, and carry out a task like opening up a browser. And additionally, we can’t just write down rubbish in certain programming languages mentioned in the previous episode and expect the computer to understand. So how are we supposed to write code then? Well, the answer is with an IDE. An IDE, which stands for Integrated Development Environment, allows the facilitation of code by a computer. IDE’s provide a graphic interface on your computer in which the programmer can easily write, run, and debug code without having to worry about problems with compilation or interpretation of the program. Think of an IDE as any other program on your computer such as a game, a browser, or even the file
7:07 explorer, except we’ll be using it to write code. IDE’s are able to turn your code into machine code and run it through the computer to produce results. In addition to providing a place for programmers to develop their code, IDE’s provide some extremely useful tools for programmers to ease the job of writing code, such as built-in error checking because as we’ll talk about later; code doesn’t always run correctly, auto-fill in for frequently used words or phrases, and project hierarchy which will help you organize and manipulate the files within your project. Back in the olden-days, before IDE’s, code used to be written on punch cards and then fed into computers which would take hours and cause a lot of pain. IDE’s nowadays act as a sort of fast-track to writing code and make things a whole lot easier for programmers. An example of a specific IDE can be seen on your screen now. In the center you can see the program that is currently being written, and right below it the console, which can print out useful information for the programmer. This specific IDE is used to write java code. IDE’s are extremely powerful and will be used in almost 100% of your programming projects. So through these IDE’s we are finally able to write and compile code smoothly without
8:12 worrying about the computer not being able to understand it. The next problem we run into then becomes how do we write this code in the IDE, because it’s not like we can just type random words from a certain programming language and expect the compiler to understand it. This is where a programming language’s syntax comes into play. Now, just as if you were learning a real language, learning a computer language can be very similar. Some have different styles that may seem odd, some may make you use abstract or weird concepts which may be confusing, and like all languages, programming languages have a set of rules that you must follow when writing code in that language, and at the forefront of those rules is grammar. Programming grammar is referred to as syntax and is very similar to real-world grammar. Each programming language has its own syntax, or rules, that you have to follow
8:59 to a tee if you want your program to run correctly, just as if you were speaking in real life. These can be things such as how you type out certain functions, what you put at the end of a line of code, and how you set up certain functions. Each language is unique in its syntax, and while some may share similar rules, all will have some quirk which makes it stand out from the rest. Syntax is something that catches a lot of people off guard, since many expect every programming language to follow the same set of rules, but as we spoke about in the last segment, because each language is specialized for a specific task, each needs its own set of rules to function. Breaking or disregarding these rules will result in an error, just how breaking or disregarding rules in real life will result in an unintended message. As an example. If we wanted to do something simple such as initialize a variable, which
9:47 is something we haven’t covered yet but the example is still relevant. In java, you’ll notice we have to specify what type of variable we are defining in this case an integer, and also add a semi-colon after the statement. In python, we don’t even need to define that we are trying to create a variable and just have to type what we want to create, and in javascript, we just specify we are making a variable, but do not include what type of variable we want to make. Even in this simple example you can see how much syntax matters when learning a new language since while the goal of our program remained the same, define an integer with value three, all the programming language shown took different approaches. All these languages require that you follow this syntax because remember, computers are extremely dumb, if you forget one semicolon or misplace a character, the entire program
10:35 will not run and send you back a syntax error, which is something we will talk about later. Think of this as if you forget a comma in a sentence and the entire context of what you are trying to say get misinterpreted. For example, in the sentence “let’s eat, grandma”. If you were to forget that comma, while it may seem like a small mistake, it changes the entire context of the sentence, making it sound like you’re about to eat your grandma. The same rules follow for programming, if you forget a semicolon, the entire context of your program could be corrupted and misinterpreted. Now another thing which makes IDE’s so useful is that they will let you know if and when there are syntax errors in your code. Syntax errors of course being parts of your code which do not follow the rules we talked about previously. The IDE will tell you where in your code the error is, and also won’t let you run your program until the error has been fixed. Because of how important syntax is to writing code and learning a new language, it's recommended that you learn the rules and syntax of a language before beginning to write complex programs in that language. Most of the rules are tedious to learn but easy to master and as soon as you can do that, you’ll be able to easily identify syntax errors and take care of them easily.
11:45 That covers the basic gist of syntax and programming rules, so now that we know HOW to write code, and WHERE to write code, we next need to cover what happens after we have typed out our program and run our code. Because writing a piece of code for a game or for a database is cool and all, but after the computer interprets the program, how will we know what’s happening, and whether it is working or not? Well, programmers do this by looking at the console. The console is a text interface within the computer that us programmers can use for a variety of different purposes. If you remember, a short while ago we showed a picture of a basic IDE, and one of the main parts of that picture was the console. The main use of the console is to output text from the program. This is usually done using a print statement. A print statement is a command that does exactly what it sounds like: it prints text to the console. This print statement is the first piece of ACTUAL CODE
12:31 we’ve talked about in this series, and it’s about as simple as it gets. The print statement, despite its simplicity, is one of the most important functions in programming and exists in some form in just about every programming language. The most basic thing you can ask the print statement to do is just simply make it say something. This is done by instructing the console to print, and then include whatever you want to be printed inside the parentheses. For example, in python, the segment of code print(“Hello World”) will cause a message reading “hello world” to appear onto the console. Pretty neat. The print statement is also vital for viewing and interpreting the computer’s output from a program. For instance, if you tell a computer to run a simple calculation, for instance to determine what 4+3 is, it will run the program internally and compute an answer. However, what is the purpose of having the computer run this program if you will not
13:19 be able to tell what the result is? Instead of simply telling the computer to perform this calculation, instruct the computer to print the output of the program to the console, and upon the program’s completion, 7 will appear on the console. As you can see, the console allows us to easily print information out to the developer for a variety of uses. It is important to note that the print statement varies depending on the programming language being used. For example, in Java there are multiple versions of the print statement depending upon whether you would like a line break after the printed text, and specialized print statements which make your code more efficient. Also, the general syntax of using a print statement and certain nuances of its function can change between languages. However, you can generally rely on it carrying out the same overall function, as it is a foundational statement for programming in general. So print statements, they print information out to the console for the developer to use, nice. All of its functionality makes the print statement, along with the console, a very useful developer tool. However, it is important to remember that that is all it is: a developer tool. The console is not really meant to be viewed by the end user of your program. It tends to be hidden away behind the scenes, and other methods of displaying information such as
14:18 displaying text, graphics, or images are used to convey information to the user instead. Think of it like this, when you’re using your phone, you see the console in none of the programs you use. So while you can use the console to give yourself information about how your program is performing, don’t try to implement it in the final product because it fundamentally just isn’t meant for that. Overall, remember to use the console to its fullest extent when writing and fixing problems in your programs, as it is a great tool to use to tell how your program is performing behind the scenes. So now that we know a whole bunch of information about programming languages and how and where to write them, along with the print statement under our belts, let’s go over some intuitive things the computer can do all by itself, without you having to tell it how to. More specifically, we’ll be covering basic number mathematics as well as string math Starting off with basic mathematics, the computer already knows how to do simple arithmetic. This includes addition, subtraction, multiplication, and division, all of which are represented by the symbols shown on the screen now. In any IDE that you may install, you’ll be able to print out the answer to simple math problems using the print statement, which
15:23 may seem counter-intuitive because why would you use the computer to do math when you have a perfectly good calculator on your phone, but you have to remember that computers are dumb, and anything we want a computer to do we have to build up from scratch. Basic arithmetic, while simple, helps out in almost any program you may write. For example, if we wanted to build a basic calculator app, we’d need to utilize this functionality in order to correctly display the answer to an arithmetic problem when our user tries to add, subtract, multiply, or divide two numbers. Now in addition to the 4 basic math equations, most programming languages include an additional operator known as modulus. If this is your first time hearing this word, don’t worry, since it's not usually taught in math classes. Modulus allows us to get the remainder of a divisional operation. For example, when we take 10 modulus 3, we are essentially telling
16:14 the computer to take 10, divide it by 3, ignore the actual answer and just give us the remainder of that operation in this case 1, since when we divide 10 by 3, the answer is 3 remainder 1. The 1 in this case is what gets printed out to the console. If there is no remainder, say in the case that we take 50 modulus 2, since the remainder is 0, the function would return 0 if we were to print it out. This can be extremely useful in many cases, the most obvious being if we want to determine whether or not a certain integer is even or odd. If we take a certain number modulus 2 and it returns 0, then we know it's even since any even number divided by 2 will always result in a full answer without a remainder, but if the system returns 1, then we know that the integer is odd. You will find yourself using the basic math operators a lot more than you think, so it's good to keep them in mind when writing your programs.
17:11 Now our computer can work with numbers, as well as Strings. Strings by the way are another way to just say text. For example, “hello world” is a string, The letter “a” is a string, anything enclosed by quotation marks is denoted as a String in programming language. We’ll cover more about Strings in a bit when we talk about variables, but for now let’s continue. We already talked about printing strings to the console, but let’s say we’re making a game, and we wanted to print out the statement” “Game over, 4 was your final score”. Now while we could just make a string that says that exact phrase and print it out to the console, in some cases it would be more useful to print out the actual integer value, especially in the case of a game where the score can change each time you play, because score definitely isn’t always going to be 4. Well, we’re also able to print multiple strings of text, and even integers by “adding” them together in the print statement. This is called concatenation.
18:08 Continuing with our score example. If we wanted to print out the statement “Game over, 4 was your final score”, using 4 as an integer rather than a string, we could do this by breaking down the statement into two strings and an integer like so. Print (“Game over, “ + 4 + “ was your final score) We of course begin with a print statement, which again will be different across all languages. Inside the print statement we start off by printing the string “Game over, “. Now here comes the important part, from there, we use a plus sign and add 4 to the print statement, just like if you were adding two numbers. Then, we can repeat this process with another + sign for the final string “ was your final score” and we’re able to print out the entirety of our statement easily. Doing this, we can easily print out multiple different strings and integers together in one print statement. We could also combine the two lessons we’ve learned thus far and do something like “Game
19:08 over, “ + (4+4) + “ was your final score”, in the case lets say where we have a game which gives you a base score, then 4 points for a certain task you completed. This demonstration also displays another important part of programming, which is that oftentimes to get your program to be the most efficient, you have to combine aspects of code. Now it's important to note that the computer will take whatever you put in parenthesis and print it out character for character, so oftentimes programmers will forget to add a space onto the end of their strings. This can result in an small mistake in which the string from the previous example will be printed out as if to say “Game over,4was your final score” which isn’t that appeasing when displayed on screen to the user, so it's good practice to always put a space after and before your strings to make sure this doesn’t happen, and your string doesn’t end up like this.
19:58 Another important thing to note is the difference between “4” in quotation marks and 4 without quotation marks. Now “4” in quotation marks is treated as a string rather than a 4 without quotation marks which is treated as a number. This may not seem like a big deal, but again computers are dumb, and if you try to do math with a number in quotation marks it will return an error, because the computer doesn’t understand that you’re trying to preform the operation on a number, and it thinks you’re trying to add an integer to a string, which is a big no-no in programming. So when you are programming make sure to make a mental note of whether or not you want to make something an integer, or a string, because that type of stuff makes a big difference. Alright, that concludes our segment on the base power of computers. Now next up we’re going to be covering one of the most important components of computer
20:48 science, so make SURE you pay attention because next up we are going to be covering variables, what they are, and how we use them. First of all, what exactly is a variable? A variable is simply something that can store information and can be referenced and manipulated. Think of variables like a cardboard box. Cardboard boxes serve as a means to store items in them which can be changed out, replaced, and modified. Variables are like cardboard boxes that store information for the programmer to reference, manipulate, and refer to. Each variable simply has a type, a name, and a piece of information stored inside of it. The type and piece of information will be covered next and the name is simply a name for the variable, think of it as writing out a label on the cardboard box in sharpie. Now, there are many different types of variables that a programmer can use, but right now we will just be covering what are called “primitive variables”, which include integers, booleans, floats and doubles, strings, and chars. We’ll start off talking about an Integer. An integer, or int for short, is as simple as it sounds: a variable that can store an integer value. This includes all whole numbers from -2,147,483,648 to 2,147,483,648. Now notice how I said whole numbers, integer variables CAN not and WILL not hold any decimal values, so keep that in mind when using variables.
22:09 Secondly is a boolean. A boolean is a very primitive variable which can store a value of either true or false. Boolean variables can ONLY hold these two values, and are extremely useful for conditional statements, which we will cover soon. The next two types of variables are floats and doubles. Both of these variable types are floating point data types, which essentially means that these variables can store numbers with decimal places. Whereas integer values cannot hold decimal values, floats and doubles can. The main difference between the two is that a float variable can store numbers of a precision up to 32 bit, while double can store numbers with precision up to 64-bit. Essentially, a double can store more decimal places than a float, so it all comes down to how precise you want the variable to be. Up next we have string variables, which are like the strings we’ve actually talked about
22:55 beforehand except stored somewhere in a value. String variables can store strings of letters, which are just words and sentences. Strings are useful for displaying text and storing input information. Strings can also be concatenated together to form combinations of string variables and prewritten strings. This is very useful for outputting information in a readable format for the user. For example, imagine that we have a string called “name”. The code asks for input, and stores that string of text in name. To output this information to the user, rather than simply displaying their name, you can add the phrase “Your name is:” and make it into a sentence by concatenating “Your name is:” + name + “.” This makes it easier to read your code, while also adding variability to your code, which always makes things more interesting for the end user. Finally, we have char variables. Char stands for character, and just as the name suggests,
23:43 they can each hold one character. This is useful when a programmer wants to read one button press or one character in a string without using a string variable. A specific example is making a game that is controlled by the keyboard. The program needs to recognize the character that is pressed, and translate that into carrying out some function. Now strings can also hold one character, but char’s can’t hold more than one character so keep that in mind when defining variables. Now, why are variables so useful? Well, being able to store information in a format that can be easily referenced later is essential for any good program. Oftentimes in code you’re going to want to keep track of things such as a user’s name or score, and so by creating a variable called “name” or “score” you can store this information in that variable and then reference it, add to it, or modify it. Also, many times, the program will have to take input from the user, which cannot be pre programmed into the code, and thus a variable is required to store the information. A program may also rely on factors that will change as the program progresses, in which case a variable is once again required. Also, taking these variables and manipulating them is quite necessary for carrying out many of the tasks you want a program to carry out, for instance multiplying int variables or
24:47 concatenating string variables. Overall variables are the backbone of any good program and you’ll find yourself using them often if you want clean and efficient code, so it's best that you learn what types of variables you need to use and when. So now that we know all about the different variable types and we’ve talked about them a little bit, now we’re going to delve further into what happens when we actually define, or create, a variable, how we reference them, and how we can manipulate them for our programs. To start, let's go over what happens when we define a variable. Now when we write a line of code which initializes a variable, and that code is executed, the computer essentially creates a little space in memory that stores your variable name and its contents so that it can be referenced later. Going back to our cardboard box example. Think of this as
25:32 if you have a storage facility, and you make a new cardboard box, labeled “Name” and inside of it you put a piece of paper with the word “NullPointerException” on it. Now, anytime you want to know the contents of your name box, you could simply look inside and see that it has the contents “NullPointerException”. This is what the computer does, except the storage facility is memory, the box is a variable, and the contents of the box are whatever the variable is set to be equal to. Any time you want to know the contents of the name variable, you can simply call it and the computer will pull the information that is stored in that variable out and use it how the user sees fit. Another thing to note really quickly is that you can actually make a variable without putting information inside of it. This would be like if you built up a new cardboard box, gave it a label with a sharpie, but just didn’t put information inside of it. You’re simply
26:27 saving that space in your warehouse for later. This can be because you want to store information in it later down the road, or if you’re going to use it to store information given to you by the user, in which case you CAN’T give it information since you don’t know what the user will input. Just note that if you try to reference, or point, to a variable which does not have any information in it, you’ll get what’s known as a NullPointerException, which despite being an awesome name for a YouTube channel, is something you generally want to avoid when programming. Now, programming languages allow us to do some pretty cool things with these “boxes” that we’ve created. For example, let’s say we created a second variable “channelName” and instead of setting it equal to “NullPointerException”, we instead set it equal to our already created “Name” variable. This doesn’t create a space in memory for this new variable; however,
27:18 it simply points to the same location of memory we have already created for the “Name” variable. Going back to our storage facility example, this would like if instead of creating a whole new box labeled “channelName” and storing a sheet of paper with the word “NullPointerException” on it, we instead simply added another label below the “Name” box, entitled “channelName”. Now, we have two variables but both point towards the same contents, that being the string “NullPointerException”. We usually do this to save space in our code for things that we know are going to have the same value. Variables can also be updated throughout your code. For example, let's say you had an “Age” variable, and inside it was the integer 17. Then you celebrated a birthday and wanted to update your age. All you have to do is reference the variable, and set it equal to
28:06 whatever new integer you want the variable to hold, in this case 18. This would be the same as having a box labeled age, with a sheet of paper reading 17 inside of it, and then taking that piece of paper out, erasing 17, replacing it with 18, and then placing it back into the box. Doing this, we are able to easily update the contents of our variables throughout the code as things dynamically shift. As another example, if you were making an RPG, your character will likely have stats such as attack, defense, mana, etc. As the game progressed you could continuously update the variables so that the player could get more powerful the further along that they went through the game, and you wouldn’t have to create new variables. You would just simply need to keep grabbing that box from your storage facility, erasing and replacing the numbers on the piece of paper, and then
28:55 continue along with your code. Just keep in mind that these variables are nothing more than places in memory in which a certain value is stored, so we can easily update the numbers, and their place will remain constant. After a code has run its course, the place in memory is deleted until you run the code again and the program dedicates space for the variable again. Each time you run the code, you're making new boxes in your storage facility, and at the end of your code, you destroy them all to make room for new boxes next time. Another cool thing you can do with variables is add them, subtract them, multiply them, divide them, and even modulus them. Now this mostly only works for integer variables as multiplying and dividing strings doesn’t make too much sense. But if you were making a calculator app and you stored the first number the user entered as num1, and the second as num2. You could then multiply num1 and num2 together, and either print them, or store them in a new variable entitled result. Then, each time you run the program, the user could input new numbers into the num1 and num2 variables, and they would simply be set to those new integers and return the result that corresponds to those specific numbers. This allows for you to keep easy track of which numbers are which and what’s going on in your program
30:07 which is extremely useful. Also, while you cannot subtract, multiply, divide, or take the modulus of strings, you are able to add them. Let’s say you had a string Str1 with the contents “Hello “ and a Str2 with the contents “there”. You could add Str1 and Str2 to create a string that had the contents “Hello there”, either storing it in a third variable or printing it out to the console. The last topic we’ll be covering on the topic of variables is the naming conventions of a variable, which albeit may seem odd, but it's extremely important when trying to read your code so we will be covering it now. Now, variables have to be one continuous string, and so if you wanted to make a variable that stored the player score, you’d have to find some way to combine the words player and score, since you can’t have the phrase “player score” be the name of a variable. All programmers have their own personal preference when it
31:02 comes to naming variables, but the one we will be using on this lecture is called CamelCase, which is the process of not capitalizing the first word, but capitalizing every word that follows it. Going back to the player score example, using the camelCase method, the variable would be called playerScore. This allows us to easily identify each word and becomes really useful for long variable names like thePlayersScoreBeforeFinalBoss”, whereas if we just type it out without capitalization, it would be really confusing. This will help a ton when you start to find bugs in your code and need to quickly scan your program to figure out whats wrong, and adds to the overall readability of the program. Other programmers might use different naming conventions like using underscores to separate the words in a phrase, but for now we’ll be sticking with camelCase. Next, we’ll be moving on to conditional statements, which at their core, are statements
32:00 that change the path of our code depending on certain conditions. For the sake of keeping things simple, for this section, red lines will connote that our code will NOT be following that specific path, and green lines will mean that our code IS following that path. The main type of conditional statement that programmers use is the if statement, and this will show up countless amounts of times in any program you write. It is as simple as it sounds: if some condition is true, and usually the condition will be enclosed by braces, then carry out the instructions located within the if statement’s brackets. Else, do another thing. Now, brackets are used in most programming languages to indicate a segment of code which will run. It works like this, if the condition in parentheses is true, then all of the code contained within the brackets will run, and
32:47 if the condition in the parenthesis is NOT true, then it will skip over all of the statements within the brackets. A quick note is that while this is the case with most programming languages, some; like Python, use colons and white space to determine where a piece of code starts and ends, but for the sake of this series, we’ll be using curly braces. Now, the condition within the parenthesis can take on thousands of different forms such as if the value the string variable Name is equal to “Steven” or if the player's score, stored in an int variable, is greater than 5, the list goes on and on. Each of these statements is evaluated as a boolean, which you will remember from when we talked about variables is either true or false. If the boolean is true, we run the code inside the curly braces, if it’s not, we pretend everything inside the curly braces never existed and move on with our code.
33:40 The if statement comes with two more additional statements that can go with it: else if and else. Else if is a conditional statement used directly after an if statement, and carries out mainly the same function as an if statement. However, the else if statement will only be evaluated if the preceding if (or else if) statement is bypassed due to its condition being false. So we would run through it like so, if something is true, we would run the code inside of that statement’s curly braces. Else, if that something is not true, BUT another statement inside of parentheses is true, we would then run THAT code segment. And if neither of them are true, we would skip both segments of code and move on in our program. This is a hard concept to wrap heads around so let’s do an example. If we had a program that evaluated the if statement if (age = 10), we could then have a statement under that
34:32 which stated else if (age = 12). Now, if the age variable was 10, which we can see from the example it is, then the code immediately following that conditional statement in the brackets would run. The else if statement we made would not even be tested since we know it is going to be false, and thus the print statement inside of THAT conditional statement’s brackets will be ignored, and the code will move on to the rest of the program. Now, for example, let’s say we changed the age variable to be 12 instead of 10. Now, instead of the first conditional statement being, true, it actually evaluates as false, since age no longer equals 10. So what we do now, is first skip over the print statement which prints out that the age is 10, since it’s not. And now we evaluate the else if statement. We check if age is equal to 12, which again it does, and so now we run all the code inside of that conditional statement before finally moving on to the rest of the program. So as a review, we check the initial if statement, if it’s good we run all code within that if statement’s curly brackets and move on with our program, if the initial if statement is NOT true, we then move on to any else if statement’s and evaluate if THOSE conditional statements are true. We can have as many else if statements as we want, although this could
35:37 lead to clutter amongst your code so we’ll talk about some alternatives later on to help this out. Now that takes care of the if-else statement, so now we’ll move on to the else statement. The else statement once again comes after an if or an else if statement, and will carry out its instructions no matter what, as long as the preceding statement/statements are evaluated as false. If we went back to our previous program, we could add an else statement which would only have the code in its brackets run if the age variable wasn’t “10” or “12”. This would catch all cases of the program that didn’t fit our parameters. It’s good practice to ALWAYS have an else statement at the end of your conditional statements to catch any weird cases that may come up in your program. Now remember back to the fact that we can have thousands of else if statements, after a while that can get pretty cluttered, and so another very useful conditional statement which helps circumnavigate this problem is the switch statement. A switch statement is functionally similar to many if and else if statements together. You write a switch statement in the form of switch (variable), and then below that you write how many cases that the variable could be. For instance, if we wrote switch(var), then under it we could write out 5 cases that the variable var could be, and then the instructions listed under that
36:39 case to be carried out if the var variable is equal to that case. Now switch statements are different since instead of using brackets, they use a colon to signify the start of a set of instructions, and a break statement to end it. This is very useful because you are able to essentially use many if and else if statements without having to write nearly as much. In switch statements, you just always have to remember to include a default case at the bottom of the expression to denote any and all cases that don’t meet the above requirements. This simply catches all of the inputs that don’t fit within the programs main cases. It’s very similar to an else case at the end of an if-else chain. Now, why are these statements so useful? Well, many times, programmers want their programs to function differently depending on different conditions. For instance, a program could function differently depending on the information that the user inputs, such as allowing a user to use a program or not use a program if they are above or below 18 years old respectively. Or in say a video game, if a user’s experience level is above a certain threshold we might want to give them harder opponents to battle. Another example could be a program which changes the color scheme depending on the time of day. Or even more simply, if a user presses
37:40 a button that is meant to move on to another screen in an app, the programmer would only want the app to change screens if the user clicks that button. A program without conditional statements would do the same thing every time, and would be very primitive compared to one that can change depending on its conditions. So now that we know how to make and use variables, how to compare them, and what we can do with those comparisons, let’s move on to another foundational concept of computer science: arrays. Now we’ve already talked about variables, and how great they are for storing singular bits information for making our code more simplistic, but one of the biggest drawbacks that come with variables is their inability to hold more than one piece of differing information. For example, let’s say you’re making an app which allows users to create a grocery list. Well there’s no real easy way to create lists using variables, because it's not like
38:28 you can have one variable store the names of 7 or 8 different food items. Remember, we can only put one piece of paper in our cardboard box; no more. And besides, even if you were to add multiple items to one string variable, you still would have a lot of trouble doing simple tasks you might want from a list like searching through it or splitting it or even deleting items from the list when you’re done with them. This is the problem that using arrays solves for us. An array is, as you may have guessed by now, a list. You can have an array of integers, an array of strings, and even an array of other arrays which is something we’ll cover in a minute. Programmers use arrays when they want to store a lot of variables containing information that is all related to each other, such as a grocery list or a high score list in a game. Think of arrays as a column in excel or google sheets. You have the title
39:18 at the top and then below it are a bunch of bits of information which all relate to the title. Arrays are super useful when programmers want to store a lot of information that can be easily searched through because programmers have developed methods of breaking down and using arrays to find specific information in arrays full of thousands of different variables. As an example to show just how useful arrays are, let's say you’re a startup company that owns an app that has 100,000 users. Every time a user wants to create a new account, they input the username they want and then your program will have to check to make sure the account name hasn’t already been taken. Doing this requires you to search through the information of all 100,000 of your users to see if that username has an account with your service. An array would be able to contain all of this information and make it easy to search through and find out if the account name has been taken with little to no delay. Now the single most important thing to note about arrays is how you reference each element of the array within them. Let’s create a basic array called numbers, and inside of it put the digits 1-10. Now when we want to refer to each cell in this array, we call upon its “index”. An index is just a fancy way of saying that numbers are placed within
40:27 the array. Now you would think that the first integer in this array would be the first index, the second would be the second index and so on, but that’s not the case. In computer science, programming languages refer to the first cell as the 0th element in the array. This means that if we were talking about our array of numbers we just made, the number 4 would actually be in the 3rd index, 5 would be in the 4th and so on, so instead of starting our count from 1, we start from 0. It’s extremely weird and confusing but it's one of those programming quirks you are going to have to memorize and commit to memory. If you were to not follow this nomenclature and you refer to the last element in this array as the 10th, you get what is referred to as an “out of bounds” error, since you are trying to reference the 10th element, but there is no 10th element. What you’re actually trying to do is reference the 9th.
41:16 Another extremely important thing to note about arrays has to do with their size. When you initialize an array, you can do it either one of two ways. You can either populate it with the elements that you want contained in the array right then and there, creating and filling the array at the same time, or you can define how many elements you want in the array-essentially the arrays size- and then populate it with elements later. This is because when we initialize an array, it creates a space in memory that has a size of exactly what you give it, no more no less. This is great for when we want to access elements in the array because we can do so instantaneously, but the one downside is that we can’t increase the size of the array later on, all array sizes are final. Think of it like setting up a bookshelf with books. By populating a bookshelf with a certain number of books and
42:11 then moving on and filling the next shelf with different books. We have no way to go BACK and add books to that first shelf without shifting everything. Once we decide how much space to dedicate space for an array in this case, there’s no way to ADD more space. Once again because this is extremely important to remember, this means that once an array has been defined, there is NO WAY to change the size of it. If you have an array titled “Names” with a size of 8, and you try to add another name to the array you will receive an error, so be careful when messing around with array sizes. Of course, you can always go back at the start of the code when you initially MAKE the array and allocate more space to it if you find out you need more space to hold items, but once it’s defined, you CANNOT change its size. Another small thing I want to touch upon really quickly is that when you initialize an array,
43:03 you must determine which type of array it is right then and there. For example you have to specifically say it will be an array of strings or integers when defining it, and also you’re not allowed to mix and match, meaning you can’t have an array full of integers with a few strings and some doubles thrown into the mix. They have to be all the same type. Now the last thing we’re going to cover on arrays is a little funky, and that is the practice of putting arrays inside of arrays. If you make an array of arrays it's referred to as a 2D, or 2 dimensional array. Think of these as matrices if any of you have taken algebra classes. Now If you haven’t, think back to our google sheets example but instead of just columns, we would add rows as well. So now, each element in our array wouldn’t simply be just a String variable or an Integer variable, but an entirely new array with its own set of values and elements.
43:53 The way we index these is mostly the same, except we would have 2 numbers to index instead of 1. We start with the row and then the column. So a number in the position (0,2) would be in the first row three columns down, in this case the name Clint. A number in the position (1,1) would be two rows down and 2 columns across, in this case the name Chris. You get the idea. Now you can also make 3-dimensional arrays by putting an array inside an array inside an array, but that's a little above what we’re going to be covering so I’m gonna cut it off there. Next up we’re going to be talking about loops, so what exactly are loops? Next up we’re going to be talking about loops, so what exactly are loops? Next up we’re going to be talking about loops, so what exactly are loops? Next up we’re going to be talking about loops, so what exactly are loops? Next up we’re going to be talking about loops, so what exactly are loops?
44:46 Well as you could probably tell by that statement right there, a programming loop is a statement that is used to run certain instructions repeatedly, just like how the opening statement of this topic was repeated 5 times. Loops are very useful for a variety of reasons. For instance, imagine you want to print something 15 times. Sure, you could just copy and paste the print statement 15 times. But this is really annoying to have to do, and becomes even more unrealistic when that number goes up to, say, 100 or so. Now what if instead of rewriting the same instructions over and over again, you could simply place the print statement inside of a loop, and it will occur as many times as you would like, that’s the power of loops baby. With loops we’re able to repeat parts of code multiple times. Now there are three different types of loops that we will be discussing today. And up first is the for loop. A for loop is very useful for situations described above, where you would like to carry out a certain set of instructions numerous times. The syntax for a for loop varies depending on the language, however it usually consists of three parts. An integer value, a condition which the integer value must meet in order to exit the loop, and an operation to modify the integer value after the instructions inside of the loop are completed. Each time the for
45:48 loop runs, the operation you set will be performed on the integer, and as long as that integer still meets the condition you set, usually being greater than or less than a constant value, the for loop will continue to run. Eventually, when the integer has been modified by either increasing or decreasing it to the point where it no longer meets the condition, the for loop will terminate and the code will continue to run. For example, let’s say our integer value was i and we set it equal to 0, then we set the conditional statement as i being less than 3, so basically we’re saying that as long as i - the variable we just created - is less than 3, continue running the instructions contained within the loop. Finally, we make the operation i + +, meaning each time the loop runs we will increase it by one, and inside the loop let’s just put a simple print statement. Now let’s run through the loop. We start with i = 0, 0 is less then 3 so we enter the
46:35 loop and print out Hello World. Now that the instructions are done we add 1 to i making it 1. Moving on, 1 in less than 3 again, so we once again enter the loop and print out Hello World. Again, we add 1 to i, making it 2 now. 2 is still less than 3 so we enter the loop again and print out hello world. Finally, we add 1 to i once again and it becomes three. 3 is not less than 3 though, it is equal to 3, and so we don’t enter the loop and it terminates, moving on to the next segment of code. This is a simple example, but you can extrapolate it across programming to fit your needs. Now when using a for loop you have to make sure to set up a condition that, given the initial integer value and the operation, will at some point not be met, to avoid creating an infinite loop and crashing your program. An infinite loop occurs when you give a for loop a condition which will always be met given the parameters of the program, and so
47:21 the software crashes. For example, a for loop beginning at 10 and checking if i is ever less than 0, and then adding 1 to i at the end of the loop will never terminate, since i will just increase infinitely. After the for loop is the very similar for each loop. A for each loop (or a for-in-list loop, in python) is used for iterating through arrays or lists. Essentially the loop will go through each element in the array and carry out some set of instructions for each value. If you would like to read all of the elements in an array and compare them to some value, or perform some operation on them, a for each loop is extremely useful. So for example, we could have a for each loop which iterated across an array and simply printed out the value of each array location. Next up, we have the while loop. A while loop will continually carry out its instructions while a conditional statement given to it is true. This can be as long as a certain
48:14 variable is true, as long as a number is less than another number, or while a value is still equal to another value. While loops are different than for loops in that the loop is not contained within one statement, but stretched out and will continue to run so long as its condition is true. Like a for loop, you can make the condition such that it will eventually return false and exit the loop, however while loops will not crash your computer should you create an infinite loop. In fact, it is very common for while loops to run infinitely, as, for certain programs, you would like the program to continually be iterated through instead of running once, all the way through until you exit out of the program. When programming a game, for instance, a while loop would be used to iterate through your code, continually refreshing the screen as the game runs. From there you could perform operations on the screen to make the game playable. Creating an infinite while loop would simply be done using the syntax while(true), as the condition true will always be evaluated as true. Finally, I’d quickly like to cover an extension of the while loop, the do-while loop. Do-while loops are very similar to while loops, except they will carry out their instructions at least once, even if the condition is false, and then will carry on like a basic while
49:16 loop. Essentially, the conditions inside the loop will run AT LEAST once, and then if the condition is still met they will run again and function as a normal while loop. As you can see, loops and their many varieties have some extremely useful functions. Using them, you are able to perform an operation many times in a row, you can iterate through arrays and lists, and overall decrease the clutter of your code. Next up, we’re going to take a break from learning about common programming statements, and dive into what happens when the code we write doesn’t work. More specifically, we’ll be covering the different types of errors that can occur when you’re programming, and what causes them. Now when you’re writing code, you have to understand that things aren’t always going to go the way you expected them, and sometimes the program doesn’t always work as you had intended it to. We programmers call these errors and while annoying, they are always going to come up in computer science and so it's best to learn what they are and how to deal with them. Often referred to as “bugs”, errors in scripting languages can be narrowed down to one of three “types”: syntax, runtime, and logic, all three of which we will be covering in today’s video. To kick things off, let’s talk about syntax errors. These are usually the easiest of the
50:27 3 to solve since they are oftentimes something that can be fixed within seconds. If you remember back to earlier in the video when we talked about syntax and programming rules, we said that if you were to break the programming rules, or syntax, that it would result in an error. Well that’s what syntax errors are, parts in your program where you fail to meet the programming rules and so the computer doesn’t know how to interpret your code. This can be anything, from forgetting a semicolon at the end of a statement in java, accidentally defining a variable with two words instead of one, or even just misspelling the word String when you’re trying to define a string variable. Lucky for you guys, these errors are extremely easy to fix since you just need to figure out where the error occurred and what the syntax rule you broke was. Now thinking back to IDE’s, we mentioned that IDE’s are so useful because they do precisely that, they underline syntax errors and usually provide helpful hints as to how to fix them. Think of syntax errors as small misspellings or grammatical errors in an essay you’re writing, annoying; yes, but not the most infuriating things. Another useful thing about IDE’s when it comes to syntax errors is that the program will actually restrict you from running the code unless all syntax errors are cleared, making them even easier to identify and fix.
51:40 The second type of error we will be covering is the runtime error. These errors don’t show until you actually “run” the code, hence the name “runtime” error. Runtime errors are caused by a statement in your code that SEEMS seems logically sound, but the computer physically has no way of computing it in a reasonable amount of time. The most common of these errors is one which we’ve already talked about; the infinite loop. As a refresher/example, think of an infinite loop like this. Say you sat your friend down in front of the TV, put on the Office on repeat, and told him he could leave as soon as Michael made a “That’s what she said” joke. Seems pretty simple right? WRONG, because instead of putting in the Office, you put in FRIENDS on blu ray. No michael, no inappropriate joke, meaning your friend would be sitting there for the rest of his life probably confused
52:26 as to why Dunder Mifflin looks so much like a coffee shop. This is essentially what happens with the computer, you give it some condition that it has to complete before the program can terminate; however, you give it no feasible way to finish that task. This puts the computer in error mode and most likely will crash your program, as the computer desperately tries to complete the condition you gave it. As a computer example, if we try to have a program terminate when an integer i is no longer greater than 99, but i is initially 100 and only increases, the loop will never terminate and the program will crash. To avoid these, you generally want to think through the flow of your code before running it -especially with loops- to make sure that all of your statements can be completed by the computer. Carefully planning out your code before you begin writing it is an extremely useful practice, and something
…