Building with the if statement
Video: Building with the if statementWe'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.
- Where to go from here
Viewers: in countries Watching now:
Finally, the course compares how code is written in several different languages, the libraries and frameworks that have grown around them, and the reasons to choose each one.
- Writing source code
- Understanding compiled and interpreted languages
- Requesting input
- Working with numbers, characters, strings, and operators
- Writing conditional code
- Making the code modular
- Writing loops
- Finding patterns in strings
- Working with arrays and collections
- Adopting a programming style
- Reading and writing to various locations
- Managing memory usage
- Learning about other languages
Building with the if statement
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.
Find answers to the most frequently asked questions about Foundations of Programming: Fundamentals .
Here are the FAQs that matched your search "" :
- Q: Using TextEdit with Mac OS 10.9 Mavericks?
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.