0:00:00.210,0:00:02.040 - [Instructor] We can use[br]an IF statement to control 0:00:02.040,0:00:04.710 that a particular block[br]of code only executes 0:00:04.710,0:00:06.960 when the condition evaluates to true. 0:00:06.960,0:00:08.610 But what if we wanna do something else 0:00:08.610,0:00:11.010 only when the condition[br]evaluates to false? 0:00:11.010,0:00:13.800 Well, we can add another[br]IF statement and try 0:00:13.800,0:00:16.170 and construct a condition[br]that's the exact opposite 0:00:16.170,0:00:17.760 of the original condition. 0:00:17.760,0:00:19.080 That's a bit annoying 0:00:19.080,0:00:21.660 and sometimes the opposite[br]condition isn't obvious 0:00:21.660,0:00:23.040 or it's super long. 0:00:23.040,0:00:24.030 To save us the trouble, 0:00:24.030,0:00:26.880 Python instead lets us use an else branch. 0:00:26.880,0:00:30.420 We can stick an else branch[br]onto the end of any IF statement 0:00:30.420,0:00:33.270 and any instructions indented[br]inside the else branch 0:00:33.270,0:00:36.420 only execute if the corresponding[br]IF statements condition 0:00:36.420,0:00:38.130 evaluates to false. 0:00:38.130,0:00:39.300 That makes the IF branch 0:00:39.300,0:00:42.510 and the else branch mutually[br]exclusive based on the answer 0:00:42.510,0:00:43.343 to the condition, 0:00:43.343,0:00:47.040 the computer decides which[br]of the two paths to take. 0:00:47.040,0:00:48.900 The Python syntax for an else branch 0:00:48.900,0:00:50.430 is just the keyword else 0:00:50.430,0:00:51.720 followed by a colon. 0:00:51.720,0:00:54.030 Again, we use indentation[br]to tell the computer 0:00:54.030,0:00:56.640 which lines of code are[br]inside the else branch. 0:00:56.640,0:00:58.830 An else branch must always[br]follow an IF branch. 0:00:58.830,0:01:01.260 Otherwise, what are we[br]taking the opposite of? 0:01:01.260,0:01:03.270 Let's trace that execution path. 0:01:03.270,0:01:05.700 If the condition evaluates[br]to true as normal, 0:01:05.700,0:01:08.100 the computer goes on to[br]execute any instructions 0:01:08.100,0:01:10.350 indented inside that IF branch. 0:01:10.350,0:01:13.170 When it's done, execution[br]jumps to the next line of code 0:01:13.170,0:01:15.660 that's indented outside[br]of the conditional. 0:01:15.660,0:01:17.610 If the condition evaluates to false, 0:01:17.610,0:01:19.560 the computer skips the[br]rest of the IF branch 0:01:19.560,0:01:21.300 and jumps to the else branch. 0:01:21.300,0:01:24.090 Then it executes any lines[br]of code that are indented 0:01:24.090,0:01:25.920 inside the else branch. 0:01:25.920,0:01:28.230 When it's done, it just[br]continues execution 0:01:28.230,0:01:29.730 with the next line of code indented 0:01:29.730,0:01:31.860 outside of the conditional. 0:01:31.860,0:01:33.540 Note that this execution path 0:01:33.540,0:01:35.790 with an else branch is[br]different from this, 0:01:35.790,0:01:38.070 where we just have that[br]instruction indented outside 0:01:38.070,0:01:39.180 of the IF statement. 0:01:39.180,0:01:41.070 Here if the condition evaluates to true, 0:01:41.070,0:01:42.540 we print mobile layout, 0:01:42.540,0:01:44.250 then we jump outside of the conditional 0:01:44.250,0:01:45.960 and print desktop layout. 0:01:45.960,0:01:47.730 If the condition evaluates to false, 0:01:47.730,0:01:50.430 we skip the IF branch, jump[br]outside of the conditional 0:01:50.430,0:01:52.140 and print desktop layout. 0:01:52.140,0:01:54.210 Because the instruction is not indented 0:01:54.210,0:01:57.840 it's independent of the[br]conditional`, it always executes. 0:01:57.840,0:01:59.730 However, if we indent this instruction 0:01:59.730,0:02:01.710 inside an else branch instead, 0:02:01.710,0:02:04.260 we only print desktop[br]layout if the condition 0:02:04.260,0:02:05.820 evaluates to false. 0:02:05.820,0:02:07.800 If the condition evaluates to true, 0:02:07.800,0:02:10.533 we print mobile layout[br]and skip the else branch. 0:02:11.610,0:02:14.309 What if we have more than two cases 0:02:14.309,0:02:16.140 like a mobile tablet and a desktop layout? 0:02:16.140,0:02:17.640 We could try to construct three 0:02:17.640,0:02:20.610 mutually exclusive conditions[br]or we can take advantage 0:02:20.610,0:02:23.400 of a shortcut and use the elif branch. 0:02:23.400,0:02:25.440 The elif or else IF branch 0:02:25.440,0:02:28.740 allows us to chain multiple[br]conditions together. 0:02:28.740,0:02:29.970 Starting with the IF branch, 0:02:29.970,0:02:32.550 the computer evaluates[br]each condition in order 0:02:32.550,0:02:34.890 until it finds one that evaluates to true, 0:02:34.890,0:02:37.530 then it chooses that branch to execute. 0:02:37.530,0:02:39.360 Note that order matters here 0:02:39.360,0:02:41.880 because the computer will[br]stop checking other branches 0:02:41.880,0:02:44.640 as soon as it finds one[br]that evaluates to true. 0:02:44.640,0:02:46.890 If instead I put this condition first, 0:02:46.890,0:02:48.900 the IF branch would[br]capture any screen widths 0:02:48.900,0:02:51.150 that are smaller than 760. 0:02:51.150,0:02:52.950 That means that if I have a mobile screen 0:02:52.950,0:02:55.890 that's say 300 pixels,[br]it will get captured 0:02:55.890,0:02:58.740 by this first case and[br]print tablet layout. 0:02:58.740,0:03:01.200 The computer only ever chooses one branch. 0:03:01.200,0:03:02.760 It doesn't print mobile layout. 0:03:02.760,0:03:05.880 Even though that condition[br]would have evaluated to true. 0:03:05.880,0:03:08.250 We can attach as many[br]elif branches as we want 0:03:08.250,0:03:10.530 to any if branch and[br]then we can optionally 0:03:10.530,0:03:12.093 add an else branch at the end. 0:03:13.110,0:03:14.790 So we can see what the computer's doing 0:03:14.790,0:03:17.430 let's trace each possible execution path. 0:03:17.430,0:03:19.200 If the first condition evaluates to true, 0:03:19.200,0:03:20.820 then the computer chooses that branch 0:03:20.820,0:03:23.670 and it executes any instructions[br]indented inside of it. 0:03:23.670,0:03:25.380 Then it jumps to the next line of code 0:03:25.380,0:03:27.000 outside of the conditional. 0:03:27.000,0:03:29.400 If the first condition evaluates to false, 0:03:29.400,0:03:32.010 then the computer goes on[br]to check the next condition. 0:03:32.010,0:03:34.260 If the second condition evaluates to true, 0:03:34.260,0:03:35.880 then the computer chooses this branch 0:03:35.880,0:03:38.760 and executes any instructions[br]indented inside of it. 0:03:38.760,0:03:40.440 Then it jumps to the next line of code 0:03:40.440,0:03:42.360 outside of the conditional. 0:03:42.360,0:03:44.310 If the computer checks[br]the first condition, 0:03:44.310,0:03:45.960 finds that it evaluates to false, 0:03:45.960,0:03:47.400 then checks the second condition, 0:03:47.400,0:03:48.993 also finds that it evaluates to false, 0:03:48.993,0:03:51.030 then it moves on to the else branch else. 0:03:51.030,0:03:52.740 Else branches don't have a condition, 0:03:52.740,0:03:54.900 so if the computer[br]reaches it, it just runs. 0:03:54.900,0:03:56.730 So we execute the instructions indented 0:03:56.730,0:03:58.170 inside the else branch 0:03:58.170,0:04:00.090 and then we jump to the next line of code 0:04:00.090,0:04:01.920 outside of the conditional. 0:04:01.920,0:04:04.740 So if you have multiple related[br]conditions in your program, 0:04:04.740,0:04:07.800 it's generally better to use[br]a chain conditional with elif 0:04:07.800,0:04:10.290 and else branches instead[br]of several independent 0:04:10.290,0:04:12.210 single branch conditionals. 0:04:12.210,0:04:14.520 Chain conditionals make[br]programs easier to read 0:04:14.520,0:04:15.780 because it makes the relationship 0:04:15.780,0:04:17.610 between conditions obvious. 0:04:17.610,0:04:20.280 It reduces bugs because[br]we as the programmer 0:04:20.280,0:04:22.980 don't have to worry about[br]making sure all our conditions 0:04:22.980,0:04:26.520 are mutually exclusive and[br]cover all possible cases 0:04:26.520,0:04:28.650 and it saves the computer some work. 0:04:28.650,0:04:29.790 With independent conditions 0:04:29.790,0:04:31.200 that computer doesn't[br]know they're related, 0:04:31.200,0:04:34.440 so it'll evaluate every single[br]one, even if it's impossible 0:04:34.440,0:04:36.240 for multiple of them to be true. 0:04:36.240,0:04:37.230 With chain conditionals, 0:04:37.230,0:04:38.580 we give the computer the hint 0:04:38.580,0:04:40.440 that these are all mutually exclusive, 0:04:40.440,0:04:42.030 so it knows it can stop evaluating 0:04:42.030,0:04:44.763 as soon as it finds a branch[br]that evaluates to true.