Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We've written a few lines of code, but so far, what's been happening is this. We start at the first statement, we execute it, we move onto the next, we execute it, we move on as fast as the computer can fully execute each line. Now it might pause if we're prompting for information from the user, but as soon as we come back, we move on again, top to bottom, each line in order, no exceptions, and it's kind of dumb really. You see beyond the most simple Hello World level programs, we need our code to be smarter than this.
We need to start asking questions. If the bank balance is positive, calculate interest; if it's negative, charge a penalty. If they check the checkbox on the form, add them to the mailing list; if they didn't, then don't. If the position of the missile image is the same as the position of the spaceship image, then show the explosion image. We need to have code that sometimes runs, and sometimes doesn't depending on the conditions of the program at that time. This is conditional code. We begin with the classic if statement, which is found in every programming language.
Bear in mind that in programming languages, we are very picky about the symbols we use and about what we call them. So just to be really specific here, when I say parentheses, I mean these; when I say brackets, and I'll usually say square brackets; I mean these ones and when I say braces, and I'll try and say curly braces, I mean these ones. They do serve similar purposes, they are mocking where something starts and where it finishes, but they are not interchangeable. Now, unlike things like say a semicolon or an equals sign, when you see parentheses, brackets, or braces, they are always found in pairs.
Now here is the thing. Whatever is inside these parentheses must evaluate, must break down, as being either true or false. So if I am checking here if the variable b is greater than 20, I don't care what it is. I don't care if that's 21 or 25 million, or 20.000000001. All I want to know is, is this true or is this false? All conditions must boil down to simply true or false.
So in this case, if d is not equal to 100, is that true or is that false? Now, when you have several statements surrounded by curly braces, that's referred to as a code block and this is all that these curly braces are doing. They are grouping a section of code together; they don't have any other meaning than that. And as you'll see, blocks can be nested inside each other if you need to. Now, what you'll also see is the code inside a code block is indented. Our alert statement is about three spaces in from the if.
It doesn't have to be. Again, white space is insignificant. It just makes it much easier to read. So here is an example. On the first two lines I am creating a couple of variables, variable a = 5, variable b = 10. Then I am checking if a is less than b, we open the code block and we're going to execute this line if that is true. Just pop up an alert, yes, a is less than b, and then we have a second if statement. if a is equal to b. Again, this must be either true or false.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101128 Viewers
61 Video lessons · 87889 Viewers
71 Video lessons · 71763 Viewers
56 Video lessons · 103634 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.