Introduction to Programming and Computer Science - Full Course
freeCodeCamp.org
0:00 Welcome to Introduction to programming.
0:02 My name is Steven in my name is Shawn.
0:05 Over the next 90 minutes,
0:06 we'll be taking you through this series consisting of 21
0:09 different segments that hope to cover the basics of computer programming,
0:12 which can apply to any and all programming languages you might want to learn.
0:16 We'll be starting with the simplest question of what is programming.
0:19 And from there, we will be working our way up as we
0:22 talk about common features of computer science such as loops and arrays,
0:25 we'll discuss how to read and write code,
0:27 debug code that you've written some strategies to help
0:29 you plan out your code and much, much more.
0:32 The complete list of topics that are going to be
0:35 covered in this lecture style video are shown on the screen.
0:38 Now, additionally, there will be timestamps in the description,
0:40 so feel free to skip around.
0:41 If you're already proficient in some areas of computer science,
0:43 or just want to know about a specific topic we will be covering.
0:47 Hopefully, by the end of the series,
0:49 you'll have a basic understanding of what computer science is,
0:51 along with an armory of useful skills that will
0:54 help you unravel whichever programming language you decide to learn.
0:57 First, we'll only be covering the major
0:59 key points that apply to all programming languages.
1:01 So we'll be shying away from topics such as object oriented coding,
1:05 and command line navigation, as those are things which are language specific.
1:09 Additionally, there will be no software required for you
1:11 to download in order to follow along with this tutorial,
1:14 as we won't be writing any code in an ID to keep things simple and concentrated.
1:19 This video is meant for those who
1:21 are interested in computer science and programming
1:23 but have no idea where to start
1:24 and have little to no background information on coding.
1:26 And so if that sounds like you, then strap in, as Shawn and I work
1:30 our way through the wacky world of computer science,
1:32 starting with the biggest question probably on your mind,
1:35 which is what even is programming?
1:36 Well, the dictionary defines it as the process
1:40 of preparing an instructional program for a device.
1:43 But that's a really confusing definition.
1:46 So in layman's terms, what exactly does that mean?
1:50 Essentially, it is attempting to get a computer
1:53 to complete a specific task without making mistakes.
1:57 Imagine this for example,
1:58 you want your less than intelligent friend to build a Lego set,
2:04 except he has lost the instructions and can only build based on your commands.
2:09 Remember, though, your friend is far from competent.
2:12 And so if they are not given very specific instructions on how to build the set,
2:17 there are many mistakes that they could make.
2:19 If he thinks like a computer, then if there's even one piece that you have not
2:23 told him specifically where to place and how to place it,
2:26 the entire Lego set will be ruined,
2:27 and you will be left to suffer a complete mental breakdown,
2:30 causing the whole goal of the project to be corrupted.
2:34 giving instructions to your friend is very similar to how programmers code.
2:39 Instead of a less than intelligent friend,
2:41 you have a less than intelligent computer.
2:43 And instead of instructions on how to build a Lego set,
2:46 we are feeding information on how to complete
2:48 a program like a game or a web application.
2:51 And an important thing to note is that computers are actually very dumb.
2:55 We built them up to be this super
2:58 sophisticated piece of technology, when in actuality,
3:01 a computer's main functionality comes from how
3:03 we manipulate it to serve our needs.
3:05 Now, programming isn't as simple as giving your friend instructions.
3:08 Since in a programmers case,
3:10 the computer doesn't speak the same language as you.
3:13 The computer only understands machine code,
3:15 which is a numerical language known as binary that is designed
3:18 so that the computer can quickly read it and carry out instructions.
3:23 Every instruction fed to the computer is converted into a string of ones
3:27 and zeros and then interpreted by the computer to carry out a task.
3:31 Going back to the Lego example,
3:33 this process wouldn't be like if he was not only less than intelligent.
3:37 But to make matters worse,
3:38 he could not understand English and only speaks in Mandarin Chinese.
3:42 In order to speak with him,
3:44 you have to convert the instructions that you understand
3:47 in English into the language that your friend understand.
3:50 This process is essentially what you must do for your computer
3:53 in order to make it understand the instructions that you give.
3:57 The big difference between the two examples, however,
4:00 is that it is very difficult for people to understand machine code in binary.
4:04 Directly translating what you want the computer
4:07 to do into machine code is extremely difficult, in fact, almost impossible,
4:11 and will take a very long time to do it if you could.
4:15 Each program is composed of millions upon millions of those ones and zeros.
4:19 So how exactly are we supposed to translate our instructions into machine code.
4:24 This is where programming languages come into play.
4:28 programming languages are fundamentally a middleman
4:30 for translating a program into machine code.
4:33 These languages are much easier for humans to learn than machine code,
4:37 and thus are very useful for programmers.
4:39 Going back to our Lego example, a programming language would sort of be like
4:43 an interpreter that's able to take the instructions
4:46 you give them in English and translate them
4:48 into instructions your non English speaking friend can understand.
4:51 This makes programming languages extremely useful
4:53 and the backbone of any good program.
4:56 Think of programming languages as not English and not machine code,
5:00 but somewhere in the middle.
5:02 There are many different programming languages out
5:04 there that each have their own unique uses.
5:07 languages such as Python and Java, are just general purpose languages that can
5:12 perform a variety of computational tasks,
5:14 or robots C or HTML or CSS, or languages designed for more specific purposes,
5:19 such as moving a robot or constructing a website.
5:23 languages can also vary and how powerful they are.
5:26 For instance, JavaScript is a scripting
5:28 language that is designed for smaller tasks,
5:30 or Java or Python can carry out much more computationally taxing processes.
5:34 We can measure a programming languages power,
5:37 or level by how similar it is to machine code,
5:41 the series of zeros and ones we talked about earlier.
5:44 low level programming languages, such as assembly, or C,
5:47 are closer to binary than a high level programming language,
5:50 such as Java or Python.
5:52 The basic idea is that the lower the level of your programming language,
5:57 the more your code will resemble what the machine can interpret as instructions.
6:01 Aside from the different purposes that each language fulfills,
6:04 choosing a language typically comes down to a matter of preference,
6:08 as are usually many languages that can accomplish similar tasks.
6:12 Try different languages,
6:13 and decide which ones rules interface and level of simplification you like best?
6:22 So now that we know what programming is, how do we actually write code?
6:25 It sounds like we can simply type words into a text document
6:28 and automatically assume that the computer can translate it into machine code,
6:31 read it and carry out a task like opening up a browser.
6:35 And additionally, we can't just write down rubbish
6:37 in certain programming languages mentioned in the previous segment,
6:40 and expect the computer to understand.
6:41 So how are we supposed to write code then?
6:44 Well, the answer is with an ID.
6:46 And ID, which stands for integrated development environment
6:49 allows the facilitation of code by a computer.
6:51 It is provide a graphic interface on your computer
6:54 in which the programmer can easily write,
6:56 run and debug code without having to worry
6:58 about problems with complication or interpretation of the program.
7:01 Think of an ID is any other program on your computer,
7:04 such as a game browser, or even the file explorer,
7:07 except we'll be using it to write code,
7:09 IDs are able to turn your code into machine
7:11 code and run it through the computer to produce results.
7:14 In addition to providing a place for programmers to develop their code IDs
7:18 provide some extremely useful tools for programmers
7:20 to ease the job of writing code, such as built in error checking,
7:24 because as we'll talk about later, code doesn't always run correctly.
7:27 Auto filling for frequently used words or phrases, and a project hierarchy,
7:30 which will help you organize and manipulate the files within your project.
7:34 Back in the olden days before IDs code used
7:37 to be written on punch cards and then fed into computers,
7:40 which would take hours and cause a lot of pain.
7:42 IDs nowadays act as sort of a fast track to writing
7:45 code and make things a whole lot easier for programmers.
7:48 An example of a specific ID can be seen on your screen now.
7:52 In the center, you can see the program that is currently being written.
7:55 And right below it is the console,
7:56 which can print out useful information for the programmer.
7:59 This specific ID is used to write Java code.
8:02 IDs are extremely powerful and will be
8:05 used in almost 100% of your programming projects.
8:08 So through these IDs, we are finally able to write and compile code smoothly
8:12 without worrying about the computer not being able to understand it.
8:15 The next problem we run into then becomes how do we write this code in the ID.
8:19 Because it's not like we can just
8:21 type random words from a certain programming language
8:23 and expect the computer to understand this is
8:26 where a programming languages syntax comes into play.
8:28 Now, just as if you were learning a real language,
8:32 learning a computer language can be very similar.
8:34 Some have different styles that may seem odd.
8:37 Some may make you use weird or abstract concepts which may be confusing.
8:41 And like all languages, programming languages have a set of rules that you
8:45 must follow when writing code in that language.
8:47 And at the forefront of those rules is grammar.
8:49 Programming grammar is referred to as syntax
8:52 and is very similar to real world grammar.
8:54 Each programming language has its own syntax or rules that you have
8:58 to follow to a tee if you want your program to run correctly,
9:02 just as if you were speaking in real life.
9:04 These can be things such as how you type out certain functions,
9:06 what you put at the end of the line of code,
9:09 and how you set up certain functions.
9:11 Each language is unique in its syntax, and while some may share similar rules,
9:15 all will have some quirk which makes it stand out from the rest.
9:19 syntax is something that catches a lot of people off guard since
9:22 many expect every programming language to follow the same set of rules.
9:25 But as we spoke about in the last segment,
9:28 because each language is specialized for a specific task,
9:30 each needs its own set of rules to function.
9:34 Breaking or disregarding these rules will result in an error.
9:37 Just how breaking or disregarding rules in real
9:39 life will result in an unintended message.
9:42 As an example, if you wanted to do something simple,
9:45 such as initialize a variable, which is something that we haven't covered yet,
9:48 but the example is still relevant.
9:49 In Java, you'll notice how we have
9:51 to specify what type of variable we're defining,
9:54 in this case an integer and also add a semi colon after the statement.
9:58 In Python, we don't even need to define that we are trying
10:01 to create a variable and just have to type what we want to create.
10:04 And in JavaScript, we just specify that we are making a variable,
10:08 but we don't include what type of variable we want to make like in Java.
10:12 Even in this simple example,
10:13 you can see how much a syntax matters when learning a new language.
10:16 Since while the goal of our program remained the same,
10:19 which was to define an integer with the value three,
10:22 all the programming languages shown took different approaches.
10:25 All these languages require that you follow this syntax because remember,
10:29 computers are extremely dumb.
10:31 If you forget one semi colon or misplace a character,
10:34 the entire program will not run and send you back a syntax error,
10:37 which is something we'll talk about later.
10:39 Think of this as if you forget a comma in a sentence
10:42 and the entire context of what you're trying to say gets misinterpreted.
10:46 For example, in the sentence, let's eat grandma.
10:48 If you were to forget that comma.
10:50 While it may seem like a small mistake,
10:52 it changes the entire context of the sentence,
10:54 making it sound like you're going to eat your grandma.
10:56 The same rules follow for programming.
10:59 If you forget a semi colon, the entire context of your program can
11:03 be corrupted and misinterpreted by the ID.
11:05 Now another thing which makes it so useful is that they will
11:09 let you know if and when there are syntax errors in your code.
11:12 syntax errors, of course, being parts of your code,
11:14 which do not follow the same rules where we talked about previously,
11:17 the ID will tell you where in your code the errors,
11:20 and also won't let you run your program until the error has been fixed.
11:24 Because of how important syntax is to writing code and learning a new language.
11:28 It's recommended that you learn the rules and syntax
11:30 of a language before beginning to write complex programs in that language.
11:34 Most of the rules are tedious to learn but easy to master.
11:37 And as soon as you can do that, you'll be able to easily identify
11:40 syntax errors and take care of them easily and be writing code in no time.
11:44 That covers the basic gist of syntax and programming rules.
11:47 So now that we know how to write code and where to write code,
11:50 we next need to cover what happens after we
11:52 have typed out our program and run our code.
11:54 Because writing a piece of code for a game or for a database is cool and all.
11:58 But after the computer interprets the program,
11:59 how will we know what's happening and whether or not is working?
12:03 Well, programmers do this by looking at the console.
12:05 The console is a text interface within your computer
12:07 that us programmers can use for a variety of different purposes.
12:11 If you remember, a short while ago, we heard a picture of a basic ID.
12:14 And one of the main parts of the picture was the console.
12:17 The main use of the console is to output text from the program.
12:21 This is usually done by using a print statement.
12:24 A print statement is a command that does exactly what it sounds like.
12:27 It prints text to the console.
12:28 The print statement is the first piece
12:30 of actual code we've talked about in this series.
12:32 And it's about as simple as the print statement.
12:35 Despite its simplicity is one of the most important functions in programming
12:38 and exists in some form in just about every programming language.
12:41 The most basic thing you can ask the print statement
12:44 to do is to just simply make it Say something.
12:46 This is done by instructing the console to print,
12:49 and then include wherever you want to be printed inside the parentheses.
12:52 For example, in Python, the segment of code print hello world will cause
12:57 a message reading HelloWorld to appear onto the console.
13:00 Pretty neat.
13:01 The print statement is also vital
13:03 for viewing interpreting the computer's output from program.
13:05 For instance, if you could tell a computer to run a simple calculation.
13:10 For example, to determine what four plus three is,
13:12 they will run the program internally and compute an answer.
13:15 However, what is the purpose of having the computer run this program
13:18 if you will not be able to tell what the result is,
13:20 instead of simply telling the computer to perform this calculation,
13:22 instruct the computer to print the output of the program to the console,
13:26 and upon the program's completion, seven will appear on the console.
13:29 As you can see, the console allows us to easily
13:31 print information out to the developer for a variety of uses.
13:34 It is important to note that the print
13:36 statement varies depending on the programming language being used.
13:38 For example, in Java,
13:39 there are multiple versions of the print statement depending upon whether
13:42 you would like a line break after the printed text or not,
13:45 as well as specialized print statements
13:46 which make your code run more efficiently.
13:48 Also, the general syntax of using a print statement
13:51 and certain nuances of its function contains between languages.
13:54 However, you can generally rely on it to carry out the same
13:57 overall function as it is a foundational statement for programming in general.
14:00 All of its functionality makes a print statement
14:03 along with the console and very useful developer tool.
14:06 However, it is important to remember that that is all it is the developer tool.
14:10 The console is not really meant to be viewed by the end user of your program.
14:14 It tends to be hidden away behind
14:16 the scenes and other methods of displaying information, such as displaying text,
14:19 graphics, or images are used to convey information to the user instead,
14:23 think of it like this.
14:24 When you're using your phone,
14:25 you see the console and none of the programs you use.
14:28 So while you can use the console to give
14:30 yourself information about how your program is performing,
14:32 don't try to implement it in the final
14:34 product because it fundamentally just isn't meant for that.
14:36 Overall, remember to use the console to its fullest
14:39 extent when writing and fixing problems in your programs,
14:41 as it is a great tool to use
14:43 to tell how your program is performing behind the scenes.
14:47 So now that we know a bunch of information about programming languages,
14:50 and how and where to write them, along with the print statement under our belts,
14:54 let's go over some intuitive things that the computer can do
14:58 all by itself without you having to tell it how To.
15:01 More specifically, we'll be covering some basic
15:03 number mathematics as well as string math.
15:05 Starting off with basic mathematics,
15:07 the computer already knows how to do simple arithmetic.
15:10 This includes addition, subtraction, multiplication, and division,
15:12 all of which are represented by the symbol shown on the screen now,
15:16 in any ID that you may install, you'll be able to print out the answer
15:20 to simple math problems using the print statement.
15:22 Which may seem counterintuitive,
15:23 because why would you use the computer to do math
15:26 when you have a perfectly good calculator on your phone.
15:28 But you have to remember that computers are dumb.
15:31 And anything we want a computer to do, we have to build up from scratch.
15:35 basic arithmetic, while simple helps out in almost any program you may write.
15:40 For example, if we wanted to build a basic calculator app,
15:43 we need to utilize this functionality in order to correctly display
15:47 the answer to an arithmetic problem when our user tries to add,
15:51 subtract, multiply or divide two numbers.
15:53 Now in addition to the four basic math equations,
15:57 most programming languages include an additional operator known as modulus.
16:00 If this is your first time hearing this word,
16:02 don't worry, since it's not usually taught in math classes.
16:06 modulus allows us to get the remainder of a divisional operation.
16:09 For example, when we take 10 modulus three,
16:12 we're essentially telling the computer to take 10 divided by three,
16:16 ignore the actual answer and just give us the remainder of the operation.
16:20 In this case, one, since when we divide 10 by one,
16:23 the answer is three remainder one.
16:25 The one in this case is what gets printed out to the console.
16:30 If there is no remainder, say in the case that we take 50 modulus two,
16:34 since the remainder is zero,
16:36 the function would return zero if we were to print it out to the console.
16:40 This can be extremely useful.
16:42 In many cases, the most obvious being if we want
16:45 to determine whether or not a certain integer is even or odd.
16:48 If we take a certain number, modulus two, and it returns zero,
16:52 then we know that is even because any number divided
16:56 by two will always result in a full answer without a remainder.
16:59 But if the system returns one, then we know that the integer is odd,
17:04 you will find yourself using the basic math operators a lot more than you think.
17:07 So it's good to keep them in mind when running your program.
17:10 Now, not only can our computer work with numbers,
17:13 but it can also work with strings.
17:16 strings, by the way, are just another way to say text.
17:19 For example, hello world is a string, the letter A is a string,
17:22 and anything enclosed by quotation marks is
17:25 denoted as a string in programming languages.
17:27 We'll cover more about strings in a bit when we talk about variables.
17:31 But for now, let's continue.
17:32 We already talked about printing strings to the console.
17:35 But let's say we're making a game and we wanted to print out the statement,
17:39 game over for was your final score.
17:42 Now while we could just make a string that says that exact
17:45 phrase and print it out to the console, in some cases,
17:47 it would be more useful to print out the actual integer value,
17:50 especially in the case of a game where the score can change each time you play.
17:54 Because score definitely isn't always going to be for.
17:57 Well, we're also able to print out multiple strings of text
18:01 and even integers by adding them together in the print statement.
18:05 This is known as concatenation.
18:07 Continuing with our score example,
18:09 if we wanted to print out the statement gameover for was your final score,
18:13 using four as an integer rather than a string.
18:16 We could do this by breaking down the statement
18:18 into two strings and an integer like so.
18:21 Print game over plus four plus was your final score.
18:26 We of course begin with a print statement,
18:29 which again will be different across all languages.
18:31 But in this case, we're using Python.
18:33 Inside the print statement, we start off by printing the string game over.
18:38 Now here comes the important part.
18:40 From there, we use a plus sign and add four to the print statement.
18:44 Just like if you were adding two numbers,
18:46 then we can repeat this process with another plus
18:49 sign for the final string was your final score.
18:52 And we're able to print out the entirety of our statement easily.
18:55 Doing this we can easily print out multiple
18:58 different strings and integers together in one print statement.
19:01 We could also combine the two lessons we've learned thus far and do
19:06 something such as game over plus four plus four plus was your final score.
19:11 In the case, let's say where you have a game which gives you
19:15 a base score and then four points for a certain task that you complete it.
19:18 This demonstration also displays Another important part of programming,
19:22 which is that oftentimes to get your program to be the most efficient,
19:25 you have to combine aspects of code.
19:27 Now it's important to note that the computer will take whatever
19:30 you put in the parentheses and print it out character for character.
19:34 So oftentimes programmers will forget to add
19:36 a space onto the end of their strings.
19:38 This can result in a small mistake in which the string from the previous
19:42 example would be printed out as if to say gameover four was your final score,
19:46 which isn't that appealing when displayed on the screen to the user.
19:49 So it's a good practice to always put a space
19:51 after and before your strings to make sure this doesn't happen,
19:55 and your string doesn't end up like that.
19:57 Another important thing to note is
19:59 the difference between four in quotation marks,
20:01 and four without quotation marks.
20:03 Now for in quotation marks is treated as a string,
20:08 rather than a four without quotation marks, which is treated as a number.
20:12 This may not seem like a big deal, but again, computers are dumb.
20:15 And if you try to do math with a number in quotation marks,
20:19 it will return an error.
20:20 Because the computer doesn't understand
20:22 that you're trying to perform the operation
20:24 on a number and thinks you're trying to add an integer to a string,
20:27 which is a big no no in programming when you're adding numbers.
20:31 So when you're programming, make sure to make a mental note of whether
20:34 or not you want to make something an integer or string,
20:36 because that type of stuff makes a big difference.
20:39 All right, that concludes our segment on the base power of computers.
20:43 Now Next up, we're going to be covering
20:45 one of the most important components of computer science.
20:48 So make sure you pay attention because
20:50 next up we're going to be discussing variables,
20:52 what they are and how we use them.
20:54 First of all, what exactly is a variable?
20:57 A variable is simply something that can
20:59 store information and can be referenced and manipulated.
21:01 Think of variables like a cardboard box.
21:04 cardboard boxes, servers means to store items in them,
21:07 which can be changed that replaced and modified variables are like
21:11 cardboard boxes to store information
21:12 for the programmer to reference or manipulate.
21:14 each variable simply has a type, a name,
21:17 and a piece of information stored inside of it.
21:19 The type and piece of information will be covered next,
21:21 and the name is simply a name for the variable.
21:23 Think of it as writing out a label on the cardboard box in Sharpie.
21:27 Now there are many different types of variables
21:29 that a programmer can use, or right now,
21:31 we will just be covering what are called primitive variables,
21:34 which include integers, Boolean, floats, and doubles, strings, and characters.
21:39 We'll start off by talking about an integer.
21:42 An integer or int for short, is as simple as it sounds,
21:46 a variable that can store an integer value.
21:49 This includes all whole numbers from negative 2,147,483,648 to 2,147,483,648.
21:54 Notice how I said whole numbers,
22:00 integer values can not and will not hold any decimal values.
22:06 So keep that in mind when using variables.
22:09 Secondly is a Boolean.
22:10 A Boolean is very primitive variable which can
22:13 store a value of either true or false.
22:15 Boolean variables can only hold these two values,
22:18 and are extremely useful for conditional statements, which we will cover soon.
22:22 The next few types of variables are floats and doubles.
22:25 Both of these variable types are floating point data types,
22:28 which essentially means that these variables
22:29 can store numbers with decimal places.
22:32 Whereas integers values cannot hold decimal values, floats and doubles can.
22:35 The main difference between the two is that a float
22:39 variable can store numbers of our precision up to 32 bit.
22:42 All doubles can store numbers with a precision up to 64 bit.
22:45 Essentially, a double can store more decimal places than a float.
22:49 So it all comes down to how precise you want the variable to be.
22:52 Up.
22:52 Next, we have string variables,
22:53 which are like the strings we've talked about beforehand,
22:56 except store somewhere in a value.
22:58 String variables can store strings of letters,
23:00 which are just words and sentences.
23:02 Strings are useful for displaying text and storing input information.
23:05 Strings can also be concatenated together to form
23:08 combinations of string variables and pre written strings.
23:10 This can be very useful for outputting
23:12 information in a readable format for the user.
23:14 For example, imagine we have a string called name.
23:17 The code asked for input and storage that string of text and name.
23:20 To output this information to the user.
23:22 Rather than simply displaying their name, you can add the phrase Your name is
23:26 and make it into a sentence by concatenating.
23:27 Your name is plus a name plus a period.
23:30 This makes it easier to read your code,
23:32 while also adding variability to your code,
23:34 which always makes things more interesting for the end user.
23:38 Finally, we have char variables.
23:39 char stands for character, and just as a name suggests,
23:43 they can each hold one character.
23:44 This is useful when a programmer wants to read one button
23:47 press or one character in a string without using a string variable.
23:51 a specific example is being a game that is controlled by the keyboard,
23:54 the program needs to recognize the character that is
23:56 pressed and translate that into carrying out some function.
23:59 Now strings can also hold one character,
24:01 but chars can hold more than one character.
24:03 So keep that in mind when defining variables.
24:05 Now, why are variables so useful?
24:07 Well, being able to store information in a format that can
24:11 be easily referenced later is essential for any good program.
24:14 Oftentimes, in code,
24:14 you're going to want to keep track of things such as a user's name, or score.
24:18 And so by creating a variable called name or score,
24:20 you store this information in the variable and then reference it,
24:23 add to it or modify it.
24:25 Also, many times to program will want to take input from the user,
24:28 which cannot be pre programmed into the code,
24:30 and thus the variables required to store the information.
24:32 A program may also rely on factors that will change as the program progresses,
24:36 in which case a variable is once again required.
24:39 Also, taking these variables and manipulating
24:41 them is quite necessary for carrying
24:42 out many of the tasks you want to program to carry out.
24:45 For instance, multiplying in variables or concatenating string variables.
24:49 Overall variables are the backbone of any program,
24:51 and you'll find yourself using them often
24:53 if you want to clean and efficient code.
24:55 So it's best that you learn what types of variables you need to use and when.
24:58 So now that we know all about the different variable types,
25:01 and we've talked about them a little bit,
25:02 we're going to delve further into what happens
25:05 when we actually define or create a variable,
25:07 how we reference them, and how we can manipulate them for our programs.
25:11 To start, let's go over what happens when we actually define a variable.
25:15 Now when we write a line of code,
25:17 which initializes a variable, and that code is executed,
25:20 the computer essentially creates a little space in memory that stores
25:23 your variable name and its contents so that it can be referenced later.
25:27 Going back to our cardboard box example from the previous segment.
25:30 Think of this as if you had your own storage facility,
25:34 and you make a new cardboard box labeled name.
25:37 And inside of it, you put a piece
25:39 of paper with the word no pointer exception on it.
25:42 Now, anytime you want to know the contents of your name box,
25:45 you could simply look inside and see
25:46 that it has the contents no pointer exception.
25:49 This is what the computer does,
25:51 except the storage facility is the memory in your computer.
25:55 The box is a variable and the contents of the box
25:58 or whatever the variable is set to be equal to.
26:01 Anytime you want to know the contents of the name variable,
26:04 you can simply call it and the computer will
26:06 pull the information that is stored in that variable,
26:09 and use it how the user see fits.
26:12 Another thing to note really quickly is that you can
26:15 actually make a variable without putting information inside of it.
26:18 This would simply be like if you built up a new cardboard box,
26:22 gave it a label with a sharpie, but just didn't put information inside of it.
26:26 You're simply saving that space in your storage facility for later.
26:30 This can be maybe because you want
26:32 to store information in it later down the road.
26:34 Or if you're going to use it to store information given to you by the user,
26:38 in which case you can't give it information
26:40 since you don't know what the user will input.
26:43 Just to note that if you try to reference or point to a variable,
26:46 which does not have any information in it,
26:48 you'll get what's known as a null pointer exception,
26:50 which despite being an amazing name for a YouTube
26:53 channel is something you generally want to avoid when programming.
26:56 Now programming languages allow us to do some
26:59 pretty cool things with these boxes that we've created.
27:02 For example, let's say we created a second variable called channel name.
27:07 And instead of setting it equal to no pointer exception,
27:09 we instead set it equal to our already created named variable.
27:13 This doesn't create a space in memory for this new variable.
27:17 However, it simply points to the same location
27:20 of memory we have already created for the name variable.
27:23 Going back to our storage facility example,
27:25 this would be like instead of creating a whole new box label channel
27:29 name and storing sheet of paper with the word no pointer exception on it,
27:33 we instead simply added another label below the name box,
27:37 and titled channel name.
27:39 Now we have two variables which point to the same contents,
27:43 that being the string no pointer exception,
27:46 we usually do this to save space in our code
27:48 for things that we know are going to have the same value.
27:51 variables can also be updated throughout your code.
27:54 For example, let's say you had an age variable,
27:56 and inside of it was the integer 17.
27:59 Then you celebrated a birthday and wanted to update your age,
28:02 all you would have to do is reference the variable and set
28:06 it equal to whatever new integer you want the variable to hold.
28:09 In this case, 18.
28:10 This would be the same as having a box labeled
28:13 age with a sheet of paper eating 17 inside of it,
28:16 and then taking that piece of paper out, erasing 17,
28:19 replacing it with 18 and then placing it back in the box.
28:23 Doing this we are able to easily update the contents
28:26 of our variables throughout the code as things dynamically shift.
28:29 As another example, if you were making an RPG,
28:33 your character would likely have stats such as attack, defense manner, etc.
28:37 As the game progressed, you could continuously update the variables so
28:41 that the player could get more powerful,
28:43 the further along they went through the game.
28:45 And you wouldn't have to create new variables,
28:47 you would simply need to just keep grabbing that box from your storage facility,
28:51 erasing and replacing the numbers on the piece
28:53 of paper and then continue along with your code.
28:56 Just keep in mind that these variables are nothing more
28:59 than places in memory in which a certain value is stored.
29:02 So we can easily update the numbers and their place will remain constant.
29:05 After the code has run its course the place
29:08 in memory is deleted until you run the code again,
29:11 and the program dedicated space for the variable again,
29:13 each time you run the code, you're making new boxes in your storage facility.
29:17 And at the end of the code,
29:19 you destroy them all to make room for the new boxes next time.
29:22 Another cool thing you can do with integer
29:25 float and double variables is add them subtract them,
29:28 divide them, multiply them and even modulus them.
29:32 For example, if you're making a calculator app and you store the first number,
29:36 the user entered as number one and the second as number two.
29:40 You could then multiply num one and num two together and either
29:44 print them or store them in a new variable entitled result.
29:47 Then each time you run the program,
29:49 the user could input new numbers into the number one and number two variables.
29:53 And they would simply be set to those new integers that the user
29:57 inputted and return the result that correspond Through those specific numbers,
30:01 this allows you to keep easy track of which numbers are which,
30:04 and what's going on in your program, which is extremely useful.
30:08 Also, while you cannot subtract, multiply,
30:10 divide or take the modulus of strings, you are able to add them.
30:15 Like we said previously,
30:16 let's say you had a string str one with the contents Hello,
30:20 and a string str two with the contents there, you could add str one and str two
30:26 to create a string that had the contents Hello there,
30:29 either storing it in a third variable or printing it out to the console.
30:33 The last topic we'll be covering on the topic
30:36 of variables is the naming conventions of variables,
30:38 which will be it may seem odd,
30:40 but it's extremely important when trying to read your code.
30:43 So we'll be covering it now.
30:45 Now variables have to be one continuous string.
30:47 And so if you wanted to make a variable that store the player score,
30:51 you'd have to find some way to combine the words player and score.
30:55 Since you can't have the phrase player score be the name of a variable.
31:00 All programmers have their own personal
31:01 preference when it comes to naming variables.
31:03 But the one we'll be using in this lecture is called camelcase,
31:06 which is the process of not capitalizing the first word,
31:10 but capitalizing every word that follows it.
31:12 Going back to the player score example,
31:15 using the camel case method, the variable would be called player score.
31:19 This allows us to easily see and identify each
31:22 word and becomes really useful for long variable names,
31:25 like the player score before the final boss.
31:28 Whereas if we just type it out without camelcase,
31:31 it would be really confusing and hard to read.
31:34 This will help you out a ton when we
31:37 start finding bugs in our code and need to quickly
31:39 scan through our program to figure out what is
31:41 wrong and adds to the overall readability of the program.
31:45 Other programmers might use different naming conventions like
31:48 using underscores to separate the words in a phrase.
31:50 But for now, and this series, we'll be sticking with camel case.
31:55 Next, we'll be moving on to conditional statements, which at their core,
31:59 are statements that change the path of our code depending on certain conditions.
32:02 For the sake of keeping things simple for this section,
32:05 where Atlanta will connote that our code
32:06 will not be following the specific path.
32:08 In Greenland's I mean, our code is following the path.
32:11 The main type of conditional statement that programmers use is the if statement,
32:16 and this will show up countless amount of times in any program you write.
32:20 It is as simple as it sounds, if some condition is true,
32:24 and usually that condition will be enclosed by braces,
32:27 then carry out the instructions located within the if statements brackets,
32:32 else do another thing.
32:34 Now, brackets are used in most programming languages to indicate
32:37 a segment of code which will run it works like this.
32:41 If the condition in parentheses is true,
32:43 then all of the code contained within the brackets will run.
32:46 And if the condition within the parentheses is not true,
32:49 then it will skip over all the statements within the brackets.
32:52 A quick note is that while this is the case,
32:55 with most programming languages, some,
32:57 like Python, use columns and whitespace to determine
33:00 where a piece of code starts and ends.
33:03 But for the sake of this series, we'll be using curly braces.
33:07 Now, the condition within the parentheses can take on 1000s of different forms,
33:11 such as if the value of the string variable name is equal to Steven,
33:15 or if the player score stored in an int variable is greater than five.
33:20 The list goes on and on.
33:21 Each of these statements is evaluated as a Boolean,
33:24 which you will remember from when we
33:26 talked about variables is either true or false.
33:29 If the Boolean is true, we run the code inside the curly braces.
33:33 If it's not, we pretend everything inside the curly braces never existed,
33:37 and move on with our code.
33:39 The if statement comes with two more additional
33:42 statements that go with it elsif and eltons.
33:45 elsif is a conditional statement used directly after an if statement,
33:49 and carries out mainly the same function as an if statement.
33:53 However, the elsif statement will only be evaluated if the proceeding F
33:57 or the preceding elsif statement is bypassed due to its condition being false.
34:02 So we will run through it like so.
34:04 If something is true, we will run the code inside of that statements
34:08 curly braces out if that's something is not true.
34:11 But another statement inside of the parentheses is true,
34:14 we would then run that code segment.
34:16 And if neither of them are true,
34:18 we would skip both segments of code and move on in our program.
34:22 This can be a hard concept to wrap your head around to so let's do an example.
34:26 If we had a program that evaluated the if statement, if age is equal to 10,
34:30 we then have a statement under that which stated outs if age is equal to 12.
34:34 Now, if the age variable was 10, which we can see from the example that it is,
34:39 then the code immediately following that conditional
34:41 statement in the brackets would run.
34:42 The Ultra statement we made will not even be tested.
34:45 Since we know that it is going to be false.
34:47 And thus the print statement inside
34:48 of that conditional statements brackets will be ignored,
34:51 and the code will move on to the rest of the program.
34:54 Now for example, let's say we change the age variable to be 12 instead of 10.
34:58 Now instead of the first conditional statement being true,
35:01 it actually evaluates as false since age is no longer equal to 10.
35:04 So what we do now is first skip over the print statement,
35:07 which prints out that ages 10 and does not.
35:09 And then we evaluate the elsif statement,
35:11 we check if age is equal to 12, which again it is.
35:14 And so now we run all the code inside of that conditional
35:17 statement before finally moving on to the rest of our program.
35:20 So as a review, we check the initial if statement.
35:22 If it's good, then we run all the code within
35:25 that if statements curly brackets and move on with our program.
35:27 If the initial if statement is not true, we then move on to any elsif statements
35:31 and evaluate if those conditional statements are true.
35:34 We can have as many elsif statements as we want,
35:36 although this could lead to clutter amongst your code.
35:38 So we'll talk about some alternatives later to help us out.
35:41 Now that takes care of the FL statement.
35:43 So now we'll move on to the else statement.
35:46 The out statement, once again, it comes after an IF or an elsif statement,
35:49 and we'll carry out its instructions no matter what.
35:51 As long as the proceeding statement slash statements are evaluated as false.
35:55 If we went back to our previous program, we could add an elf statement,
35:59 which would only have the code in his brackets
36:01 run at the age variable wasn't 10, or 12.
36:03 This will catch all cases of the program that didn't fit into our parameters.
36:06 It's good practice to always have another
36:08 statement at the end of your conditional
36:10 statements to catch any weird cases that may come up in your program.
36:12 Now remember back to the fact that we could have 1000s
36:15 of Elif statements after a while that can get pretty cluttered.
36:18 And so another very useful conditional statement
36:21 helps circumnavigate this problem is a switch statement.
36:23 A switch statement is functionally similar
36:25 to many if and else statements together,
36:27 you write a switch statement in the form of switch variable.
36:29 And then below that you write out how many cases the variable can be.
36:33 For instance, if we wrote switch var, and then under read write out five cases
36:36 that the variable var could be and then the instructions
36:39 listed under each case would be carried out if
36:40 the var variable it would be equal to that case.
36:43 Now switch statements are different sets.
36:44 Instead of using brackets, they use a column to signify the start of a set
36:48 of instructions and a break statement to end them.
36:51 This is very useful, because you are able to essentially use many if
36:54 and else if statements without having to write nearly as much.
36:56 In switch statements,
36:57 you just always have to remember to include default case at the bottom
37:00 of the expression to denote any and all
37:02 cases that don't meet the above requirements.
37:04 There simply catches all the inputs
37:06 that don't fit within the program's main cases.
37:08 It's very similar to just case the end of an if else chain.
37:12 Now, why are these statements so useful?
37:14 Well, many times programmers want their programs
37:16 to function differently depending on different conditions.
37:18 For instance, a program could function differently
37:20 depending on the information that the user inputs,
37:22 such as allowing user to use a program or not user
37:25 program if they are above or below 18 years old, respectively.
37:28 Or in say, a video game.
37:29 If the user experiences above a certain threshold,
37:32 you might want to give them harder opponents to battle.
37:34 Another example could be a program was changing
37:36 the color scheme depending on the time of day.
37:38 Or even more simply,
37:39 if a user presses a button that is meant to move on to another screen in an app,
37:43 the programmer would only want that app
37:44 to change screens if the user clicks that button.
37:46 A program without conditional statements would do the same thing every time,
37:49 and would be very primitive compared to one
37:51 that can change depending on its conditions.
37:53 So now that we know how to make and use variables, how to compare them,
37:58 and what we can do with those comparisons,
38:00 let's move on to another foundational concept of computer science.
38:03 And that is arrays.
38:04 Now we've already talked about variables,
38:06 and how great they are for storing singular
38:09 bits of information for making our code more simplistic.
38:12 But one of the biggest drawbacks that comes with variables is
38:15 their inability to hold more than one piece of different information.
38:18 For example, let's say you're making an app
38:21 which allows a user to create a grocery list.
38:23 Well, there's no real easy way to create lists using variables.
38:26 Because it's not like you can have one variable
38:29 store the names of six or seven different food items.
38:32 Remember, we can only put one piece of paper in our cardboard box no more.
38:37 And besides, even if you were able to add multiple items to one string variable,
38:41 you would still have a lot of trouble doing simple tasks
38:44 you might want from a list such as searching through it,
38:47 splitting it or even deleting items from the list when you're done with them.
38:51 This is the problem that using arrays solves for us.
38:54 An array is as you may have guessed by now,
38:57 a list, you can have an array of integers,
39:00 an array of strings, and even an array of other arrays,
39:03 which is something we'll cover in a minute.
39:05 Programmers use arrays when they want to store a lot
39:08 of variables containing information that is all related to each other,
39:10 such as a grocery list or a high score list in the game.
39:14 Think of arrays as a column in Excel or Google Sheets.
39:17 Here the title at the top and then below it are a bunch of bits of information,
39:21 which all relate back to the title.
39:23 Arrays are super useful when programmers want to store a lot
39:26 of information that can be easily searched through because programmers
39:29 have developed methods of breaking down and using arrays to find
39:32 specific information in arrays full of 1000s of different variables.
39:35 As an example to show just how useful arrays are.
39:38 Let's say you're a startup company that owns an app that has 100,000 users.
39:42 Every time a user wants to create a new account,
39:45 they input the username they want and then your program will have
39:47 to check to make sure the account name hasn't already been taken.
39:50 Doing this requires you to search through
39:52 the information of all 100,000 of your users
39:55 to see if that username has an account with your service and array would be
39:59 able to get Pain all of this information and make it easy to search through
40:02 and find out if the account name has already been taken with little to no delay.
40:07 Now, the single most important thing to note about arrays
40:10 is how you reference each element of the array within them.
40:13 Let's create a basic array called numbers and inside of it,
40:16 put the digits one through 10.
40:18 Now, when we want to refer to each cell in his array,
40:21 we would call upon its index.
40:23 an index is just a fancy way of saying that numbers placed within the array.
40:27 Now you would think that the first integer
40:30 in this array would be the first index,
40:32 the second would be the second index and so on.
40:35 But that's simply not the case.
40:37 In computer science,
40:38 programming languages refer to the first cell as zero with element in the array.
40:42 This means that if we were talking about our array of numbers we just made,
40:46 the number four would actually be in the third index,
40:49 five would be in the fourth, and so on.
40:51 So instead of starting our count from one, we start from zero.
40:55 It's extremely weird and confusing, but it's one of those programming quirks,
40:58 you're going to have to memorize and commit to memory.
41:00 If you were to not follow this nomenclature,
41:02 and refer to the last element in this array as the 10th,
41:05 you get what is referred to as an array out of bounds error,
41:09 since you're trying to reference the 10th element, but there is no 10th element,
41:12 where you're actually trying to do is reference the ninth element.
41:16 Another extremely important thing to note about
41:18 arrays has to do with their size.
41:20 When you initialize an array, you can do it in either one of two ways.
41:23 You can either populate it with the elements that you
41:26 want contained in the array right then in there,
41:28 creating and filling the array at the same time.
41:31 Or you can define how many elements you want the array to hold,
41:35 essentially the array size, and then populate it with elements later.
41:39 This is because when we initialize an array,
41:42 it creates a space in memory that has a size of exactly what you give it.
41:46 no more and no less.
41:47 This is great for when we want to access elements in the array,
41:51 because we can do so instantaneously because the computer
41:54 knows exactly where in memory the array is stored.
41:57 But the one downside is that we can't increase the size of the array later on.
42:02 All array sizes are final.
42:04 Think of this like setting up a bookshelf with books
42:08 by populating a bookshelf with a certain number of books.
42:11 And then moving on and filling the next shelf with different books,
42:13 we have no way to go back and add
42:16 books to that first shelf without shifting everything over.
42:19 Once we decide how much space to dedicate for an array,
42:22 in this case, there's no way to add more space.
42:24 Once again, because this is extremely important to remember.
42:27 This means that once an array has been defined,
42:30 there is no way to change the size of it.
42:33 If you have an array titled names with a size of eight,
42:36 and you try to add another name to the array, you will receive an error.
42:40 So be careful when messing around with array sizes.
42:42 Of course, you can always go back to the start of your code,
42:45 when you initially make the array and allocate more space to it,
42:49 increasing its size.
42:50 If you find out that you need more space to hold items,
42:53 but once it's defined, you cannot change its size through conventional methods.
42:57 Another small thing I want to touch upon
42:59 really quickly is that when you initialize an array,
43:02 you must determine which type of array it is right then in there,
43:05 for example, you have to specifically say it will
43:08 be an array of strings or integers when defining it.
43:11 And also you're not allowed to mix and match.
43:13 Meaning that you can't have an array full of integers
43:16 with a few strings and some doubles thrown into the mix,
43:19 they all have to be the same type.
43:21 Now the last thing we're going to cover on arrays is a little funky.
43:24 And that is the practice of putting arrays inside of arrays.
43:27 If you make an array of arrays is referred to as a 2d or two dimensional array.
43:32 Think of these as matrices if you've taken an algebra class before.
43:36 Now, if you haven't Think back to our Google Sheets example,
43:39 but instead of using columns, we would add rows as well.
43:42 So now each element in an array would simply
43:45 just be a string variable or an integer variable.
43:48 But an entirely new array with its own set of values and elements.
43:53 The way we index these is mostly the same,
43:55 except we would have two numbers to index instead of one.
43:58 We start with the row and then the column.
44:01 So a number in the positions 02 would be in the first row,
44:04 three columns down, in this case, the name Clint a number in the position one
44:10 one would be two rows down and to columns across,
44:14 in this case, the name Chris, you get the idea.
44:17 Now you can also make three dimensional arrays by putting
44:20 an array inside of an array inside of an array,
44:22 but that's a little above what we're going to be covering.
44:24 So I'm going to cut it off there.
44:26 Next up, we're going to be talking about loops.
44:28 So what exactly are loops?
44:29 Next up, we're going to be talking about loops.
44:32 So what exactly are loops?
44:33 Next up, we're going to be talking about loops.
44:35 So what exactly are loops?
44:36 Next up, we're going to be talking about loops.
44:39 So what exactly are loops?
44:40 Next up, we're going to be talking about loops.
44:43 So what exactly are loops?
44:45 Well, as you can probably tell by that statement right there.
44:48 A programming loop is a statement
44:49 that is used to run certain instructions repeatedly.
44:51 Just like how the opening statement of this topic was repeated five times.
44:54 loops are very useful for a variety of reasons.
44:57 For instance, imagine you want to print something 15 Sure,
45:00 you could just copy and paste the print statement 15 times,
45:03 but this is really annoying to have to do,
45:05 it becomes even more unrealistic when that number goes up to say 100 or so.
45:08 Now what if instead of rewriting the same instructions over and over again,
45:12 you would simply place the print statement inside of a loop,
45:14 and it will occur as many times as you would like.
45:16 Now that's the power of loops.
45:18 With loops were able to repeat parts of code multiple times.
45:21 Now, there are three different types of loops that we will be discussing today.
45:24 And first is the for a for loop is
45:27 very useful for situations like the one described above,
45:30 where you would like to carry out a certain set of instructions numerous times.
45:33 The syntax for a for loop varies depending on the language,
45:36 however, it usually consists of three parts an integer value,
45:39 a condition which the integer value must meet in order to exit the loop,
45:43 and an operation to modify the integer value
45:45 at the instructions inside the loop are completed.
45:47 Each time the for loop runs,
45:48 the operation you set will be performed on the integer
45:51 as long as that integer still needs to condition your set,
45:54 usually being greater than or less than a constant value,
45:56 the for loop will continue to run.
45:58 Eventually, when the integer has been
46:00 modified by either increasing or decreasing
46:02 it to the point where it no longer meets the condition,
46:05 the for loop will terminate and the code will continue to run.
46:07 For example, let's say our integer value was I and we set it equal to zero,
46:11 then we set the conditional statement as I being less than three.
46:14 So basically, we're saying that as long as I
46:16 the variable we just created is less than three,
46:19 continue running the instructions contained within the loop.
46:21 Finally, we make the operation i plus plus, meaning each time the loop runs,
46:24 we increase it by one, and inside of the loop,
46:27 let's just put a simple print statement.
46:29 Now let's run through the for loop,
46:31 we start with i equals 00 is less than three,
46:34 so we enter the loop and print out hello world.
46:36 Now that the instructions are done, we add one two, I'm making it one moving on.
46:41 One is again less than three.
46:42 So we want to get into the loop and print out hello world.
46:46 Again, we had one two, I'm making a two now to is still less than three,
46:50 so enter the loop again and print out hello world.
46:52 Finally, we add one to it once again, and it becomes three.
46:55 Three is not less than three though, is equal to three,
46:58 and so we don't enter the loop and it terminates.
47:00 Moving on to the next segment of code.
47:02 This is a simple example.
47:03 But you can extrapolate it across programming to fit your needs.
47:06 Now when using a for loop, we have to make sure to set up a condition
47:09 that given the initial integer value and the operation
47:11 will at some point not be met to avoid
47:14 creating an infinite loop and crashing your program.
47:16 an infinite loop occurs when you give a for loop a condition
47:18 which will always be met given the parameters of the program.
47:20 And so software crashes.
47:22 For example, a for the beginning at 10.
47:24 And checking if is over less than zero,
47:26 and then adding one to either the end of the loop
47:29 will never terminate since I will suggest increase infinitely.
47:31 After the for loop is the very similar for each loop.
47:35 A for each loop, or a for in a list
47:39 loop in Python is used for iterating through arrays or lists.
47:43 Essentially, the loop will go through each element in the array
47:46 and carry out some server instructions for each value.
47:48 If you would like to read all of the elements
47:51 in an array and compare them to some value,
47:53 or perform some operation on them, a for each loop is extremely useful.
47:58 So for example, we could have a for each loop which iterated
48:02 across an array and simply printed out the value of each array location.
48:06 Next up we have the while loop.
48:08 A while loop will continually carry out instructions
48:11 while a conditional statement given to it is true.
48:12 This can be as long as a certain variable is true.
48:15 As long as the numbers that's another number,
48:17 or while a value is still equal to another value.
48:19 For example, while loops are different than for loops in that the loop
48:22 is not contained within one statement
48:23 but stretched out and will continue to run.
48:25 As long as this condition is true.
48:27 Like a for loop, you could make the condition such
48:29 that it will eventually return false and exit the loop.
48:32 However, while loops will not crash
48:33 your computer should you create an infinite loop.
48:35 In fact, it is very common for wild lips to run infinitely.
48:39 As for certain programs,
48:40 you would like the program to continually be iterated through instead of running
48:44 once all the way through until you exit out of the program.
48:47 When programming a game for instance,
48:48 a while loop would be used to iterate through your code,
48:51 continually refreshing the screen as the game runs.
48:53 From there you can perform operations on the screen to make the game playable.
48:57 Creating infinite while loop could be done by simply using the syntax,
49:00 while true, as the condition true will always be evaluated as true.
49:03 Finally, I'd quickly like to cover the extension
49:05 of a while loop the do while loop.
49:07 Do while loops are very similar to while loops,
49:10 except they will carry out their instructions
49:12 at least once even if the condition is false.
49:14 And then we'll carry on like a basic while loop.
49:16 Essentially, the conditions inside of the loop will run at least once.
49:19 And then if the condition is still met,
49:21 they will run again and function as a normal while loop would.
49:25 As you can see loops in there
49:27 many varieties has some extremely useful functions.
49:29 using them, you're able to perform an operation many times in a row.
49:33 You can iterate through arrays and lists
49:34 and overall decrease the clutter of your code.
49:37 Next up, we're going to be taking a break from learning about common
49:41 programming statements and dive into what happens
49:43 when the code we write doesn't work.
49:45 More specifically, we'll be covering the different types of errors
49:48 that can occur when you're programming and what causes them.
49:50 Now when you're writing code, you have to understand that things aren't always
49:54 going to go the way you expected them to.
49:57 And sometimes the program doesn't always work as you had intended.
50:00 too.
50:00 We programmers call these errors.
50:02 And while annoying, they're always going to come up in computer science,
50:05 and so it's best to learn what they are and how to deal with them.
50:09 Often referred to as bugs,
50:10 errors and scripting languages can be narrowed down to one of three types,
50:15 syntax errors, runtime errors, and logic errors,
50:17 all three of which we'll be covering in today's video.
50:21 To kick things off, let's talk about syntax errors.
50:24 These are usually the easiest of the three to solve,
50:27 since they are oftentimes something that can be fixed within seconds.
50:30 If you remember back to earlier in the video,
50:33 when we talked about syntax and programming rules,
50:35 we said that if you were to break the programming
50:38 rules or syntax that would result in an error.
50:40 Well, that's what syntax errors are parts in your program
50:43 where you fail to meet the programming rules,
50:45 and so the computer doesn't know how to interpret your code.
50:48 This can be anything from forgetting a semicolon
50:50 at the end of a statement in Java,
50:52 accidentally defining a variable with two words instead of one,
50:55 or even just misspelling the word string
50:57 when you're trying to define a string variable.
50:59 Lucky for you guys, these errors are extremely easy to fix,
51:02 since you just need to figure out where the error occurred,
51:06 and what the syntax rule you broke was.
51:08 Now thinking back to IDs,
51:10 we mentioned that IDs are so useful because they do precisely that.
51:14 They underline the syntax errors and usually provide
51:16 helpful hints as to how to fix them.
51:18 Think of syntax errors as small misspellings
51:21 or grammatical errors in an essay you're writing annoying,
51:24 yes, but not the most infuriating things.
51:26 Another useful thing about it is when it
51:29 comes to syntax errors is that the program
51:31 will actually restrict you from running
51:33 the code unless all syntax errors are cleared,
51:35 making them even easier to identify and fix.
51:38 The second type of error we will be covering is the runtime error.
51:43 These errors don't show until you actually run the code,
51:46 hence the name runtime error.
51:48 runtime errors are usually caused by a statement
51:51 in your code that seems logically sound, but the computer physically has no way
51:55 of computing it in a reasonable amount of time.
51:57 The most common of these errors is one
52:00 which we've already talked about the infinite loop,
52:03 as a refresher or an example.
52:04 Think of an infinite loop like this.
52:06 Say you set your friend down in front of the TV, put on the office,
52:10 and told him he could leave as soon as Michael made it.
52:13 That's what she said joke.
52:14 Seems pretty simple, right?
52:16 Wrong, because instead of putting in the office, you put in friends on blu ray.
52:21 Now, Michael, no inappropriate joke,
52:23 meaning your friend would be sitting there for the rest of his life.
52:27 This is basically what happens with a computer,
52:29 you give it some condition that it
52:31 has to complete before the program can terminate.
52:33 However, you give it no feasible way to finish that task.
52:36 This puts the computer in error mode,
52:39 and most likely it will crash your program.
52:41 As the computer desperately tries to complete the condition you gave it.
52:44 As a computer example, if we try to have a program terminate
52:48 when integer i is no longer greater than 99.
52:52 But eyes initially 100 and only ever increases,
52:55 the loop will never terminate, and the program will crash.
52:59 To avoid these, you generally want to think
53:01 through the flow of your code before running it,
53:03 especially with loops to make sure that all
53:06 of your statements can be completed by the computer.
53:08 Carefully planning out your code before you
53:11 begin writing is an extremely useful practice,
53:13 and something we'll be covering towards the later part of this video.
53:17 The final type of error that we'll be covering is a logic error.
53:21 This error is also pretty self explanatory.
53:22 A logic error occurs when the code
53:25 runs smoothly without any runtime or syntax errors.
53:28 But the result that you get just isn't what you want it.
53:31 For example, let's say you had a calculator app,
53:33 and you want it to instruct a program to add two numbers,
53:36 except it multiplied them because you use the multiplication symbol on accident.
53:41 This leads to the sum being 36 instead of 13.
53:45 nothing went wrong with the code syntax or runtime wise,
53:48 the code runs just fine.
53:50 It just doesn't work as you had intended it to.
53:52 These are often the hardest types of errors to debug.
53:55 Since most of the time, you'll have no idea why the code isn't working,
53:59 and certainly not any idea of how to fix it.
54:01 This is why it's a good idea to test your code incrementally.
54:05 Don't wait until you've been programming
54:07 for an hour before testing your application.
54:08 We're all to run into a lot of logic errors.
54:11 Logic errors can be extremely fury ating,
54:13 and could cost you a lot of time making them a huge pain.
54:17 But if you know how to effectively debug your code, you'll be just fine.
54:22 Speaking of debugging your code that brings us straight into our next topic,
54:26 which is how to debug your program.
54:27 Now let's say you've written a program,
54:29 you think it's ready and you're ready to test.
54:32 You've been working hard on this and you're just wanting to see it in action.
54:35 You run the program and wait for it to run smoothly and efficiently.
54:39 Only it doesn't work.
54:40 you've encountered one of the three errors we've just mentioned.
54:43 You really want this code to work
54:44 but how this is where debugging comes into play.
54:46 If the code is giving you an error,
54:48 the first thing you should do is read the error.
54:51 Oftentimes for syntax and runtime errors,
54:53 the ID will print an error message out to the console.
54:55 See what line or lines it points to since those little lines with the occurred,
55:00 and see if you can understand and fix what the problem is.
55:04 If the error isn't clear, or you've never heard of it,
55:07 then try googling it as there are many websites out there.
55:09 So just Stack Overflow,
55:10 which service forums to ask an answer problems with code.
55:13 Chances are, if you've had a problem,
55:16 someone else has had the same issue and there's likely a tested solution.
55:20 Usually, when the syntax or runtime error pops up,
55:22 you should be able to find a fix for it fairly easily.
55:26 However, as I said before, the issue may arise from some loophole or oversight
55:30 in the code you hadn't planned for beforehand.
55:32 Maybe you did something as simple
55:33 as multiply two variables instead of adding them.
55:36 These are logic errors we talked about previously,
55:38 these problems usually won't have red text show up to explain what went wrong,
55:43 you'll have to figure it out yourself.
55:45 Now, there are a few different strategies that you can
55:47 use in order to track down and fix a logic error.
55:50 First, you could use print statements and the console
55:52 in order to determine where the code is going wrong.
55:55 Imagine you have a conditional statement that will run one
55:58 segment of code if an integer x is greater than five,
56:01 and it will run another segment of code if not, if in your program.
56:05 X is supposed to be greater than five when the program reached this conditional,
56:09 but for some reason, the program is still printing out x is small,
56:12 you can use a wrench they're meant to help.
56:14 For this problem.
56:15 Specifically, you could place the print statement before the branch
56:17 or the conditional that would print the value of x.
56:20 Now when you run the program, you know exactly what the computer is thinking,
56:24 printing out the value of x just before the FL statement.
56:27 While you know the variable has the value you wanted to have.
56:30 And if it doesn't, you know that somewhere
56:32 above that conditional is something went wrong,
56:34 and x was set to a value you didn't want it to.
56:37 In this case, x is equal to two, which is why x is small is being printed out.
56:41 Now that we know what the problem is,
56:43 we can track down where and when in the code, we modify x in order to solve it.
56:48 Use print statements to determine where your program goes wrong,
56:50 and then try to track down the cause of these issues and solve them.
56:54 If you use this strategy, make sure you end up deleting the print
56:57 statements afterwards to avoid clutter in the console.
57:00 The situation described above could also be solved using a breakpoint.
57:04 a breakpoint pauses your program when the line
57:06 you placed a breakpoint at is reached.
57:08 If say you would like to program to run through
57:11 a certain conditional and set a variable based on that conditional.
57:14 But you are unsure if this actually happens in your code,
57:16 you can place a breakpoint inside the conditional path that you expect to run.
57:21 Upon the breakpoint being reached,
57:22 the program will pause and wait for you to continue it through a button press.
57:25 This signals that this button occurred where the breakpoint was placed.
57:29 In this case, the correct conditional path has been reached by your program.
57:33 You can then continue the program knowing that this was
57:36 or wasn't where the error in your code occurred.
57:38 breakpoints can be used in conjunction with print statements.
57:41 In order to do both pause the program
57:43 and perhaps view the values of your variables
57:45 at the moment in time to give yourself all of the information you could want.
57:48 You can also have multiple breakpoints to help slowly work
57:51 your way through your program and determine where an error has occurred.
57:54 A combination of these two strategies will help you easily
57:58 determine where in your code you have incurred a logic error.
58:01 Next, let's go over what to do if you think you
58:04 have tracked down the section of code that causes the problem.
58:07 You may think you should delete it,
58:09 but it's likely you put it there for a reason.
58:11 And you don't want to lose all that work if you don't have to.
58:14 Firstly, try commenting it out.
58:15 Comments are used to markup code and explain their surrounding sections.
58:19 They can also be used to the bug.
58:21 Anything that is designated as a comment will not be
58:24 read by the program as code and will be skipped over.
58:27 Essentially, it becomes something that is only there for you,
58:30 the programmer to read.
58:32 The syntax varies from language to language,
58:34 but it usually involves placing some symbols before
58:36 or around the code you would like to be commented,
58:38 examples of how to comment in different languages can be seen on the screen now.
58:43 Also, when you comment something, the ID will grayscale that line of code,
58:47 making it extremely easy to determine what's commented and what's not
58:51 commenting code deletes it in the computer's eyes without actually deleting it.
58:55 If a problem is present before you comment a section of code,
58:58 but it's gone afterwards, then that section of code is the culprit.
59:01 If you comment part of the code out and there are still
59:04 issues and move on to another section until you find the culprit.
59:07 Once you do, you can tweak it until it works as intended or deleted entirely.
59:11 And you'll have a fully functioning program once again.
59:14 Now that we've talked about what to do if you've encountered an error,
59:18 and a strategy on how to find and fix it,
59:20 I'd like to talk about some strategies you
59:22 can use to avoid errors in the first place.
59:24 Firstly, backup your code frequently.
59:26 In the event of the code completely bugging out and you being unable to fix it,
59:30 you will want the ability to revert
59:32 to a previous version where the code was still working.
59:35 If you save frequently enough, you will probably not lose too much work.
59:39 Version managers like GitHub or subversion can help
59:41 with this as they backup code to an online cloud service,
59:44 which you can easily pull previous versions of the program from at any point.
59:48 Also on top of saving, running your program frequently to ensure
59:52 that the current version works as intended.
59:54 This accomplishes two things.
59:55 First of all, it prevents you from saving a backup
59:58 that doesn't work second If you encounter a problem,
1:00:01 it will be easier to find if you have only made a small
1:00:03 number of changes since the last time you ran it, and it worked.
1:00:06 And thus, you will only have to look through the new code for problems.
1:00:09 If you've spent five hours coding and hadn't run it during that time period,
1:00:12 it's going to be likely that at some point,
1:00:14 during that five hour code session, you messed up,
1:00:16 and it's going to be even harder to figure out where you went wrong.
1:00:20 errors, while annoying and extremely frustrating,
1:00:22 are a fundamental part of making you a better programmer.
1:00:26 Alright, now that we've covered errors for a bit,
1:00:28 let's hop back onto the programming statement train and talk about
1:00:31 one of the most important concepts in computer science, they'll function.
1:00:35 Now, you may not know it, but we've actually been talking about a few functions,
1:00:39 this entire series, print statements for loops.
1:00:42 And even the basic math operations we've
1:00:44 talked about are all examples of functions,
1:00:46 which of course, begs the question of what actually defines a function?
1:00:50 Well, a function is a segment of code
1:00:53 that can be easily run by calling the function name.
1:00:56 And depending on the type of function will do something in return.
1:00:59 functions can be called numerous times and in numerous places in your code.
1:01:03 Essentially, they're like wrapping up a segment of code into a nice present,
1:01:07 and giving it a name, which, when called, will unwrap the present and go
1:01:12 through the code that you had wrapped up.
1:01:14 For example, the print statements we've been using this series
1:01:18 allow us to print something to the console anytime we want.
1:01:21 Those are functions, you see, we just call the print function and enter in what
1:01:26 we want to be printed to the console into the parentheses,
1:01:29 and the computer does it for us.
1:01:31 Behind the scenes, there's actually even more complex code that is in charge
1:01:35 of taking your text and translating it to the console to be printed.
1:01:39 The developers of almost all programming languages
1:01:41 realize that you don't want to program something
1:01:44 that manually has to print something
1:01:46 to the console through the use of complex programming.
1:01:49 And so they implemented the print statement to reduce
1:01:51 the stress and complexity of code on the user,
1:01:54 abstracting it to the single line of code that is print.
1:01:57 All of that code that is used to print something to the console is
1:02:01 wrapped up like a present and given to us in the form of one line.
1:02:05 This is actually the main theme of all
1:02:08 functions and the backbone of any good program.
1:02:10 Oftentimes, in your program, there are going to be sections of code
1:02:14 which are repeated and serve the same purpose,
1:02:16 or equations, which you want to allow different inputs of.
1:02:19 And so you can use functions in order to condense these down into singular
1:02:23 lines of code to save both time and reduce clutter on your code.
1:02:26 As you will see soon functions are extremely powerful,
1:02:30 and will definitely be something you utilize
1:02:33 all the time in your computer science journey.
1:02:35 The print statement is just one example of functions in everyday code.
1:02:39 There are 1000s of functions that are available to you through the IDs.
1:02:43 However, because we won't always use all the functions
1:02:46 that are available to you in a single program,
1:02:48 you have to import these functions from packages found in the ID E,
1:02:52 which is something that we'll be covering later on.
1:02:55 Now, there are four main types of functions in most programming languages.
1:02:59 And they are separated by two defining features,
1:03:02 whether or not they take in arguments and whether or not they return values.
1:03:06 Let's start by separating them by whether or not they take arguments.
1:03:09 But first we have to cover what arguments actually are.
1:03:12 arguments are essentially variables that we pass
1:03:15 into the function in order to be manipulated,
1:03:18 and then either returned back to us printed
1:03:20 to the console or used in another operation.
1:03:23 Think of functions with arguments like this.
1:03:25 If you walked up to your local Five Guys,
1:03:28 and told them that you wanted to get food without supplying a type of food,
1:03:31 they would probably look at you confused.
1:03:33 In order to get the food that you want,
1:03:35 you need to tell them exactly what you want
1:03:37 to order so that they can give it to you.
1:03:40 In this case, getting food is the function and what
1:03:43 you order in terms of food is being passed
1:03:46 in as the argument based on what you tell them
1:03:49 or the argument that you pass into the get food function,
1:03:52 they will do something different.
1:03:54 You should also note that the argument can be many different things.
1:03:57 It could be fries, burgers, sodas, really anything on the menu,
1:04:01 and such as the case with arguments in programming.
1:04:04 Arguments can be strings, integers, arrays, pretty much anything.
1:04:07 As an example of a function that takes in arguments,
1:04:12 let's look at the max function, which takes in two integers as arguments
1:04:16 and returns the maximum number between the two.
1:04:19 Now for this function,
1:04:20 if you don't input two numbers or variables for it to compare,
1:04:24 it's going to throw you an error.
1:04:26 Just like the five guys employee.
1:04:28 He doesn't know what you want to eat
1:04:30 Since you didn't provide him with any arguments.
1:04:32 And the computer doesn't know which two numbers you want it
1:04:35 to compare and return Since you didn't provide it with two integers.
1:04:39 arguments are a way for programmers to have one function that can
1:04:42 do many different things depending on whichever
1:04:44 variables can be passed through arguments,
1:04:46 add variability to programming and can help diversify your code.
1:04:50 Think of it like this, a restaurant that only allows one
1:04:53 type of food to be made regardless of what you order
1:04:56 isn't going to be very useful or diversified but We're able
1:05:00 to pass in arguments and tell them what food we want,
1:05:03 our experience can be heightened and more options can become available to us,
1:05:07 which is exactly what happens when you start using arguments in your function.
1:05:10 Now that we've talked about functions that take in arguments,
1:05:14 let's move on to functions which do not,
1:05:17 because functions can also be created and used without taking
1:05:20 arguments in and still be incredibly useful to the programmer.
1:05:23 For example, let's say you're making a text based RPG game,
1:05:26 and one of the options you give your player is
1:05:29 the ability to view their stats at various points throughout the game.
1:05:32 Now every time you come upon this option, and they choose the view stats button,
1:05:36 you don't have to type out six different
1:05:39 print statements for every statistic they may have,
1:05:42 your code would get cluttered and messy very quickly.
1:05:44 Instead, what you could do is you could package the six
1:05:48 different print statements in a simple function called print stats,
1:05:50 you don't need to pass in any arguments into the function,
1:05:54 since the function will do the same thing
1:05:56 no matter what the statistics on the player are.
1:05:59 Now, every time the user wants to view their stats,
1:06:02 you could simply call the print stats function,
1:06:05 and voila, the user stats are printed for them to view.
1:06:08 This allows you to save a lot of time writing code, but also a lot of space,
1:06:12 which is extremely important when your programs begin
1:06:15 getting into hundreds and 1000s of lines of code,
1:06:17 and you want to easily search through it to maybe debug.
1:06:22 Okay, now that we've separated functions
1:06:24 into those that take arguments and those that do not,
1:06:27 let's again, split these up into those that return values and those that do not.
1:06:31 Another thing you have to understand is
1:06:33 that when you're making your own functions,
1:06:35 which is something that we'll be covering very soon,
1:06:37 you have to choose what your function will return,
1:06:40 if anything at all, functions are able to return values back to the user,
1:06:45 whether they be in the form of string variables,
1:06:48 integer variables, or even arrays.
1:06:50 Now, the thing to note about returning variables
1:06:52 is that calling the function alone won't do anything,
1:06:56 you have to return the value into something or print it out.
1:07:00 As an example, the max function we talked about
1:07:03 previously would return an integer back to the user.
1:07:06 But in order to do something with it, we would have to either set a new integer
1:07:10 variable equal to the result of that max function.
1:07:13 Or we can print out the result of the function, which in this case,
1:07:17 which is print out the maximum value between the two integers.
1:07:20 Using functions which returned values don't do much on their own,
1:07:23 you have to pair it with something in order to gain the use from it.
1:07:28 Let's do another example.
1:07:29 Let's say you had created a function which took in two string variables
1:07:33 as arguments and combine that using
1:07:35 that fancy string that we talked about earlier,
1:07:37 and then return them as a singular string.
1:07:40 This combined string function could then be used to create new string variables.
1:07:45 Since what it returns is basically a string,
1:07:48 the variable would simply be set to whatever
1:07:51 is returned from this combined string function.
1:07:53 The last type of function is one that does not return anything.
1:07:57 And these are known as void functions.
1:08:00 Oftentimes, these are like the print stack functions that we created earlier.
1:08:04 Simply use to condense large amounts of print
1:08:07 statements that appear often in your code.
1:08:09 These cannot be set to variables since they don't return anything,
1:08:12 and just get the code that's within them run through.
1:08:16 So there you have it, the four types of functions,
1:08:19 ones that taking arguments and return something,
1:08:22 ones that take in arguments and don't return something.
1:08:25 ones that don't take in arguments, but still return values,
1:08:28 and ones that take in arguments and don't return anything.
1:08:31 Each of these four types of functions are useful and unique in their own way.
1:08:36 And you will probably find yourself using
1:08:38 each of them through your programming journey.
1:08:40 So get used to the different types of functions
1:08:42 and know how to make the most of them, as they are all extremely powerful.
1:08:45 Finally, I'd like to talk about one of the major benefits of functions,
1:08:49 which is that it makes it super useful to make large
1:08:52 changes to your code without having to go through the entire program.
1:08:55 Each function call is essentially just a copy of that functions original code.
1:09:01 And so it's very easy to make changes to that initial function,
1:09:04 and have it translate across your entire code.
1:09:06 Let's go back to our print stats function and say that you wanted to go back
1:09:10 and add in a new statistic that the player
1:09:13 could level up and through experience in the game.
1:09:16 Without functions, you'd have to go back
1:09:18 into your code and find every instance that you'd
1:09:20 printed out the user stats and create
1:09:23 another print statement to display the new statistic.
1:09:26 However, if you had created a print stats function like we did just a while ago,
1:09:30 all you would need to do is find out where
1:09:33 you define that function and add in a print statement,
1:09:36 which displays the new statistic and you're done.
1:09:38 Now every place which you had previously called the print
1:09:42 stats function will now also print the new statistic as well.
1:09:45 You can see just how powerful functions can be if used properly,
1:09:50 and we haven't even scratched the surface yet.
1:09:52 Now, up next, we're going to cover how we can import
1:09:56 other people's functions that they've written and use them in our code.
1:10:00 Before we get technical,
1:10:01 close your eyes and imagine you're trying to build a house.
1:10:04 Sure, you could grow your own trees, chop down your own wood,
1:10:07 make your own tools and nails and build from scratch.
1:10:10 But why do that when you can simply go to your local Home
1:10:13 Depot and buy these materials that others have already made ready for you.
1:10:17 That's the main idea behind importing functions into your code.
1:10:20 Importing functions allows you to gain access to libraries
1:10:23 of functions that other people have already made for you.
1:10:26 This is just as useful as it sounds.
1:10:28 There are so many functions that are super useful for any given program,
1:10:32 that it will take you forever to write them all yourself.
1:10:34 Luckily, other people have already done most of this for you.
1:10:38 In each programming language,
1:10:40 you were able to use an import statement to import libraries
1:10:42 of functions into your program that you can use as you write it.
1:10:46 A library is simply a collection of functions that all have the same theme,
1:10:50 and maybe a math library,
1:10:52 a data analysis library, a library that was translated text,
1:10:55 or anything you can think of really, there's such a variety of libraries for any
1:11:00 given language that most functions you require
1:11:02 that are not hyper specific to your program can likely be found at some library.
1:11:06 In fact, a good portion of any programmers job is looking online for packages,
1:11:11 which can make his or her job easier, instead of hand writing functions.
1:11:14 Now I can hear you saying, Wow, that's sick.
1:11:17 How do I do it?
1:11:19 Well, it's quite simple, an import statement.
1:11:22 In most languages, an import statement consists of three parts.
1:11:26 The library you would like to import from the package
1:11:30 you would like to import from that library,
1:11:32 and then which class from that package you would like to use.
1:11:35 For example, we can load up the Java library.
1:11:37 And from there import the util package, short for utilities.
1:11:41 And then from that utilities package, import the scanner class,
1:11:45 a class which allows us to read information from the user.
1:11:48 A package is simply a smaller set of functions and methods
1:11:51 to help differentiate between the 1000s of methods contained in a library,
1:11:55 and a class is even more specialized than that.
1:11:58 Now, if you don't know what specific classes you're going to want to pull
1:12:02 methods from, you can use a start
1:12:05 import all classes within the package you'd like.
1:12:07 However, it can be beneficial to be more specific,
1:12:10 and only import the classes you would like,
1:12:12 as it helps with the efficiency of the program in the long run.
1:12:16 For instance, in Python,
1:12:18 the syntax to import a library is import followed by the library name.
1:12:22 However, importing an entire library is more computationally
1:12:26 taxing than importing specific functions from a library.
1:12:30 Imagine you would only be using
1:12:32 the factorial function from the Python math library,
1:12:34 it would be a waste of computing power to import the entire library,
1:12:37 and it would increase the load time for your program.
1:12:40 For smaller programs, this isn't a big deal.
1:12:42 But it really starts to add up when dealing with larger projects.
1:12:46 Therefore, you would instead use from math import factorial,
1:12:50 or the Java equivalent import java dot math dot factorial,
1:12:54 and now have access to that one math command only.
1:12:57 This limits the functions that you can use.
1:13:00 However, it saves programming runtime,
1:13:01 if you decide you want to use another function that you
1:13:04 hadn't planned for, you can always go back and import that too.
1:13:07 Many times.
1:13:07 If you try to use a function from a common package,
1:13:10 and you have not yet imported, the ID, he will prompt you to do so if you're
1:13:15 trying to figure out which libraries you want to import,
1:13:18 think of the functions you're going to need in your program,
1:13:21 perform a simple Google search.
1:13:22 And you will probably run into a package or library
1:13:25 that already exists in your ID that you can use.
1:13:28 And if you can't, there are ways to download
1:13:31 and import additional projects to fit your needs.
1:13:33 But what if, after all that you still can't
1:13:36 find a library that contains the function you're looking for?
1:13:39 Well, that's a perfect segue into what we're going to talk about next,
1:13:42 which is the basic structure for writing your own functions.
1:13:46 So at this point, we've talked about both what functions are and how
1:13:50 we can get some very useful functions by importing them through packages.
1:13:53 But there are definitely going to be moments
1:13:55 in your programming career where you're going to want
1:13:57 to make your own functions because you want one
1:14:00 to be made specifically to your program in code.
1:14:03 Luckily, making your own functions is extremely simple.
1:14:05 There are just some basic rules that I want to cover.
1:14:08 Now we've previously used making functions as an example.
1:14:12 For other topics such as the Player Stats function from a little bit ago,
1:14:16 they were extremely abstract and didn't go into depth
1:14:19 into what is needed for an actual function to operate.
1:14:21 So right now, we're going to be covering a skeletal system of everything
1:14:25 that needs to be included in a function in order to get it to work.
1:14:28 Now think back to the four different
1:14:30 types of functions that we talked about previously,
1:14:32 functions that do and don't return values and functions
1:14:35 that both do and don't take in arguments.
1:14:38 For creating your own functions.
1:14:39 We're just going to have to go down the list
1:14:42 and talk about how to approach creating each one of them.
1:14:44 Starting with the most basic of the bunch,
1:14:46 one which takes in no arguments and returns no values.
1:14:50 Before we start that there are a few small
1:14:54 things I want to note about function naming conventions,
1:14:57 the variable naming conventions we talked about previously.
1:14:59 also translate over to function names.
1:15:01 So you can't have two word functions,
1:15:04 and you can't use special characters like periods or commas.
1:15:07 Generally, you're going to want to follow the same camel case style,
1:15:11 which we talked about in the variables video,
1:15:13 which is not capitalizing the first word, but capitalizing each word after that.
1:15:19 Alright, so in general, for making functions,
1:15:21 each language differentiates on how you tell the computer,
1:15:23 that it's actually a function.
1:15:25 In Java, you have to define the function scope,
1:15:28 which is something you don't really need to know
1:15:31 unless you're going to become more invested in Java.
1:15:33 But basically, it tells the computer which parts
1:15:36 of the code can use the function in which type can,
1:15:39 for this series, all of our functions that we make are going to be public.
1:15:43 From there, you then determine which type of function it is.
1:15:46 So in this case, since it won't be returning any variables,
1:15:49 we'll just put void to signify this type
1:15:51 of function will not be returning anything.
1:15:54 Finally, you put the function name after those two identifiers,
1:15:57 along with a set of parentheses after it like so,
1:16:00 the parentheses are where your arguments would go,
1:16:02 if you were making a function that took in arguments.
1:16:05 But since for our first type of function,
1:16:07 we're not incorporating arguments into this function,
1:16:09 let's just leave those blank.
1:16:11 All of that is just for Java, Python, on the other hand,
1:16:16 all you have to do is put the word def,
1:16:18 short for define, and then the function name with a set of parentheses.
1:16:21 So as you can see, each language is going
1:16:24 to be a little bit different in how you make functions.
1:16:26 But the main thing we want to remember is to always add parentheses.
1:16:31 From there, we just have what we want our void function to do
1:16:34 within the confines of the function and close it off, and we're done.
1:16:38 In Java, the confines of the loop would
1:16:40 be whatever is contained within the curly braces.
1:16:43 And in Python, it would be until you are no longer indented.
1:16:46 At its core, this is the most primitive type of function, we've made.
1:16:50 Something which takes in no arguments and returns no values,
1:16:53 quite similar to the print stats function from early on.
1:16:56 Moving on to the next type of function,
1:16:59 creating a void function that takes in arguments.
1:17:02 Now this process is going to seem very
1:17:04 similar to the previous except for one small adjustment.
1:17:07 Remember the parentheses that I mentioned earlier?
1:17:10 Well, we put any variables,
1:17:11 we want the user to pass into the function into these parentheses.
1:17:15 And then when we call that specific function,
1:17:18 it will be required to have those variables passed into it.
1:17:21 For example, in Java, let's make a function that takes in two
1:17:25 numbers and prints out the product of those numbers.
1:17:28 We start with the public void plus name of function setup again,
1:17:31 since we won't be returning any values, and here comes a new part.
1:17:36 Inside the parentheses,
1:17:37 you define which type of variables you would like to pass in as arguments,
1:17:41 in this case, an integer and then a name for that variable.
1:17:44 This name is what you will use to refer to the integer that the user passes in.
1:17:49 For example, let's just call it num one.
1:17:52 Then if we want to add another argument,
1:17:54 we simply add a comma in between the two and we
1:17:57 can make another integer variable num two to hold the second number.
1:18:01 We can do this for however many variables we want to pass into the function.
1:18:05 But for now, let's close off the parentheses and just print out
1:18:08 the product of num one and number two, as you can see,
1:18:11 we refer to the two numbers that the user will
1:18:14 input into the function has num one and m two,
1:18:17 whatever numbers that the user passes into the function
1:18:20 will be converted into num one and m two.
1:18:24 Now, whenever we want to call the multiply numbers function,
1:18:27 we just have to make sure that we are putting two numbers in as arguments.
1:18:31 In this case, the number five becomes num
1:18:33 one and the number eight becomes num two.
1:18:36 From there, we simply run the code and the number 40 is printed to the console.
1:18:40 It's pretty important to note that you can
1:18:42 also mix and match variables when making arguments.
1:18:45 So you can have some function which takes in a car,
1:18:48 an integer, and two strings all within one function.
1:18:51 The last thing I want to mention about
1:18:54 arguments is that when you call a function,
1:18:56 you have to follow the variables you defined when making the function.
1:19:00 So for our multiply numbers function,
1:19:01 you couldn't put in a string and then an int, it has to be two integers,
1:19:06 because that's what the computer is expecting to be passed into the function.
1:19:10 So now that we've gone over how to make functions that don't return variables,
1:19:14 we have to cover those that do.
1:19:15 And we'll start with ones that don't take in arguments.
1:19:18 Now the main difference between defining functions that return variables
1:19:21 and defining ones that do not is that in some cases,
1:19:25 you have to specify that you want this function to return an integer variable.
1:19:29 This is most common in Java,
1:19:31 where you would replace void with int to tell the computer that you want
1:19:35 this function to give you something back to you in the form of an integer.
1:19:39 This works the same as if you wanted
1:19:41 to return a string care or even in an array.
1:19:44 You simply replace the word after public with whatever
1:19:47 variable you want to be returned by that function.
1:19:50 The most important thing to remember about making functions that return
1:19:54 variables is that no matter what path your code takes,
1:19:57 it must return a variable no matter What?
1:20:00 What does this mean?
1:20:01 Well, let's say you had some string function in a game.
1:20:04 And inside of it, there was an if
1:20:06 statement where if the player score was above 10,
1:20:08 you returned a congratulatory message.
1:20:10 This works fine if you printed the result of this function,
1:20:13 and the player score is above 10.
1:20:15 But if the player score was less than 10, then you don't enter the if statement,
1:20:19 and then you don't have something prepared to be returned to the user.
1:20:22 And so the function is going to throw you an error,
1:20:25 you have to have all your paths covered, which may seem simple.
1:20:28 But if you're making a function with a switch
1:20:31 statement in it containing high amounts of cases,
1:20:33 then this can get out of hand very quickly.
1:20:35 Something I like to do to make sure this doesn't happen
1:20:38 is put a return statement at the bottom of a function
1:20:40 with a string or an integer so unique that I'm able
1:20:43 to tell that the code is not running properly and can fix it.
1:20:47 Also, usually, an ID will let you know if there's a path in your code
1:20:51 or in a function that does not return a variable when it should.
1:20:54 The main point I'm trying to get across however,
1:20:57 is always cover your exits and make sure you have a return
1:21:00 statement prepared for any case the user may throw at you.
1:21:02 Another small thing to note is that you can't return one type
1:21:06 of variable if you've already defined the function to return another type.
1:21:09 For example, you can't return a string and an integer function or vice versa.
1:21:14 The return statement must always match the type of function no matter what
1:21:18 the final type of function is one
1:21:20 that returns variables and also takes in arguments.
1:21:23 And for these, all you need to do
1:21:25 is combine what we've learned from the previous cases.
1:21:28 First, you assign your arguments in between the parentheses making sure
1:21:31 that you've also defined what type of variable you want to return,
1:21:34 and then ensure that no matter what path the code takes,
1:21:37 that you're always returning that variable type.
1:21:40 That concludes our discussion on functions.
1:21:43 As you can probably tell, functions are an extremely vast subject area,
1:21:47 and require a little bit of practice to fully understand,
1:21:49 which is why later on in this series will recommend
1:21:52 some websites you can use to practice those more difficult topics.
1:21:56 Now, I'd like to switch gears a little
1:21:59 bit and continue our discussion from earlier on arrays.
1:22:03 arrays, while useful aren't the only way to store and manipulate information.
1:22:08 In fact, there are a multitude of different
1:22:11 ways to store data in computer science,
1:22:13 including linked lists, stacks, queues, maps, trees,
1:22:17 and many others to right now though, I'd like to talk about two cool,
1:22:24 wacky and zany ways to store data that we haven't previously covered,
1:22:28 array lists and dictionaries.
1:22:29 But before we get into those, let's
1:22:31 get a little review and reinforcement of arrays.
1:22:34 As you may remember,
1:22:36 arrays are basically lists of values that are stored together.
1:22:39 When you initialize an array, you give it a size and this size is fixed,
1:22:44 you won't be able to increase the size of the array.
1:22:47 So when you make an array of length is final.
1:22:49 To access the values in an array,
1:22:51 you reference them using an index for starts at zero.
1:22:54 What this means is that the first item of an array is not at position one,
1:23:00 it is that position zero.
1:23:01 And its position is commonly referred to as its index location.
1:23:04 So to find the nth item in an array,
1:23:07 you would refer to it as index location n minus one.
1:23:10 However, as the size of an array is fixed,
1:23:13 you have to be careful to not reference
1:23:16 a position that's beyond the total size of the array,
1:23:18 or to append too many items to it.
1:23:20 As this will return an error.
1:23:22 We also have what are known as two dimensional arrays,
1:23:25 which is an array containing an array and each of its indexes.
1:23:28 Or you could have an array containing
1:23:30 arrays containing arrays containing arrays containing arrays,
1:23:33 depending on what you're trying to do.
1:23:35 multi dimensional arrays can be useful in more advanced
1:23:38 programs for organizing a wide volume of related values.
1:23:41 If that's confusing at all,
1:23:42 just get back to earlier for our full discussion on arrays,
1:23:46 the timestamps will be in the description.
1:23:48 Now that we've reviewed arrays, let's go over array lists.
1:23:51 Array lists are just lists in Python can be thought of as a growing array.
1:23:56 Earlier, we mentioned how you have to be
1:23:58 careful to set an appropriate size of your array,
1:24:01 and to make sure that you only referenced
1:24:02 in the pin values such that you remain within the size.
1:24:05 However, with array lists, this isn't a problem.
1:24:09 After you initialize an array list instinctively has a size of 10.
1:24:13 What if you append values,
1:24:15 such that the size of the array list goes beyond 10 elements,
1:24:18 an array list will grow itself,
1:24:20 meaning that the computer will allocate more memory to the array
1:24:24 to increase its total size so that the new values can be appended.
1:24:27 This is quite useful when you don't know the exact
1:24:30 number of values that the array will need to store.
1:24:32 Or you want the ability to store more values to your heart's content.
1:24:35 such as when you're making a database
1:24:37 with an unknown amount of users that will sign up.
1:24:40 There's a lot more to uncover when regarding array lists.
1:24:42 But for this surface level series, that is all you pretty much need to know.
1:24:46 So let's move on to dictionaries.
1:24:48 Now when we're talking about dictionaries,
1:24:50 we're not referencing that thick book you probably have lying around your house,
1:24:54 which has 1000s of definitions.
1:24:55 In computer science.
1:24:56 dictionaries are like arrays in that they store multiple values.
1:25:00 Use, however, their values are stored very differently.
1:25:02 Rather than being referenced by their linear position within the dictionary,
1:25:05 each value is tied to another value that is used to reference it, or its key.
1:25:10 Because of this, we need to throw away all conceptions
1:25:13 of dictionaries as a linear way of looking at data.
1:25:15 Since in actuality, it is much more fluid and entertaining.
1:25:19 Basically, we say that each position in a dictionary holds a key value pair.
1:25:24 When referencing a value in a dictionary, you will use its unique key,
1:25:28 and the dictionary will tell you the value
1:25:30 that it is tied to think of it like this.
1:25:33 Each time when item to your dictionary,
1:25:35 your computer creates the handcrafted box to store the data.
1:25:38 And also a custom makes a jewel key one of a kind, no other like in the world.
1:25:43 This way, there's only one key that goes
1:25:45 to the box that stores a certain bit of information.
1:25:48 Because each key must be unique, we're using the key in a dictionary,
1:25:52 it will result in an error being thrown.
1:25:54 Because having two keys that are exactly the same will confuse the computer
1:25:58 as to what box or piece of information that key leads to.
1:26:02 However, you can store the same value in multiple
1:26:04 key value pairs since the keys would all be different.
1:26:07 Now, like I said, dictionaries are more fluid,
1:26:10 making them easier to organize and then arrays
1:26:12 as everything is set up in a more logical manner.
1:26:15 That is to say, it is easier to find
1:26:17 the value you are looking for when you're using keys,
1:26:20 rather than simply referencing their positions.
1:26:22 Let me explain what I mean.
1:26:24 Imagine you have a dictionary of prices at a store
1:26:26 where the key is the name of the product,
1:26:28 and the value is the price of the item.
1:26:31 Maybe apples cost $1 milk costs $2 and bread costs $3.
1:26:35 You can see the in the dictionary, each key is the name of a product,
1:26:39 and each value corresponds to the price of each product.
1:26:42 So to find the price of bread,
1:26:43 all you need to do is simply call the dictionary using the key bread.
1:26:47 This makes it extremely easy to track values through your code.
1:26:51 Since you're working with tangible values rather
1:26:53 than numbers which don't mean anything to you.
1:26:55 You can also manipulate dictionaries and money the same
1:26:58 way as you can manipulate arrays and array lists.
1:27:00 You can iterate through a dictionary and perform
1:27:03 many operations and comparisons on the values.
1:27:05 If you want to find the product with the highest price for example,
1:27:08 you can iterate through the dictionary to find
1:27:10 the value that is highest amongst the grocery store items.
1:27:14 arrays, array lists and dictionaries are useful in their own right,
1:27:17 as are the mass amounts of other ways to store data,
1:27:20 and each boasts certain advantages over one another.
1:27:22 We already covered the basics of these three.
1:27:25 But since there are so many,
1:27:26 we don't have time to go in depth into each and every one of them.
1:27:29 And so in order to help you grasp the basics of storing information,
1:27:32 we're now going to talk about one
1:27:34 of the most important functions needed to understand arrays,
1:27:36 which are searching algorithms.
1:27:37 Now just as there are many ways to store information in computer science,
1:27:42 there are even more ways of searching through lists.
1:27:45 Searching algorithms at their core are ways in which we
1:27:48 can look through a list of values stored in an array,
1:27:50 say a patient name list or a high
1:27:52 score list and find a particular piece of data.
1:27:55 The goal of a searching algorithm is simply to give
1:27:58 the algorithm a string or object you want it
1:28:00 to find and have it return the index of the array
1:28:03 that contains that string or object as fast as possible.
1:28:06 Now while this may seem simple, lots of software runs on the backbone
1:28:10 of being able to search through lists extremely quickly,
1:28:12 making searching algorithms and in particular,
1:28:15 efficient searching algorithms an important topic to cover.
1:28:19 Additionally, this is the main functionality that arrays are used for.
1:28:23 And it's the backbone of many of the methods
1:28:25 used within ArrayList as well as many other storage methods.
1:28:28 So knowing them will take you a very long way.
1:28:32 Typically, searching algorithms are used to return the index
1:28:35 of a particular data points so that it can be used,
1:28:38 modified or updated or checked on.
1:28:40 For example, if you're about to check
1:28:42 into a hospital run on an array system for patients,
1:28:45 the staff must search through your name in the database.
1:28:47 And by returning the index of where your name is,
1:28:50 they now have a quantifiable number that they can use
1:28:53 to easily check you in, rent out prescriptions schedule you for checkups,
1:28:57 update your personal information, etc.
1:28:59 Without having to search through the list for your name every single time.
1:29:03 You may think that there's little difference between searching algorithms
1:29:06 since computers nowadays can perform millions of calculations per second.
1:29:10 But when you're a huge multi billion dollar corporation trying
1:29:13 to find a certain data point in a list containing 1000s,
1:29:16 or even millions of data points,
1:29:18 small differences in efficiency are going to make or break the user experience.
1:29:23 Even a 1% improvement in efficiency can mean a big differences
1:29:26 in the amount of time that a user is waiting for a simple task.
1:29:30 Now before we jump into different types of searching algorithms,
1:29:33 we must discern between the two states that a razor
1:29:36 list can be in either sorted or unsorted.
1:29:39 A sorted list of information is characterized by some sort of rankl value,
1:29:44 whether that be a patient ID, credit card number,
1:29:47 or even by alphabetical values like usernames or legal names.
1:29:51 An unsorted list is just some random assortment of related
1:29:54 information not sorted by any particular order or reason.
1:29:58 Some searching algorithms only work for sorted lists,
1:30:01 usually the more efficient ones,
1:30:03 and some work for both sorted and unsorted lists.
1:30:07 Although these are usually less efficient if
1:30:09 you end up pursuing computer science further,
1:30:11 you'll have to deal with both sorted and unsorted lists.
1:30:14 So it's good to know a common searching practice for both.
1:30:18 Another thing to note is that we
1:30:20 determine the efficiency of searching algorithms,
1:30:22 based on both the worst case scenario and the average number
1:30:25 of items that must search in order to find the index.
1:30:28 We call this big O notation in which each searching algorithm
1:30:31 has an equation which takes in the size of the array
1:30:34 as an integer and, and will output a worst case scenario
1:30:37 efficiency value that we can use to compare with other searching algorithms.
1:30:41 We can then also look at how long on average
1:30:44 it takes to find an element in a list.
1:30:47 Using these two methods allows us
1:30:49 to easily compare how efficient two algorithms are.
1:30:51 Alright, now that we've got some background on searching algorithms,
1:30:55 let's hop right into it.
1:30:56 The first type of search we'll be talking about is called a linear search.
1:31:00 And Eve honestly probably uses multiple times throughout your life.
1:31:03 Every time you have to search for your name on a list of people,
1:31:07 you probably follow the same pattern,
1:31:08 you start at the top check to see if the first name on the list is yours.
1:31:12 If it is great.
1:31:13 If not, you move on to the next name on the list until
1:31:16 either you find your name or you don't in which case you leave.
1:31:19 A linear search works in the same way,
1:31:21 you start with the first element in the list
1:31:24 compared to the value that you're trying to find.
1:31:26 And if they're the same,
1:31:27 you found your match and you return the index of that element.
1:31:30 And if not, you move on to the next element in the list until you
1:31:34 either find the thing you're searching for, or you run out of lists to check.
1:31:39 Pretty simple, right?
1:31:39 This is because linear searches are pretty bad when it comes to efficiency,
1:31:43 especially in the worst case scenario.
1:31:45 If the item you're searching for in the list is the last element,
1:31:48 you're going to have to check the entire list
1:31:51 of items before you find the one you're searching for.
1:31:54 On average, however, you're going to get it about halfway through the list.
1:31:58 This makes the linear search oven worst
1:32:00 case scenario since in the worst possible case,
1:32:02 it will take the entire length of the array or n to find the correct value.
1:32:07 But linear search on average will return the correct
1:32:09 index in O of n divided by two.
1:32:11 We're halfway through the list.
1:32:13 Now while the linear search is in great,
1:32:16 it can work with both sorted and unsorted lists,
1:32:19 because of the fact that it will eventually cover every element in the list.
1:32:23 The other search we're going to cover requires the list to be sorted,
1:32:27 which may seem like a drawback, but having a sorted list allows you to use
1:32:31 algorithms that are far more efficient than the linear search.
1:32:34 So overall, the linear search is a good basic
1:32:37 searching algorithm for if you have an unsorted list.
1:32:39 But if your list is sorted,
1:32:40 there are way more efficient options out there for you,
1:32:43 such as the binary search, which we'll be talking about now.
1:32:47 The binary search uses a recursive process to break the data
1:32:50 in your list down into more and more manageable bites.
1:32:53 Taking advantage of the fact that it's sorted
1:32:55 in order to find the item you're looking for faster.
1:32:58 This one is much harder to wrap your head around.
1:33:01 So let's start with an example.
1:33:02 Let's say you have a list of 10 names sorted alphabetically,
1:33:05 like shown on the screen now,
1:33:07 and you want it to find your name within that list.
1:33:10 in binary search, you would look for the middle most name,
1:33:13 in this case, the one at the fourth index.
1:33:16 Just a quick aside, since there is no
1:33:19 true middle since the list is 10 names long,
1:33:21 the computer automatically uses the next one down as the middle value.
1:33:26 Now, once you find your middle value,
1:33:29 you first check to see if the name you're searching
1:33:31 for at the index you've chosen is the name that you're looking for.
1:33:35 If it is you simply return that index.
1:33:38 But 99% of the time it's not going to be including right now.
1:33:42 So let's keep going.
1:33:44 If the value at the middle name is
1:33:46 not equal to the one you're searching for, you
1:33:49 check to see if the value you're searching
1:33:51 for comes before or after the middle index.
1:33:54 For example, if you were looking for the name Brandon,
1:33:57 and the value at the middle index was Carl,
1:34:01 Brandon obviously comes before Carl alphabetically.
1:34:04 And since we know that the list is sorted,
1:34:06 what we can now do is ignore the entire bottom
1:34:09 half of the list and just focus on the top.
1:34:11 Since we know that if Brendon is even in the list,
1:34:14 it's going to be in that top half.
1:34:16 Now we simply treat the top half of the list
1:34:19 as an entirely new entity and repeat the process over again.
1:34:22 Again, we would find the middle most element of this new list
1:34:26 of names and again compared to the name you're trying to find.
1:34:29 If it's the name we're trying to find we return that index, but if not,
1:34:33 we compare it to see if it comes before or after the middle index.
1:34:37 Going back to our example.
1:34:39 Let's say the middle index of this new list is AJ.
1:34:42 Now we know that Brennan comes after AJ alphabetically.
1:34:44 So what we can now do is ignore the top half of the list.
1:34:48 Since we know that if Brennan is in the list,
1:34:51 it's not going to be in the top half of our list.
1:34:54 Now we again repeat this process again and again
1:34:57 until we find the name we are looking for.
1:34:59 So if For example, the middle index is time is Brenden.
1:35:02 And that's what we're searching for.
1:35:04 So finally, we would return the index to in binary search.
1:35:07 Eventually, the index we were compared to our search term will be the same.
1:35:12 And once it is we can return the index and move on.
1:35:15 Now if we don't find it, which happens after we have eliminated the entirety
1:35:19 of the list without finding our search term,
1:35:21 the algorithm will simply return a null value to let you know
1:35:24 that the item you're searching for cannot be found in the list.
1:35:28 The binary search is way faster and more efficient than a linear search.
1:35:32 Since we are drastically cutting down the amount of elements,
1:35:35 we have to look at making the program run faster.
1:35:38 In almost 99.9% of cases, in which your list is sorted,
1:35:43 the binary search it's going to return a result faster than the linear search.
1:35:47 So if you have a sorted list, your best option is to go for binary.
1:35:52 As for efficiency, the binary search is O log n for the worst case scenario,
1:35:57 which could be confusing if you don't fully understand logarithms.
1:36:00 But all you need to know is that it
1:36:02 is way more efficient than the linear search.
1:36:05 Its average scenario is actually also o log n as well,
1:36:09 which again, is infinitely times more efficient than linear cases.
1:36:13 Now while there are other types of searching algorithms you can use,
1:36:16 these two are the most common for both unsorted and sorted lists.
1:36:19 So we will stop there for now.
1:36:21 Up next, we're going to be covering one
1:36:23 of the most confusing and important topics in computer science recursion.
1:36:29 Let's start with the most important question, what exactly does recursion mean?
1:36:35 in programming recursion refers to functions that repeatedly call
1:36:38 themselves meaning that any instructions that occur within a function,
1:36:42 one of the instructions will be a call to that same function you're already in.
1:36:46 In the extremely primitive example, on your screen.
1:36:48 Now, you can see we have some function which, in the confines of itself,
1:36:52 calls itself, a recursive function will usually
1:36:55 take into account some integer as an argument.
1:36:57 And we'll use this to carry out some instructions,
1:37:00 modifying the integer that was entered before calling
1:37:02 itself again with a new integer as its argument.
1:37:05 To better understand these functions,
1:37:06 let's discuss the basics of how we go about programming one of them.
1:37:10 A really good and easy example of a recursive function
1:37:13 is one which sums up all numbers from one to n.
1:37:16 So let's make a recursive function that does just that.
1:37:19 The first thing we need is the actual function.
1:37:21 And we're going to make it an integer function,
1:37:24 which takes an integer and as its argument,
1:37:26 the reason we do this will be explained later.
1:37:28 But for now, let's move on to the base case.
1:37:30 A base case is simply a definite value,
1:37:33 which all recursive statements are the ones
1:37:35 that are being repeatedly called as we
1:37:37 go through the function tried to get towards at the beginning of the function,
1:37:40 we test the value that was passed in by the argument
1:37:43 against the base case to see if it is satisfied.
1:37:45 Usually, these base cases are some requirements like of n as I described before,
1:37:49 which has a certain value or is equal to a certain value,
1:37:53 it is extremely important that the base case
1:37:56 is set to some requirement that and will
1:37:59 eventually meet for the same reason that it
1:38:01 is important to avoid an infinite loop.
1:38:03 We do not want a Stack Overflow error to occur.
1:38:07 For example, if our base case,
1:38:09 what's the stop calling the recursive function when n was greater than 100.
1:38:12 And if it is not, we will call the same function again.
1:38:15 But with n minus one.
1:38:16 And we started with ns, say 99,
1:38:18 we would never reach the base case and the recursive
1:38:21 functional or purely call itself over and over again,
1:38:24 subtracting one from n and hoping that somehow it
1:38:27 will eventually be greater than 100 until your computer crashes.
1:38:31 Not fun.
1:38:32 So anyways, back to our recursive some example,
1:38:36 let's make our base case when n is less than or equal to one.
1:38:40 This way when you start at some positive integer and and subtract
1:38:43 from it until it is less than or equal to one,
1:38:46 in which case we can exit the recursive statement.
1:38:50 Cool.
1:38:50 Now, if n is not less than or equal to one,
1:38:54 what we want to do is return the sum of both n
1:38:58 and then the returning value of our recursive sum method minus one.
1:39:02 Why do we add and the function call?
1:39:05 Well, let's actually go through the function as if we were at the computer
1:39:10 and see why we start with a call of recursive sum with n equals three,
1:39:14 we know that three is not less than or equal to one.
1:39:18 So now we tried to return a recursive sum of n,
1:39:22 which is three and the returning value of recursive sum within n of two.
1:39:27 We don't know what the returning value of recursive sum with an N of two is.
1:39:31 So we have to go through the function again,
1:39:34 only this time and is two again, and is not less than or equal to one.
1:39:40 And so this function will go through the lF
1:39:43 statement and return to plus another recursive statement,
1:39:46 in this case, the returning value of recursive sum with an N of one.
1:39:51 So once again, we have to go through
1:39:54 the recursive sum function to get that value that will be added to two and then
1:39:58 returned and added to three And then returned,
1:40:01 hang in there, we're close now in this function,
1:40:05 and is less than or equal to one, and so we return n, which is one.
1:40:11 Now we take that n, which again is one,
1:40:15 and that is what gets added to to the previous function call, and then return.
1:40:20 So this would return three.
1:40:21 Now, this three is what gets added to the first function call,
1:40:26 which is three, and so it becomes three plus three, which is six.
1:40:31 And finally, after all that time, we get six return from the function,
1:40:36 which if you've been following along at home,
1:40:39 three plus two plus one is indeed six.
1:40:41 Now this may seem like a waste of time,
1:40:44 since three plus two plus one is not a hard operation.
1:40:48 But those of you saying that I asked you to please
1:40:51 give me a sum of all the numbers from one to 3567.
1:40:54 Godspeed.
1:40:55 Now recursion is a very difficult concept to wrap your head around.
1:40:59 So if you're not 100% comfortable with it at the moment,
1:41:02 feel free to rewatch this section of the video
1:41:05 in order to better familiarize yourself with it.
1:41:08 Alright, cool.
1:41:09 Now that we have a little background on recursion,
1:41:12 let's talk about why it works so well.
1:41:14 Now to understand why and how recursion works,
1:41:17 we must first understand what a stack is.
1:41:19 A stack is a data structure that contains all
1:41:22 of the tasks you instruct your program to complete.
1:41:25 Based on a certain method,
1:41:27 your program will then carry out the tasks you give it is called a stack.
1:41:31 Because if we start another process before the previous one completes,
1:41:35 the process is stacked on top of the other one,
1:41:38 such as the animation on your screen is showing now,
1:41:41 programs we write will follow the LIFO structure.
1:41:44 For those unfamiliar with accounting LIFO means lastin.
1:41:47 First out, or the last item put on the stack
1:41:51 will be the first one removed from it.
1:41:54 Essentially, every time you ask your computer to complete a task,
1:41:57 that task is added to the stack, and will be the first one to be resolved.
1:42:01 Think of it like a stack of stones,
1:42:03 you can keep adding stones on top of your pile.
1:42:06 But in order to get to the one at the bottom,
1:42:08 you must first remove all the rocks on top of it.
1:42:11 Now when your functions continually cause itself without end,
1:42:14 without a base case, like in our infinite loop example,
1:42:17 then this stack will never be resolved,
1:42:19 as items will be continually added to the stack
1:42:22 without any of them ever been completed.
1:42:24 In this case, the memory allocated to the stack exceeds the maximum
1:42:29 allowed and a Stack Overflow error occurs resulting in your program crashing.
1:42:34 Think of this as if you're doing chores.
1:42:37 And before you complete one chore, you get called to do another chore.
1:42:41 And then before you can complete that one, you get called to do another one.
1:42:45 Since you keep stacking tasks, or shores on top of one another,
1:42:49 the stack of tasks will never be completed.
1:42:51 And you will probably die before ever finishing all of your chores.
1:42:55 This is the same logic that makes infinite loops crash your program.
1:42:59 recursion works on these same principles.
1:43:01 The initial call makes a second call, which is added to the stack.
1:43:05 And now that one must be taken care of first.
1:43:08 But in that one, another function is called which gets added to the stack,
1:43:12 and so on until you reach the base case
1:43:15 and what you slowly start going back down the stack.
1:43:19 In conclusion, recursion in general is extremely useful,
1:43:22 because by calling the same functions repeatedly,
1:43:25 it breaks down the problem into smaller sections,
1:43:28 and results in the program being more efficient.
1:43:30 small parts of problems are easier to solve and less
1:43:34 taxing to compute than the entire problem at once.
1:43:36 And the computer can combine these small
1:43:39 solutions into the whole solution at the end.
1:43:41 Now as we wind down our introduction to programming series,
1:43:44 we want to take some time and go over some
1:43:47 of the soft skills needed to be successful computer scientists.
1:43:50 Since it's not all about writing code.
1:43:52 In fact, many professional computer scientists will tell you that the majority
1:43:56 of their job is spent thinking about code rather than actually writing it.
1:44:00 This is because much of programming boils down to problem solving.
1:44:03 How do we optimize the system?
1:44:05 How can we make this feature for our app?
1:44:07 What type of movement Do we want for our game?
1:44:10 And how can we program it?
1:44:11 The harsh truth is that no good program
1:44:14 has ever been written simply from the programmer,
1:44:16 getting the prompter idea, sitting down,
1:44:17 hopping on an ID and starting to write code.
1:44:20 There are many tasks we should go through
1:44:23 beforehand in order to plan out our code.
1:44:25 So we ensure that when the time comes to program,
1:44:27 it's a clean and easy process and not riddled with mistakes and bugs.
1:44:31 This is where pseudocode comes into play.
1:44:34 Think of pseudocode like this, if you wanted
1:44:36 to take a family trip to the Grand Canyon,
1:44:38 would you simply hop in your car and drive off and figure out things later?
1:44:42 No, because that would be ridiculous.
1:44:44 Instead, you would spend some time planning out the trip.
1:44:47 What sites or places do you want to visit?
1:44:50 What hotel reservations are you going to have to make?
1:44:52 What kinds of things are you going to do when you get there?
1:44:55 What routes or highways are you going to take and why?
1:44:57 All of these things must be determined before you
1:44:59 can even think about hopping in your Ford Explorer.
1:45:01 So how does this translate to pseudocode?
1:45:03 Well think of our family trip to the Grand Canyon as a program,
1:45:08 programmers use pseudocode pseudo meaning not real,
1:45:11 and code, meaning code as a means
1:45:13 to plan out their programs before they write them.
1:45:16 Just like how we planned out our trip before going.
1:45:18 They throw away syntax and naming conventions for variables
1:45:21 and just focus on what they want the program to accomplish,
1:45:25 and how they plan on doing that.
1:45:27 pseudocode is very similar to constructing
1:45:29 an outline for a paper that you're writing,
1:45:31 you write down the main topics of the essay,
1:45:33 and plan out your major talking points.
1:45:35 But you don't worry about the nitty gritty details of it all,
1:45:38 such as word choice, grammar, conventions, and proper formatting.
1:45:42 By doing this, we allow ourselves to think freely and not
1:45:45 worry about stressing the small stuff, at least not yet.
1:45:47 Alright, now that we know what pseudocode is,
1:45:50 let's talk about how we write pseudocode.
1:45:53 You see, the best part about pseudocode is that it can
1:45:55 take the form of many different things for many different people.
1:45:58 Each computer scientist has their own methodology for planning out their code.
1:46:02 And there are probably hundreds of different methods
1:46:04 of writing pseudocode that are out there today.
1:46:06 Today, however, I'd like to focus on three popular ones
1:46:09 that I think you might find to be extremely useful.
1:46:12 The first of these is known as flowcharts.
1:46:14 And mainly they can be used to think
1:46:17 through the process of a particular function.
1:46:19 a flowchart is fundamentally a graphical representation
1:46:22 of a function and how it might flow.
1:46:24 Many programmers do this and lay out the conditional
1:46:27 statements and loops that they want as different
1:46:29 blocks in the flow chart connected by arrows
1:46:32 and charting out every path of their function.
1:46:34 From there, it's extremely easy to create test cases and follow them
1:46:37 through the flow of the function through the different blocks and arrows.
1:46:41 For example, we could have a flow chart that goes
1:46:44 something like this, a user enters in a number,
1:46:46 and if the number is eight, I want the program to return true.
1:46:50 However, if the number is not eight, then I want it to return false.
1:46:54 It's a great way to visualize what the functions overall purpose is,
1:46:58 and also look for any errors that you
1:47:00 may have missed when thinking about the function, such as a missing path.
1:47:03 It also abstracts the programming statements up to simple blocks,
1:47:07 making it easier to modify or change completely.
1:47:10 The best part is that when you have finished testing cases,
1:47:13 you can simply convert the blocks into programming statements,
1:47:16 and you have a well written function without any debugging.
1:47:20 Another popular pseudocode technique that is often used is to simply
1:47:24 write out what you want your code to do chronologically.
1:47:27 don't necessarily think about what programming
1:47:29 statements and functions you want to use.
1:47:31 just jot down from start to finish what it is
1:47:34 the program you're writing is going to do step by step.
1:47:38 For example, let's say you're making an app
1:47:40 that takes in two numbers and divides them.
1:47:42 The pseudocode for that would look a little something like this.
1:47:45 First, I want to prompt the user to enter a number.
1:47:48 And then I want to wait for the user to enter in that first number.
1:47:52 After I get the first number,
1:47:53 I want to again prompt the user to input a second number.
1:47:57 Once they do, I complete the operation by defining the two
1:48:00 numbers entered and returning the result back to the user.
1:48:03 This all seems like it would be common sense.
1:48:06 But remember that oftentimes,
1:48:07 we're not going to be working with simple multiplication or division functions.
1:48:10 We may be working with full scale games,
1:48:13 algorithms or user interfaces with many different options.
1:48:16 This method allows you the programmer to not get bogged
1:48:20 down with the syntax and conventions that you have to follow,
1:48:23 you're simply making a note of what the program's ultimate goal should be,
1:48:26 as if you were explaining it to a friend of yours.
1:48:29 This method really lets you plan out everything that needs
1:48:32 to happen in your program in order for it to run smoothly.
1:48:34 It also ensures you don't forget about a piece of an algorithm
1:48:38 or a certain function that you need to write in afterwards.
1:48:41 And the final pseudocode strategy that I'd like to talk about to you today is
1:48:45 writing out the main features you want the user to have when using your program,
1:48:50 and what functions or smaller programs,
1:48:52 you're going to need to complete these features.
1:48:54 Let's do another example.
1:48:55 Say you're making a banking interface.
1:48:57 And on startup, you want the user to initially have two options,
1:49:01 they can set up a new account, or log into an existing account.
1:49:06 From there, if they log into their account,
1:49:08 then you want them to have the functionality to withdraw money,
1:49:11 deposit money, take out a loan or pay back a loan.
1:49:14 If they decide to set up a new account,
1:49:16 you want them to be able to create an account,
1:49:19 store their information in a database,
1:49:21 and then access all of the features that are returning member would have.
1:49:25 This may look very similar to the flow chart method.
1:49:28 The only difference being that this is
1:49:30 abstracted one level higher over an entire program,
1:49:32 rather than just a single function.
1:49:34 If you really wanted to, you could also create a flowchart
1:49:38 that would go through the functionality of all the four methods described above.
1:49:41 Setting up a hierarchy like seeing on your screen now makes it easy
1:49:45 and clear to see every function interface you're going to have to make.
1:49:49 This prevents you from having to try to shoehorn a function
1:49:52 or feature into an already finished program at the last second,
1:49:55 which is not a very fun experience in the slightest.
1:49:58 So There you have it three pseudocode strategies you can use to plan
1:50:03 out your code before you even start writing any the flowchart method,
1:50:06 which is good for thinking through the flow of a certain function.
1:50:09 The write up method,
1:50:10 which is good for getting the general idea down for a program,
1:50:14 or the functionality planning method,
1:50:15 good for listing out the functions of a certain programs,
1:50:18 you can use all of them, none of them are a mixture of them,
1:50:22 or even disregard these and find and create your own pseudocode methods.
1:50:25 The main goal here is to drastically minimize the amount of errors
1:50:29 that occurred during your program and relieve a lot of stress on your head.
1:50:33 The importance of pseudocode cannot be stressed enough.
1:50:35 And if you don't believe me,
1:50:37 I urge you to try to complete a large project without it.
1:50:41 Okay, so if you've watched the series up until this point,
1:50:45 you've gotten a pretty good understanding of many aspects of programming,
1:50:48 and also how to plan out your programs.
1:50:50 Now it's time to go out into the real world and write some actual code.
1:50:54 But what kind of program I can hear you asking me?
1:50:58 And the answer is truly whatever you want, really, as I'm sure you know,
1:51:02 by now, you can program just about anything you have on your mind.
1:51:06 Anything from simple games to complex software.
1:51:08 This video has equipped you with the basics that are going to need
1:51:12 to be used in pretty much any program you decide to write.
1:51:14 But that doesn't mean that every
1:51:16 programming language is perfect for every application.
1:51:19 Each language has its own strengths and weaknesses.
1:51:21 and choosing the right one is very important for making it easier,
1:51:25 and sometimes just even possible for you to program what you want.
1:51:29 So that's what we're going to be talking about now,
1:51:32 choosing the best language for what you want to accomplish.
1:51:35 Now, we talked earlier about low level versus high level programming languages.
1:51:39 In case you forgot, let's do a quick refresher.
1:51:42 higher level programming languages have a higher
1:51:45 level of abstraction from machine language,
1:51:46 that series of zeros and ones from way back when,
1:51:49 while lower level programming languages have
1:51:52 a low level of abstraction for machine language.
1:51:55 For example, block program where you can drag and drop programming
1:51:59 statements together like 2d Legos would be a high level language,
1:52:02 as it does not take a high level of understanding
1:52:05 of the inner workings of a computer to programming.
1:52:08 The theoretical highest level of a programming language would be if I could
1:52:12 just write down what I wanted the computer to do in Simple English,
1:52:15 and it would just work.
1:52:17 But sadly, that doesn't exist yet.
1:52:19 On the other side of the spectrum,
1:52:22 the lowest level programming language would be just feeding
1:52:25 zeros and ones into the computer at supersonic speed,
1:52:28 which would be almost impossible and extremely absurd.
1:52:31 So how do you choose what type of language is best for your needs?
1:52:35 Well, it depends on what you are trying to do,
1:52:38 as sometimes you need very specialized languages to get done what you want.
1:52:42 The world of computer science is vast and contains many fields.
1:52:46 So trying to cover everything in one language would be impossible.
1:52:49 This has led to the creation of hundreds of different programming languages,
1:52:53 each designed for a specific task.
1:52:55 Right now though, we'll cover some of the most popular languages and their uses.
1:53:00 Now, if you are trying to design a website,
1:53:03 using HTML and CSS is probably your best bet.
1:53:06 HTML is a markup language that is designed for writing the content of a website.
1:53:12 While CSS is great for designing the style of the website.
1:53:15 you interact with HTML every day.
1:53:17 And you can even see it right now.
1:53:19 If you right click and hit Inspect Element.
1:53:21 This will truly show you how complex HTML and CSS can be.
1:53:25 Maybe it would be best for you to use a scripting language.
1:53:29 A scripting language is a language that has many commands for you to use.
1:53:32 And that can also be run without needing to be compiled.
1:53:34 Scripts can be faster to write in most programs, and tend to be easier to port
1:53:40 between operating systems allowing for cross platform support.
1:53:43 Scripts can also be used with websites,
1:53:46 oftentimes adding to the overall user experience of the site.
1:53:49 If you want to go into web design, this might also be a path for you to go down.
1:53:55 Examples of scripting languages are Perl, PHP, Ajax, and JavaScript.
1:53:58 For most programs, you could probably use a general purpose language.
1:54:04 General Purpose languages, as they sound have a wide range of applications.
1:54:10 Usually, these should be your go to languages.
1:54:13 Examples of general purpose languages are Java, c++, and Python.
1:54:18 They each have their own different benefits over one another.
1:54:21 Java is very useful for developing games and interactive web pages.
1:54:25 Python can act as a scripting language for web programming,
1:54:28 as well as writing applications and data analysis.
1:54:31 And c++ tends to be used for writing applications and system programs.
1:54:35 They all have a variety of packages that you can
1:54:38 import and use to achieve the functionality you need from them.
1:54:41 while selecting the right general purpose language
1:54:43 for your big projects is very important.
1:54:45 For most of your programs, any one of them will work.
1:54:48 It really comes down to preference.
1:54:50 Get to know each language and decide which ones
1:54:53 syntax rules you like best and find most comfortable.
1:54:56 If you get to know one general purpose
1:54:58 language really well and enjoy programming with it,
1:55:00 you can apply it to just about any of the programs you plan on writing.
1:55:04 Personally, I tend to use Python for most of my projects.
1:55:07 This is mostly not due to any functional
1:55:09 difference between Python and any other general purpose language,
1:55:12 though there are a few, but it is mainly because I find it syntax
1:55:16 rules most convenient and easiest to write programs with.
1:55:19 Overall, either you can consider the product you plan on doing
1:55:22 and research and see which language
1:55:25 boasts the most advantages for your purposes.
1:55:27 Or you can simply become comfortable with a language and use
1:55:31 it for most of the programs you decide to write.
1:55:34 will now be looking at our final
1:55:37 topic of this introduction to programming miniseries,
1:55:39 you now have the basic knowledge of programming,
1:55:41 which will take you far in any language that you decide to learn, you know,
1:55:45 some good pre programming pseudocode strategies to help
1:55:48 you design your code from the ground up.
1:55:50 And you might already have a good idea as to the type
1:55:53 of programming language you might want to start with.
1:55:55 So what's the next step?
1:55:56 How can I learn that language?
1:55:58 And what applications can it be used for?
1:56:00 Well, that's what we're going to be covering now.
1:56:02 So let's just jump right into it.
1:56:04 Starting with the biggest question, which is what is the next step?
1:56:08 Well, now that you might know which type
1:56:10 of language you might be interested in, research
1:56:12 that language and find out whether or not
1:56:15 you truly want to pursue that programming language.
1:56:17 Most languages like Python or c++ will have either
1:56:20 an official website where you can read up on the language,
1:56:23 or Wikipedia page, which will provide you with useful information
1:56:26 in deciding whether or not you want to pursue that path.
1:56:29 From there, the next step is to actually learn the language,
1:56:32 which can be done right here on YouTube.
1:56:35 While we've taught you the basics of any programming language,
1:56:37 each specific language is going to expand upon these concepts.
1:56:41 And so watching tutorial videos on a certain
1:56:43 language is going to be very beneficial.
1:56:45 I would start with an introduction series,
1:56:48 like the one you're currently watching both for the language
1:56:51 you've chosen and work your way through that series,
1:56:53 picking up on the syntax and rules
1:56:55 of that language until you become comfortable with it.
1:56:58 Once you do that, you come to a crossroads.
1:57:01 You know how to program in a certain language,
1:57:04 but you may be completely clueless as to what to make in that language.
1:57:07 Programmers blog can leave you uninspired and not want to continue programming.
1:57:11 So I'd like to give you a few websites to help out.
1:57:15 First is coding bat,
1:57:16 a completely free website which has hundreds of coding challenges
1:57:19 in both Java and Python to help you refine your programming skills,
1:57:23 and even learn some new programming shortcuts and tips.
1:57:26 This is great if you want to get better at improving
1:57:29 your efficiency and need something to hone your skills as a developer.
1:57:33 The next is coder bite, which offers over 200 plus challenges that you
1:57:37 can complete in over 10 different languages,
1:57:39 something that is sure to help you improve.
1:57:42 The final website I'd like to talk about is hacker rank,
1:57:45 which not only provides programming challenges to keep you on your toes,
1:57:48 but also provides support for you using
1:57:50 your programming skills to find jobs or internships.
1:57:53 These and many more websites exists solely to keep you interested
1:57:56 in code and work on refining your skills to become better.
1:58:00 Now if you're a teenager watching this series in high school,
1:58:03 I also encourage you to take programming classes in your high school.
1:58:06 AP Computer science principles and AP Computer Science a are
1:58:09 both amazing courses which can help you greatly in the future,
1:58:13 and are also incredibly informative and important to colleges.
1:58:16 Your school might also offer other classes in the field of computer science,
1:58:20 including ones on key data structures, game design, and data science.
1:58:24 Any and all classes you can take to help expand your knowledge
1:58:27 of programming and help you find your niche is going to be extremely helpful.
1:58:32 As you can see, the world of code has now been opened up to you.
1:58:36 These are just a few examples of where you can go from here.
1:58:39 But there are many more we didn't talk about.
1:58:41 You could get into GitHub and start contributing to projects.
1:58:43 You could work on your own projects and collaborate with others.
1:58:47 The possibilities are endless.
1:58:48 The next step is up to you.
1:58:51 This concludes our introduction to programming mini series.
1:58:53 We hope you enjoyed watching it as much as we enjoyed making it.
1:58:57 If you enjoyed the series as a whole consider subscribing to our channel,
1:59:01 no pointer exception,
1:59:02 which will be linked in the description for more content like this coming soon.
1:59:06 Thank you so much for watching.