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 L 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 that is no that's the else Branch then we display an error
message but if the answer 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 and 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 El 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 and I skip everything inside the else Branch I'm not asked to reenter 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 and if I typo something we're seeing that this conditional is executing because I'm getting passwords don't match and in the last possible case if it is long enough I reenter and they do match then I see this El branch is executing and I'm getting password changed note that this execution with this conditional nested inside the El 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 because 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 L if 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 L if 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 chain 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