Fundamentals of Programming, Programming Elements, Branching. Oftentimes we need to choose different paths through our code. For example, if the date is bad, we might not want to process it. Or under some specific circumstance we might want to take extra actions. So we can make a decision, we can evaluate a formula to a Boolean, so we refer to this as a predicate, an assertion. And if that condition, that predicate, that Boolean expression is true, we can go one way, and if it's false, we can go the other way. The general idea is expressed here in terms of code. We have an IF statement, with that condition, again the condition is an expression that will result in a Boolean. So a true or false. And that IF will be associated with a block of code to be executed only if the condition is true. Well, that's I suppose is simple enough, but we can do more. Now everything more is optional, that's what the square brackets here are indicating. Remember, this is just pseudo code on the screen. This is the general idea. Nothing here is to be presumed to be actual programming syntax, we'll look at our Python syntax shortly. So [else-if] and another condition, and code to be executed only if that condition is true. So the [else-if] is checked only if the original condition were false. And in some languages like Python, the [else-if], there actually is an actual elif statement. In other languages like C, C++, and Java, it's just a combination of an else with another if. And speaking of [else], so given an IF and a condition, and then zero or more [else-if] with other conditions. And again, think of it as a list, first, this one, then check the next, the next, and the next, as soon as one is true, we're done. We only continue through that list if they're all false so far. If we get to the [else] which has to be the end of all this, that code is executed only if we got there, which means only if no condition has been true. So on this slide, since this code is to be executed, so we're talking about a block of code. And different languages delineate these blocks of code in different ways. As the earlier reading showed, Python uses indentation, and we'll see that again when we go to our demo. C, C++, Java, JavaScript, otherwise known as, more properly as ECMAScript, use braces. Other languages may use keywords, it doesn't matter. Again, we're not going to be focused, other than looking at our Python demos on language specifics. So here we have our current example, our area of circle. Let us now introduce the error checking that we talked about in the earlier lectures. So, if not isinstance (diameter), and we want it to be either an (int, float). If it is not one of those things, we will raise an exception. We're not going to go deeply into exceptions in this class. The idea, however, is worth understanding. There are different ways in which a function can indicate that there has been some sort of failure. I could return a negative value or some other sort of error code. But in Python or Java or other modern languages, we tend to indicate erroneous conditions with something called an exception. We already saw one when I had an accidental typo in an earlier lesson, and I will now show you doing them deliberately. What it would look like to receive them. We're not going to worry about handling the exceptions, we're only going to look at what it means to have one. So the exception I'm going to raise here is a type error, one of the built-in exceptions in Python. So here I'm simply reporting that it's a TypeError, that the diameter must be a number. elif, which is python's way of dealing with else-if, and you'll notice the indentation, raises indented, the elif is back out. elif diameter < 0:, I'll handle 0 because 0 will give me 0 for the area, but less than 0, that's going to be an error. So we will raise another built-in exception, a ValueError ('Diameter must be greater than or equal to zero!'). else, and in this case it's the original code, so I'm just going to indent it, and now we're done, area of circle. If we receive anything that's not an int or a float, that will be an error. If the diameter is less than zero, that'll be an error. Otherwise we will do our normal calculation. So, we'll save this and run it. area_of_circle, and our standard 16 inch pie. There's our value. I want a ("Medium with Extra Cheese''). And here's that exception, TypeError: Diameter must be a number! And the programmer calling area_of_circle would have to know how to handle exceptions. I just want you to see what an exception is and how it's used to report an error. We are not going to program so deeply in this concepts course as to actually have to handle them. And finally, let's have a pizza with a negative diameter, and there's the ValueError. So, here's what we were focused on. It was the branching, remember. Here's our condition, and here's the code to execute. If that condition were false, then we would check this condition, and execute this code if that were true. If they're both false, in other words, if nothing is true, we'll come down to our [else] and execute that. And that is how we're branching demo. In our next lesson, we'll look at looping or repetitive execution.