Practical Guidelines for Working with Complex Coding
Working with complex conditions
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.
User Experience Fundamentals for Web Design453,594 Views
Foundations of Programming: Data Structures452,603 Views
WordPress.com Essential Training113,015 Views
Foundations of Networking: Networking Basics313,427 Views
Becoming a Web Developer: Full Stack vs Front End154,497 Views
Foundations of Programming: Object-Oriented Design3,524,340 Views
PHP with MySQL Essential Training4,271,059 Views
HTML Essential Training3,810,033 Views