Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
And this just verifies that I have both of them in place and I'll just come back up and start typing the code that's indented inside my code block. So we are just trying to prove a point here. So I'll do something like alert, the balance is positive, and we have a very straightforward condition. But the question is well, what if it's not? What if that was false? I could do another if statement and check balance again, but what I could also do instead is if that's not the case, I am going to add the word else.
Then I'm going to do the opening curly brace for the code block again because I wrote that, do the closing one, and what we have going on is if the condition is true, we'll do whatever is in the first code block. Otherwise or else, we'll do whatever is in the second code block. So I'm going to write a line of code here, closing that with a semicolon. Now, I have introduced a very common error here and it's an error in my thinking, not in the code.
We ask if balance is greater than 0, we're going to output a message saying the balance is positive. Otherwise, we'll output a message saying the balance is negative. Well, what happens if it's 0? In this case, we're going to hit the if statement. If balance is greater than 0, well that would be false. So if our balance is 0, it will give us the balance is negative message. really not what we want, but it's quite a common error to get hit with, particularly when you're dealing with things like positive and negative numbers. So be a bit too exclusive about what you're checking against.
Thinking a bit deeper about it, what I want to ask here is if the balance is greater than or equal to 0. And of course what we are asking there is as far as our program is concerned, what do we care about? Let's say we are dealing with bank balances, where 0 would not count as a negative balance. Now, there is an operator for that. It's just the greater than or equals sign. This is regarded as a single operator. So it's just the two characters next to each other. What we can also do is take this a little bit further.
Inside this first code block that will be executed if the balance is greater than or equal to 0, I could add another if statement. If balance is greater than 10,000, do my opening curly brace for the code block, couple of lines, and then do the closing one. And notice that what I am doing here is trying to make this apparent. I am lining up my closing curly brace with its corresponding if statement, the same way that I would line up the first closing curly brace with its corresponding if statement, and then I would indent any code inside of that.
If we do have to have more complex logic, there are other techniques we can use to deal with it, such as working with functions which we're going to get to soon.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98575 Viewers
61 Video lessons · 85870 Viewers
71 Video lessons · 69760 Viewers
56 Video lessons · 102063 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.