yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Nested conditionals | Intro to CS - Python | Khan Academy


4m read
·Nov 10, 2024

What happens if you indent a conditional inside another conditional? To trace how the computer executes a program with nested conditionals, we need to look at the indentation. We know that an if starts a new conditional, so that means we have two conditionals here. But which if does this else branch belong to? The computer matches if and else branches to if branches based on the indentation.

These three lines are not indented; they're right up against the left margin. Then these lines of code are indented one level in—that's one tab over. Then this line of code is indented two levels in—that's two tabs over. So this else branch must belong to this if branch because they're at the same level of indentation.

If instead I indented it one level over, it would now belong to this if branch because these would now be at the same level of indentation. We can visualize this another way using a control flow diagram. We ask the user to enter a password, and then we check the condition: does it have enough characters? If the answer to that is no, that's the else branch, then we display an error message.

But if the answer to that is yes, that's the if branch, then we change the password. Only then do we ask the question: is it an easily guessable password? We only want to ask this question if the answer to this other question was yes—that's a nested conditional.

So in the code, this conditional would be indented inside this conditional, and then we only display a warning if the answer to this second question was yes. What about nesting a conditional inside the else branch? Here, I've reversed our condition. So now I'm asking: is the password too short? If yes, I print an informative error message. If no, then I change the password.

But what if I want to do that thing where I ask the user to enter the password a second time to confirm and only change the password if they match? I'm going to want another input function call where I ask the user to re-enter the password, and I only want that to happen if the first password they entered was valid. So I'm going to put it inside the else branch.

Then I want to check if the two passwords match. I only need to check this if they re-entered a password in the first place. If they didn't, I don't care, so I'm going to nest this condition inside the else branch. If they don't match, I'll print an error message, and if they do match, then I'll change the password.

Now let's test this with a few different inputs to see the execution path. If I enter a short password where this condition evaluates to true, then I print this error message and skip everything inside the else branch. I'm not asked to re-enter a password, and neither of these print, so that means this conditional isn't executing.

If I enter a password that's long enough, then I'm asked to confirm. If I type something wrong, we're seeing that this conditional is executing because I'm getting passwords don't match. In the last possible case, if it is long enough, I re-enter and they do match, then I see this else branch is executing and I'm getting password changed.

Note that this execution, with this conditional nested inside the else branch, is different from if I didn't indent this conditional at all. Without the nesting, the computer always executes this conditional, so it always checks if the passwords match. If my password was too short, it doesn't really make sense to check this condition; I don't have a confirmed password.

In fact, this gives a name error. I can get around that name error by initializing confirm password to the empty string. But now, if the password's too short, this condition will always evaluate to true and we print "passwords don't match," which I don't want because it's confusing to the user. They weren't asked to confirm, so they're like, "What do you mean, doesn't match? Doesn't match what?"

So if you only want to check a condition based on the result of another condition, you can nest it inside the if, elif, or else branch of the first conditional. Technically, you can go as many levels deep here as you want. You can be nesting an if inside another if, inside an elif, inside an else, inside an if.

But now we're four levels of indentation deep, and it's really hard to read how the control flow works. So try to go more than two layers deep—three if you have to. With compound conditions, chained conditionals, and nested conditionals in our toolkit, we can often rewrite our code in different ways to reduce the level of nesting. So always make sure you're evaluating that trade-off between readability and efficiency.

More Articles

View All
Dividing polynomials of degree one | Algebra 1 (TX TEKS) | Khan Academy
What we’re going to do in this video is get some practice dividing expressions. So, what do I mean by that? So let’s say that I have the expression 6X + 12, and I want to figure out what that divided by, maybe I’ll write this in a different color: divided…
Irregular plural nouns | base plurals | The parts of speech | Grammar | Khan Academy
Hello, Garans. I wanted to talk today about a different kind of a regular plural. So, we’ve been talking about regular plurals, where you take a word, and you add an S. For example, the word ‘dog’ becomes ‘dogs.’ You add an S, and that this is the regula…
Rare Dumbo Octopus Shows Off for Deep-sea Submersible | National Geographic
Oh oh oh oh! Look, we got a little octopus up in the comments. You get rewarded after all those sea pigs. All right, valet crew, here we go! All right, I’m gonna paint it with the lasers, and I’m gonna turn them off for some really good imaging. Yeah, ye…
why i don't spend money
These are five ways that I’m able to save a lot of money: Reuse grocery bags as trash bags instead of buying them from Costco. Only do laundry after 9 PM on weekdays when electricity is the cheapest. Skip the bottled water and drink right from the tap. Y…
Cannon Shock Waves in Ultra Slow Motion - Smarter Every Day 200
Hey, it’s me Destin. Welcome back to Smarter Everyday. Today, we’re in rural Tennessee for an actual artillery competition. And if you learn anything from this truck that’s coming up behind me, this is pretty legit. There’s dudes from all over the country…
Calculating height using energy | Modeling Energy | High School Physics | Khan Academy
So I have an uncompressed spring here, and this spring has a spring constant of 4 newtons per meter. Then, I take a 10 gram mass, a 10 gram ball, and I put it at the top of the spring. I push down to compress that spring by 10 centimeters. Let’s call that…