1 00:00:00,240 --> 00:00:04,320 Hello welcome to this tutorial on IF statement 2 00:00:07,490 --> 00:00:09,190 conditional statements. 3 00:00:09,320 --> 00:00:19,160 Give us an ability to check conditions and change the behavior of the program accordingly and if statements 4 00:00:19,250 --> 00:00:27,020 are also known as Control floor statements which help us to run a particular chord. 5 00:00:27,080 --> 00:00:38,980 Only when a certain condition is met this is the syntax for the if statement if it is the key word then 6 00:00:38,980 --> 00:00:43,810 we have a boolean expression and then a set of statements. 7 00:00:43,960 --> 00:00:54,130 The first line of statement that is the if condition is known as if clause and the condition your is 8 00:00:54,130 --> 00:01:03,640 a boolean expression that is evaluated to either true or false and in the next line. 9 00:01:04,000 --> 00:01:14,680 We have a block of statements and notice the colon after the if clause in the first line. 10 00:01:14,710 --> 00:01:23,020 This is very important because it separates the if clause with the body that is the block of statements 11 00:01:23,020 --> 00:01:24,010 here. 12 00:01:24,010 --> 00:01:29,360 Next the line after the colon must be indented. 13 00:01:29,500 --> 00:01:38,910 That means the block of code should be positioned by the same amount to the right of the. 14 00:01:38,950 --> 00:01:50,770 If keyword and Biden recommends to indent each statement in the block by four spaces so beginning from 15 00:01:50,770 --> 00:01:52,360 the if key word. 16 00:01:52,360 --> 00:01:57,510 We need to indent each statement by Force bases. 17 00:01:57,520 --> 00:02:07,120 So you need to leave four spaces and then you can type your code after that. 18 00:02:07,250 --> 00:02:11,780 So all of this code gets executed. 19 00:02:12,050 --> 00:02:21,260 If the if condition is true and these statements followed by the if clause which are not indented that 20 00:02:21,260 --> 00:02:24,420 means the statement here. 21 00:02:24,620 --> 00:02:35,110 For example the non belong to the if block so these statements are not part of the block. 22 00:02:35,470 --> 00:02:44,890 So as a result it will always execute no matter whether the condition in the if clause is true or false. 23 00:02:44,920 --> 00:02:48,130 So let's now look at some examples. 24 00:02:48,130 --> 00:02:49,680 I have the if clause here. 25 00:02:49,720 --> 00:02:55,430 This boolean expression age is less than or equal to fly. 26 00:02:55,900 --> 00:03:03,780 And if this condition is true the print function print the text here. 27 00:03:04,180 --> 00:03:12,670 And as you can see there is only one statement in this if clause going to the next example. 28 00:03:13,090 --> 00:03:22,930 I have two variables and a is assigned to two and then B is assigned to 5 and the boolean expression 29 00:03:22,930 --> 00:03:23,780 your checks. 30 00:03:23,830 --> 00:03:36,300 If it is greater than B Since 2 is not greater than 5 the indented code here is not executed. 31 00:03:36,390 --> 00:03:42,000 The condition is false and this block of code is not executed. 32 00:03:43,780 --> 00:03:54,040 So decontrol goes to the first UN indented statement in the program and this is the statement and the 33 00:03:54,040 --> 00:03:58,780 program flaw goes like this. 34 00:03:58,810 --> 00:04:08,680 So here I assign why you do to the variable each and then using the if condition. 35 00:04:08,910 --> 00:04:17,720 I'm going to check if this particular boolean expression is true or not and there's one statement in 36 00:04:17,720 --> 00:04:29,960 the if clause notice the indentation here I have left some spaces to the right of the if keyword so 37 00:04:29,960 --> 00:04:33,280 that this gets executed. 38 00:04:33,440 --> 00:04:40,480 If the condition is true so now let execute this piece of code. 39 00:04:42,940 --> 00:04:48,590 So this condition is true and the text has been printed. 40 00:04:49,150 --> 00:04:51,360 Let's consider in another example. 41 00:04:52,720 --> 00:05:02,350 So in this example I have assigned devalued tend to debatable each and the boolean expression in the 42 00:05:02,350 --> 00:05:07,160 if clause checks if the variable is less than or equal to 5. 43 00:05:07,190 --> 00:05:16,150 And if this boolean expression is true the print function print this text or another print function 44 00:05:16,240 --> 00:05:25,180 but just not indented which means it does not belong to the if clause and it gets executed no matter 45 00:05:25,180 --> 00:05:29,530 whether the condition in the clause is true or not. 46 00:05:29,590 --> 00:05:37,090 So let's execute this piece of code devalue 10 is not less than equal to 5. 47 00:05:37,090 --> 00:05:40,180 That means the boolean expression used is false. 48 00:05:40,630 --> 00:05:45,390 So this print function is not executed. 49 00:05:45,610 --> 00:05:56,340 The next statement in the program which is not indented is the print function so the program executes 50 00:05:56,430 --> 00:05:59,150 this line of code. 51 00:06:00,770 --> 00:06:11,320 It does now discuss another exam but so I have a variable flag that is assigned to the Boolean value 52 00:06:11,440 --> 00:06:23,130 true and then I have the if statement the if statement checks if the variable flag is true or not. 53 00:06:23,130 --> 00:06:33,750 And if this is true it prints this text we have discussed before that the if statement always checks 54 00:06:33,810 --> 00:06:42,780 if the condition is true only if the condition is true the statement within the if block are executed 55 00:06:43,350 --> 00:06:46,620 or not executed at all. 56 00:06:47,280 --> 00:06:59,220 So comparing variable flag here with the Boolean value true is redundant so we can put this if statement 57 00:06:59,220 --> 00:07:00,530 like this. 58 00:07:00,660 --> 00:07:05,620 If flat and then print. 59 00:07:06,200 --> 00:07:07,520 We'll see. 60 00:07:07,530 --> 00:07:18,810 So in this if statement we have simply parsed the variable flag to the if clause because if always checks 61 00:07:18,870 --> 00:07:20,370 if the condition is true. 62 00:07:20,850 --> 00:07:32,970 So here in this example I have assigned the Boolean value true to the variable flag and in the if condition 63 00:07:33,030 --> 00:07:38,060 I have simply parsed the variable flag. 64 00:07:38,110 --> 00:07:49,510 That's because the if condition gets executed only if this particular condition is true we don't have 65 00:07:49,510 --> 00:07:59,160 to compare the variable flag with the Boolean value true and if this is true the print function prints 66 00:07:59,200 --> 00:08:00,070 the text. 67 00:08:00,280 --> 00:08:03,570 So let's execute this set of. 68 00:08:03,700 --> 00:08:05,500 So this is the output. 69 00:08:05,620 --> 00:08:16,600 Now we have discussed in previous tutorials that in Python the value 0 evaluates to false. 70 00:08:17,260 --> 00:08:29,430 Similarly an empty string evaluates to false and empty list evaluate to false and the integer 1 2 3 71 00:08:29,440 --> 00:08:37,070 for any integer greater than 0 evaluates to true so. 72 00:08:37,260 --> 00:08:47,870 So here to the if a word I have passed an empty string and an empty string and Python evaluates true 73 00:08:47,870 --> 00:08:48,860 false. 74 00:08:48,860 --> 00:08:53,570 So this brain function will not get executed. 75 00:08:53,570 --> 00:08:55,820 So let's execute this in and check. 76 00:08:56,510 --> 00:08:59,440 As you can see this is false. 77 00:08:59,450 --> 00:09:06,530 An empty string evaluates to file and so d if block did not get executed.