[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.21,0:00:02.04,Default,,0000,0000,0000,,- [Instructor] We can use\Nan IF statement to control Dialogue: 0,0:00:02.04,0:00:04.71,Default,,0000,0000,0000,,that a particular block\Nof code only executes Dialogue: 0,0:00:04.71,0:00:06.96,Default,,0000,0000,0000,,when the condition evaluates to true. Dialogue: 0,0:00:06.96,0:00:08.61,Default,,0000,0000,0000,,But what if we wanna do something else Dialogue: 0,0:00:08.61,0:00:11.01,Default,,0000,0000,0000,,only when the condition\Nevaluates to false? Dialogue: 0,0:00:11.01,0:00:13.80,Default,,0000,0000,0000,,Well, we can add another\NIF statement and try Dialogue: 0,0:00:13.80,0:00:16.17,Default,,0000,0000,0000,,and construct a condition\Nthat's the exact opposite Dialogue: 0,0:00:16.17,0:00:17.76,Default,,0000,0000,0000,,of the original condition. Dialogue: 0,0:00:17.76,0:00:19.08,Default,,0000,0000,0000,,That's a bit annoying Dialogue: 0,0:00:19.08,0:00:21.66,Default,,0000,0000,0000,,and sometimes the opposite\Ncondition isn't obvious Dialogue: 0,0:00:21.66,0:00:23.04,Default,,0000,0000,0000,,or it's super long. Dialogue: 0,0:00:23.04,0:00:24.03,Default,,0000,0000,0000,,To save us the trouble, Dialogue: 0,0:00:24.03,0:00:26.88,Default,,0000,0000,0000,,Python instead lets us use an else branch. Dialogue: 0,0:00:26.88,0:00:30.42,Default,,0000,0000,0000,,We can stick an else branch\Nonto the end of any IF statement Dialogue: 0,0:00:30.42,0:00:33.27,Default,,0000,0000,0000,,and any instructions indented\Ninside the else branch Dialogue: 0,0:00:33.27,0:00:36.42,Default,,0000,0000,0000,,only execute if the corresponding\NIF statements condition Dialogue: 0,0:00:36.42,0:00:38.13,Default,,0000,0000,0000,,evaluates to false. Dialogue: 0,0:00:38.13,0:00:39.30,Default,,0000,0000,0000,,That makes the IF branch Dialogue: 0,0:00:39.30,0:00:42.51,Default,,0000,0000,0000,,and the else branch mutually\Nexclusive based on the answer Dialogue: 0,0:00:42.51,0:00:43.34,Default,,0000,0000,0000,,to the condition, Dialogue: 0,0:00:43.34,0:00:47.04,Default,,0000,0000,0000,,the computer decides which\Nof the two paths to take. Dialogue: 0,0:00:47.04,0:00:48.90,Default,,0000,0000,0000,,The Python syntax for an else branch Dialogue: 0,0:00:48.90,0:00:50.43,Default,,0000,0000,0000,,is just the keyword else Dialogue: 0,0:00:50.43,0:00:51.72,Default,,0000,0000,0000,,followed by a colon. Dialogue: 0,0:00:51.72,0:00:54.03,Default,,0000,0000,0000,,Again, we use indentation\Nto tell the computer Dialogue: 0,0:00:54.03,0:00:56.64,Default,,0000,0000,0000,,which lines of code are\Ninside the else branch. Dialogue: 0,0:00:56.64,0:00:58.83,Default,,0000,0000,0000,,An else branch must always\Nfollow an IF branch. Dialogue: 0,0:00:58.83,0:01:01.26,Default,,0000,0000,0000,,Otherwise, what are we\Ntaking the opposite of? Dialogue: 0,0:01:01.26,0:01:03.27,Default,,0000,0000,0000,,Let's trace that execution path. Dialogue: 0,0:01:03.27,0:01:05.70,Default,,0000,0000,0000,,If the condition evaluates\Nto true as normal, Dialogue: 0,0:01:05.70,0:01:08.10,Default,,0000,0000,0000,,the computer goes on to\Nexecute any instructions Dialogue: 0,0:01:08.10,0:01:10.35,Default,,0000,0000,0000,,indented inside that IF branch. Dialogue: 0,0:01:10.35,0:01:13.17,Default,,0000,0000,0000,,When it's done, execution\Njumps to the next line of code Dialogue: 0,0:01:13.17,0:01:15.66,Default,,0000,0000,0000,,that's indented outside\Nof the conditional. Dialogue: 0,0:01:15.66,0:01:17.61,Default,,0000,0000,0000,,If the condition evaluates to false, Dialogue: 0,0:01:17.61,0:01:19.56,Default,,0000,0000,0000,,the computer skips the\Nrest of the IF branch Dialogue: 0,0:01:19.56,0:01:21.30,Default,,0000,0000,0000,,and jumps to the else branch. Dialogue: 0,0:01:21.30,0:01:24.09,Default,,0000,0000,0000,,Then it executes any lines\Nof code that are indented Dialogue: 0,0:01:24.09,0:01:25.92,Default,,0000,0000,0000,,inside the else branch. Dialogue: 0,0:01:25.92,0:01:28.23,Default,,0000,0000,0000,,When it's done, it just\Ncontinues execution Dialogue: 0,0:01:28.23,0:01:29.73,Default,,0000,0000,0000,,with the next line of code indented Dialogue: 0,0:01:29.73,0:01:31.86,Default,,0000,0000,0000,,outside of the conditional. Dialogue: 0,0:01:31.86,0:01:33.54,Default,,0000,0000,0000,,Note that this execution path Dialogue: 0,0:01:33.54,0:01:35.79,Default,,0000,0000,0000,,with an else branch is\Ndifferent from this, Dialogue: 0,0:01:35.79,0:01:38.07,Default,,0000,0000,0000,,where we just have that\Ninstruction indented outside Dialogue: 0,0:01:38.07,0:01:39.18,Default,,0000,0000,0000,,of the IF statement. Dialogue: 0,0:01:39.18,0:01:41.07,Default,,0000,0000,0000,,Here if the condition evaluates to true, Dialogue: 0,0:01:41.07,0:01:42.54,Default,,0000,0000,0000,,we print mobile layout, Dialogue: 0,0:01:42.54,0:01:44.25,Default,,0000,0000,0000,,then we jump outside of the conditional Dialogue: 0,0:01:44.25,0:01:45.96,Default,,0000,0000,0000,,and print desktop layout. Dialogue: 0,0:01:45.96,0:01:47.73,Default,,0000,0000,0000,,If the condition evaluates to false, Dialogue: 0,0:01:47.73,0:01:50.43,Default,,0000,0000,0000,,we skip the IF branch, jump\Noutside of the conditional Dialogue: 0,0:01:50.43,0:01:52.14,Default,,0000,0000,0000,,and print desktop layout. Dialogue: 0,0:01:52.14,0:01:54.21,Default,,0000,0000,0000,,Because the instruction is not indented Dialogue: 0,0:01:54.21,0:01:57.84,Default,,0000,0000,0000,,it's independent of the\Nconditional`, it always executes. Dialogue: 0,0:01:57.84,0:01:59.73,Default,,0000,0000,0000,,However, if we indent this instruction Dialogue: 0,0:01:59.73,0:02:01.71,Default,,0000,0000,0000,,inside an else branch instead, Dialogue: 0,0:02:01.71,0:02:04.26,Default,,0000,0000,0000,,we only print desktop\Nlayout if the condition Dialogue: 0,0:02:04.26,0:02:05.82,Default,,0000,0000,0000,,evaluates to false. Dialogue: 0,0:02:05.82,0:02:07.80,Default,,0000,0000,0000,,If the condition evaluates to true, Dialogue: 0,0:02:07.80,0:02:10.53,Default,,0000,0000,0000,,we print mobile layout\Nand skip the else branch. Dialogue: 0,0:02:11.61,0:02:14.31,Default,,0000,0000,0000,,What if we have more than two cases Dialogue: 0,0:02:14.31,0:02:16.14,Default,,0000,0000,0000,,like a mobile tablet and a desktop layout? Dialogue: 0,0:02:16.14,0:02:17.64,Default,,0000,0000,0000,,We could try to construct three Dialogue: 0,0:02:17.64,0:02:20.61,Default,,0000,0000,0000,,mutually exclusive conditions\Nor we can take advantage Dialogue: 0,0:02:20.61,0:02:23.40,Default,,0000,0000,0000,,of a shortcut and use the elif branch. Dialogue: 0,0:02:23.40,0:02:25.44,Default,,0000,0000,0000,,The elif or else IF branch Dialogue: 0,0:02:25.44,0:02:28.74,Default,,0000,0000,0000,,allows us to chain multiple\Nconditions together. Dialogue: 0,0:02:28.74,0:02:29.97,Default,,0000,0000,0000,,Starting with the IF branch, Dialogue: 0,0:02:29.97,0:02:32.55,Default,,0000,0000,0000,,the computer evaluates\Neach condition in order Dialogue: 0,0:02:32.55,0:02:34.89,Default,,0000,0000,0000,,until it finds one that evaluates to true, Dialogue: 0,0:02:34.89,0:02:37.53,Default,,0000,0000,0000,,then it chooses that branch to execute. Dialogue: 0,0:02:37.53,0:02:39.36,Default,,0000,0000,0000,,Note that order matters here Dialogue: 0,0:02:39.36,0:02:41.88,Default,,0000,0000,0000,,because the computer will\Nstop checking other branches Dialogue: 0,0:02:41.88,0:02:44.64,Default,,0000,0000,0000,,as soon as it finds one\Nthat evaluates to true. Dialogue: 0,0:02:44.64,0:02:46.89,Default,,0000,0000,0000,,If instead I put this condition first, Dialogue: 0,0:02:46.89,0:02:48.90,Default,,0000,0000,0000,,the IF branch would\Ncapture any screen widths Dialogue: 0,0:02:48.90,0:02:51.15,Default,,0000,0000,0000,,that are smaller than 760. Dialogue: 0,0:02:51.15,0:02:52.95,Default,,0000,0000,0000,,That means that if I have a mobile screen Dialogue: 0,0:02:52.95,0:02:55.89,Default,,0000,0000,0000,,that's say 300 pixels,\Nit will get captured Dialogue: 0,0:02:55.89,0:02:58.74,Default,,0000,0000,0000,,by this first case and\Nprint tablet layout. Dialogue: 0,0:02:58.74,0:03:01.20,Default,,0000,0000,0000,,The computer only ever chooses one branch. Dialogue: 0,0:03:01.20,0:03:02.76,Default,,0000,0000,0000,,It doesn't print mobile layout. Dialogue: 0,0:03:02.76,0:03:05.88,Default,,0000,0000,0000,,Even though that condition\Nwould have evaluated to true. Dialogue: 0,0:03:05.88,0:03:08.25,Default,,0000,0000,0000,,We can attach as many\Nelif branches as we want Dialogue: 0,0:03:08.25,0:03:10.53,Default,,0000,0000,0000,,to any if branch and\Nthen we can optionally Dialogue: 0,0:03:10.53,0:03:12.09,Default,,0000,0000,0000,,add an else branch at the end. Dialogue: 0,0:03:13.11,0:03:14.79,Default,,0000,0000,0000,,So we can see what the computer's doing Dialogue: 0,0:03:14.79,0:03:17.43,Default,,0000,0000,0000,,let's trace each possible execution path. Dialogue: 0,0:03:17.43,0:03:19.20,Default,,0000,0000,0000,,If the first condition evaluates to true, Dialogue: 0,0:03:19.20,0:03:20.82,Default,,0000,0000,0000,,then the computer chooses that branch Dialogue: 0,0:03:20.82,0:03:23.67,Default,,0000,0000,0000,,and it executes any instructions\Nindented inside of it. Dialogue: 0,0:03:23.67,0:03:25.38,Default,,0000,0000,0000,,Then it jumps to the next line of code Dialogue: 0,0:03:25.38,0:03:27.00,Default,,0000,0000,0000,,outside of the conditional. Dialogue: 0,0:03:27.00,0:03:29.40,Default,,0000,0000,0000,,If the first condition evaluates to false, Dialogue: 0,0:03:29.40,0:03:32.01,Default,,0000,0000,0000,,then the computer goes on\Nto check the next condition. Dialogue: 0,0:03:32.01,0:03:34.26,Default,,0000,0000,0000,,If the second condition evaluates to true, Dialogue: 0,0:03:34.26,0:03:35.88,Default,,0000,0000,0000,,then the computer chooses this branch Dialogue: 0,0:03:35.88,0:03:38.76,Default,,0000,0000,0000,,and executes any instructions\Nindented inside of it. Dialogue: 0,0:03:38.76,0:03:40.44,Default,,0000,0000,0000,,Then it jumps to the next line of code Dialogue: 0,0:03:40.44,0:03:42.36,Default,,0000,0000,0000,,outside of the conditional. Dialogue: 0,0:03:42.36,0:03:44.31,Default,,0000,0000,0000,,If the computer checks\Nthe first condition, Dialogue: 0,0:03:44.31,0:03:45.96,Default,,0000,0000,0000,,finds that it evaluates to false, Dialogue: 0,0:03:45.96,0:03:47.40,Default,,0000,0000,0000,,then checks the second condition, Dialogue: 0,0:03:47.40,0:03:48.99,Default,,0000,0000,0000,,also finds that it evaluates to false, Dialogue: 0,0:03:48.99,0:03:51.03,Default,,0000,0000,0000,,then it moves on to the else branch else. Dialogue: 0,0:03:51.03,0:03:52.74,Default,,0000,0000,0000,,Else branches don't have a condition, Dialogue: 0,0:03:52.74,0:03:54.90,Default,,0000,0000,0000,,so if the computer\Nreaches it, it just runs. Dialogue: 0,0:03:54.90,0:03:56.73,Default,,0000,0000,0000,,So we execute the instructions indented Dialogue: 0,0:03:56.73,0:03:58.17,Default,,0000,0000,0000,,inside the else branch Dialogue: 0,0:03:58.17,0:04:00.09,Default,,0000,0000,0000,,and then we jump to the next line of code Dialogue: 0,0:04:00.09,0:04:01.92,Default,,0000,0000,0000,,outside of the conditional. Dialogue: 0,0:04:01.92,0:04:04.74,Default,,0000,0000,0000,,So if you have multiple related\Nconditions in your program, Dialogue: 0,0:04:04.74,0:04:07.80,Default,,0000,0000,0000,,it's generally better to use\Na chain conditional with elif Dialogue: 0,0:04:07.80,0:04:10.29,Default,,0000,0000,0000,,and else branches instead\Nof several independent Dialogue: 0,0:04:10.29,0:04:12.21,Default,,0000,0000,0000,,single branch conditionals. Dialogue: 0,0:04:12.21,0:04:14.52,Default,,0000,0000,0000,,Chain conditionals make\Nprograms easier to read Dialogue: 0,0:04:14.52,0:04:15.78,Default,,0000,0000,0000,,because it makes the relationship Dialogue: 0,0:04:15.78,0:04:17.61,Default,,0000,0000,0000,,between conditions obvious. Dialogue: 0,0:04:17.61,0:04:20.28,Default,,0000,0000,0000,,It reduces bugs because\Nwe as the programmer Dialogue: 0,0:04:20.28,0:04:22.98,Default,,0000,0000,0000,,don't have to worry about\Nmaking sure all our conditions Dialogue: 0,0:04:22.98,0:04:26.52,Default,,0000,0000,0000,,are mutually exclusive and\Ncover all possible cases Dialogue: 0,0:04:26.52,0:04:28.65,Default,,0000,0000,0000,,and it saves the computer some work. Dialogue: 0,0:04:28.65,0:04:29.79,Default,,0000,0000,0000,,With independent conditions Dialogue: 0,0:04:29.79,0:04:31.20,Default,,0000,0000,0000,,that computer doesn't\Nknow they're related, Dialogue: 0,0:04:31.20,0:04:34.44,Default,,0000,0000,0000,,so it'll evaluate every single\None, even if it's impossible Dialogue: 0,0:04:34.44,0:04:36.24,Default,,0000,0000,0000,,for multiple of them to be true. Dialogue: 0,0:04:36.24,0:04:37.23,Default,,0000,0000,0000,,With chain conditionals, Dialogue: 0,0:04:37.23,0:04:38.58,Default,,0000,0000,0000,,we give the computer the hint Dialogue: 0,0:04:38.58,0:04:40.44,Default,,0000,0000,0000,,that these are all mutually exclusive, Dialogue: 0,0:04:40.44,0:04:42.03,Default,,0000,0000,0000,,so it knows it can stop evaluating Dialogue: 0,0:04:42.03,0:04:44.76,Default,,0000,0000,0000,,as soon as it finds a branch\Nthat evaluates to true.