1 00:00:00,510 --> 00:00:03,020 Hello and welcome to this video. 2 00:00:03,060 --> 00:00:11,590 In this video I'm going to introduce you to another type of conditional statement caught else statements. 3 00:00:11,640 --> 00:00:22,020 What are else statements l statements are conditional statements that only execute certain block of 4 00:00:22,020 --> 00:00:27,750 code when a condition is false. 5 00:00:27,930 --> 00:00:38,160 So it is different from the if statement and the ill if statement those condition checks for conditions 6 00:00:38,180 --> 00:00:39,890 that are true. 7 00:00:40,170 --> 00:00:49,020 While the L statements will check for conditions that are not true and then execute the block of code 8 00:00:49,470 --> 00:00:51,800 that it has. 9 00:00:51,850 --> 00:00:55,630 So this is what a basic syntax looks like. 10 00:00:55,630 --> 00:00:58,390 For an else statement. 11 00:00:58,390 --> 00:01:06,220 So you start off with the if statement which is the first conditional statement to check if a condition 12 00:01:06,220 --> 00:01:07,480 is true. 13 00:01:07,480 --> 00:01:15,520 You then go into the L if conditional statement which checks if another condition is true. 14 00:01:15,580 --> 00:01:27,160 Now when the condition in the F and the L if are not true It then goes to the else block and execute 15 00:01:27,460 --> 00:01:29,890 the code in the X block. 16 00:01:30,250 --> 00:01:39,250 So remember with the F and the L L if this block of code will only activate or run if the conditions 17 00:01:39,250 --> 00:01:40,210 are true. 18 00:01:40,210 --> 00:01:47,890 Now if those conditions are not true it then jumps to the code inside this ls block and execute the 19 00:01:47,890 --> 00:01:48,810 code. 20 00:01:48,910 --> 00:01:57,970 If the code in the if and the else fails I'm going to illustrate how the else statement works and when 21 00:01:57,990 --> 00:02:06,560 to create some variables so create a variable a and I'll give you the value of 7 or create another variable 22 00:02:06,560 --> 00:02:17,640 B and I'll give it a value of 8 or create or more call it C and give it a value of 9. 23 00:02:18,860 --> 00:02:25,720 So before you can implement an LS block he must have an F block in place. 24 00:02:25,970 --> 00:02:35,420 So I'll start with an F so I'll see if the variable a is greater than the variable B. 25 00:02:35,530 --> 00:02:41,190 Now add my colon and press enter and that gives me the indentation. 26 00:02:41,190 --> 00:02:48,990 So now are right the body of the code that will execute if the condition is true. 27 00:02:48,990 --> 00:02:57,750 So I'm going to reference the variable a symbol and say a comma and then I'll add a string and the string 28 00:02:57,780 --> 00:03:11,310 is basically going to say if a is true that condition the Turks will say a a smaller than our the comma 29 00:03:11,370 --> 00:03:14,640 to escape thus shrink I'll say B. 30 00:03:14,640 --> 00:03:20,640 So now this condition here it's my first condition. 31 00:03:20,690 --> 00:03:27,590 Don't forget the condition in the F block will always test for a true condition. 32 00:03:27,590 --> 00:03:39,560 Next I'm going to add an L if block some say l if if the variable B is greater than or equal s to the 33 00:03:39,560 --> 00:03:41,790 variable c. 34 00:03:41,900 --> 00:03:50,590 Add my call on our tab the tab gives me indentation when you anytime you have a call on the. 35 00:03:50,640 --> 00:04:00,050 In Python that you are writing a new line of code to be indented against the block of code so you know 36 00:04:00,050 --> 00:04:04,730 that each block of code belongs to a particular condition. 37 00:04:04,730 --> 00:04:11,960 So now I'm going to add a print statement so say print and I'm just going to add something that will 38 00:04:11,960 --> 00:04:19,960 print I'll say B which is the value of the variable B and inside that I will add a string. 39 00:04:20,330 --> 00:04:35,940 O say B is smaller than and then escape that string to a comma and add the variable C so this condition 40 00:04:36,000 --> 00:04:37,260 in my if. 41 00:04:37,280 --> 00:04:45,720 BLOCK And in my else if block both have to evaluate to true for the code to execute. 42 00:04:45,750 --> 00:04:53,610 So this code here on line 6 is the F block code that will run if the condition set in line 5 is true 43 00:04:54,510 --> 00:05:00,540 the code block on Line 9 is what will execute if the conditions set online. 44 00:05:00,570 --> 00:05:01,630 It is true. 45 00:05:01,920 --> 00:05:08,370 So if those both conditions are not true this is where the else statement comes in. 46 00:05:08,440 --> 00:05:14,220 Then we want the code inside the else block to execute. 47 00:05:14,670 --> 00:05:24,110 So you are the colon and I just tap and I just do a simple print statement inside that print statement. 48 00:05:24,120 --> 00:05:39,060 I would say C which is a variable C and I'll add a text that will say is larger than I'm going to escape 49 00:05:39,210 --> 00:05:53,430 that string with a comma larger and b not another comma add another string and do a comma to escape 50 00:05:53,430 --> 00:05:56,800 that string and then I'll add an A. 51 00:05:56,850 --> 00:06:07,530 So that is my code completes when the code executes it will check for all this condition this first 52 00:06:07,530 --> 00:06:16,530 condition here is going to evaluate to be false because a which is seven is not greater than B which 53 00:06:16,530 --> 00:06:17,330 is eight. 54 00:06:17,370 --> 00:06:23,240 So this block will not execute so this block here is also false. 55 00:06:23,370 --> 00:06:30,950 B is not greater than or equal to C because B is 8 and c is 9. 56 00:06:30,950 --> 00:06:36,540 So this block of code again will not run cause the condition is false. 57 00:06:36,690 --> 00:06:43,920 We then jump onto this block of code which is the else block this block of code will execute. 58 00:06:44,010 --> 00:06:51,540 There is no condition set up just said if these two conditions are not met I just want this code to 59 00:06:51,540 --> 00:06:56,460 run the code on line 12 is what will execute. 60 00:06:56,460 --> 00:07:00,940 So let me play that so that you can see the result. 61 00:07:00,960 --> 00:07:11,790 So if I press the play button you can see here is printing out this code on line 12 which says 9 is 62 00:07:11,790 --> 00:07:14,260 larger than eight and seven. 63 00:07:14,340 --> 00:07:27,530 This C is the 9 the b is 8 and the A is the 7 so you can see only that code executed because the other 64 00:07:27,530 --> 00:07:29,990 conditions have not been met. 65 00:07:30,050 --> 00:07:37,280 So you only have one block of code execute anyone that matches the condition that block executes. 66 00:07:37,400 --> 00:07:41,380 If for example this condition was true the code will stop there. 67 00:07:41,390 --> 00:07:48,890 The other to this to block will not run at all if this block was true it will stop here and this block 68 00:07:48,890 --> 00:07:50,330 here will not run. 69 00:07:50,360 --> 00:07:58,340 So the else statement is an alternative statement that will run when the conditions you're testing for 70 00:07:58,970 --> 00:08:00,350 is false. 71 00:08:00,640 --> 00:08:02,210 So that's it for this video. 72 00:08:02,210 --> 00:08:03,590 Thank you for watching. 73 00:08:03,590 --> 00:08:04,340 Bye for now.