1 00:00:00,460 --> 00:00:06,590 In this lesson you're going to talk about one of the most fundamental aspects of any programming language 2 00:00:06,650 --> 00:00:14,930 or an algorithm and that is a conditional statement or what you call it if else statement which is some 3 00:00:14,930 --> 00:00:17,180 sort of a control flow. 4 00:00:17,180 --> 00:00:25,340 What it does for us is it helps us compare two or more objects of values or variables against each other 5 00:00:25,750 --> 00:00:31,130 and depending on the result of the comparison we could do different things. 6 00:00:31,310 --> 00:00:38,330 So let's go ahead and see this in practice so we get a better understanding of what it means as a concept 7 00:00:38,610 --> 00:00:40,970 and then jump back into X code. 8 00:00:40,970 --> 00:00:49,770 I'm going to close this project Stop this time and I say give me your command line tool. 9 00:00:49,940 --> 00:00:54,340 And this one is called if else project. 10 00:00:54,650 --> 00:01:02,750 So I might if else project I'm going to go into my main as usual when I get rid of all the comments 11 00:01:02,870 --> 00:01:05,900 and then reformat everything nice and tidy. 12 00:01:06,320 --> 00:01:10,850 And here is how I'm going to write my first if else statement. 13 00:01:10,850 --> 00:01:12,800 Imagine a situation like this. 14 00:01:12,800 --> 00:01:23,270 Imagine I have an integer called A which is 5 and I have another integer called B which is 6. 15 00:01:23,350 --> 00:01:38,400 Now what they could do is I would say if A is larger than B then do this and this large say A is large 16 00:01:38,400 --> 00:01:40,380 as simple as that. 17 00:01:40,380 --> 00:01:46,440 Now we all know if I run this application now it's obviously nothing is going to happen simply because 18 00:01:46,830 --> 00:01:53,320 in this case 5 is not bigger than 6 5 is actually smaller than 6. 19 00:01:53,340 --> 00:01:57,540 So what if I wanted to have the are there a statement. 20 00:01:57,530 --> 00:02:00,070 What if this wasn't true. 21 00:02:00,240 --> 00:02:14,810 So I'm going to go ahead and say let's do this and this long b is larger and then rerun this code and 22 00:02:14,810 --> 00:02:19,880 now it says B is larger B is bigger. 23 00:02:20,030 --> 00:02:26,580 Now that we saw it once in practice let's better understand what it does essentially. 24 00:02:26,600 --> 00:02:36,980 And if the situation depends on a condition which is here within this is that is either true or false. 25 00:02:37,190 --> 00:02:44,210 So the condition that we feed into that if a statement is either true in which case the if part will 26 00:02:44,210 --> 00:02:50,090 happen or it is false that the else or the L says will happen. 27 00:02:50,290 --> 00:02:57,710 Talk about the Elsa's soon how we could have more than one else but let's see a different kind of practice. 28 00:02:57,710 --> 00:03:01,080 Let's say I had to remove all of these. 29 00:03:01,380 --> 00:03:13,530 So I had Boullion my Boullion was meant to walk are nuts for that or is fetched. 30 00:03:13,620 --> 00:03:15,140 So that is true. 31 00:03:15,140 --> 00:03:21,270 Obviously this is not something that I would make a static kill like this we would make this depend 32 00:03:21,370 --> 00:03:23,360 on some other arised you. 33 00:03:23,390 --> 00:03:29,770 And then based on that they would decide whether or network data is fetched or it is still we're still 34 00:03:29,780 --> 00:03:31,190 fixing their ideas. 35 00:03:31,340 --> 00:03:36,440 But based on that they could say if that is a fetch. 36 00:03:36,440 --> 00:03:48,320 So their value is true or false if that's the case say and Aslaug we got the data and then if it is 37 00:03:48,320 --> 00:03:58,310 else in the house you could for instance say and it's like still reading for instance or something to 38 00:03:58,310 --> 00:03:59,130 that effect. 39 00:03:59,390 --> 00:04:08,330 So again we have an if else a statement that works based on a boolean result of a conditional. 40 00:04:08,480 --> 00:04:12,980 So the format of any false statement is essentially like this. 41 00:04:13,180 --> 00:04:14,300 I have that. 42 00:04:14,400 --> 00:04:34,600 So I have if condition and then if in case of true and then else in chase all from Faux now a little 43 00:04:34,600 --> 00:04:41,370 bit earlier I said about the fact that there might be more than one else there might be Ellis says happening. 44 00:04:41,440 --> 00:04:45,620 Let's see that once again and let's see how that could be the case. 45 00:04:45,680 --> 00:04:56,890 And will writes My original quote in a five city is six and I'm going to say if a is larger than B then 46 00:04:56,950 --> 00:05:11,340 obviously you say and this long a is bigger and this is what happens in the else in the else a is either 47 00:05:11,430 --> 00:05:17,770 a smaller than B because that's not true or 8 is actually equal to be. 48 00:05:17,940 --> 00:05:29,340 So let's see both of those in the else I'm going to say then if a is smaller than B then say and Aslaug 49 00:05:29,850 --> 00:05:34,050 and this time say a is a smaller. 50 00:05:34,050 --> 00:05:35,540 Simple as that. 51 00:05:35,720 --> 00:05:37,520 And there isn't else. 52 00:05:37,740 --> 00:05:41,710 If not neither of these two situations are correct. 53 00:05:41,850 --> 00:05:43,880 A is not larger than. 54 00:05:43,890 --> 00:05:53,220 So we ended up in the US in the ass 8 is another smaller than B then it means and it's like a is equal 55 00:05:53,220 --> 00:05:56,720 to B and we can actually test. 56 00:05:56,730 --> 00:06:03,120 Let me just get myself a little better for my thing so I can show everything that's wrong this could 57 00:06:03,240 --> 00:06:09,850 of course right now that you say is a smaller but if they were both six for instance I read around this 58 00:06:09,850 --> 00:06:15,330 chord is going to say a is equal to a B because it is flawed. 59 00:06:15,360 --> 00:06:16,080 It didn't work. 60 00:06:16,080 --> 00:06:18,510 It went to the else within the else. 61 00:06:18,510 --> 00:06:21,030 It says Well is it a smaller. 62 00:06:21,030 --> 00:06:24,810 That wasn't the case so it ended up in this one. 63 00:06:24,810 --> 00:06:27,730 Let's rewrite this a little bit differently. 64 00:06:27,780 --> 00:06:38,970 So I'm going to say if a is larger than B do that else if a is a smaller than B then I don't need to 65 00:06:38,970 --> 00:06:40,020 have this mess. 66 00:06:40,020 --> 00:06:41,920 That difference is statesman's. 67 00:06:42,150 --> 00:06:49,430 I'm going to remove that get through the fall of it kind of makes sense now. 68 00:06:49,680 --> 00:06:53,960 So I'm going to say if a is larger than B say is bigger. 69 00:06:54,030 --> 00:06:55,300 That makes sense. 70 00:06:56,340 --> 00:07:01,600 Else if a is a smaller than B say a is a smaller. 71 00:07:01,830 --> 00:07:06,170 What is the tared situation is neither larger nor a smaller. 72 00:07:06,330 --> 00:07:14,730 So else if neither of the two were correct say they are equal. 73 00:07:15,060 --> 00:07:16,160 That's about down. 74 00:07:16,350 --> 00:07:23,880 So you could either nest your false statements or you could use them in an else if situation. 75 00:07:23,880 --> 00:07:31,200 Keep in mind that the else if situation is best used when your partners are the same like when you're 76 00:07:31,200 --> 00:07:39,360 comparing a against B but you're not comparing a against B and C in that situation is much much better 77 00:07:39,360 --> 00:07:45,800 if you actually miss your if statements just like what we did a few minutes ago. 78 00:07:46,050 --> 00:07:55,800 Now in saying that let's say Now you want to expand your if statement into covering more than just the 79 00:07:55,800 --> 00:07:58,660 sign of being larger or smaller. 80 00:07:58,680 --> 00:08:02,420 What if you want to know if it is larger or equal. 81 00:08:02,610 --> 00:08:04,560 So I'm going to rewrite this. 82 00:08:04,680 --> 00:08:19,110 Say if a is larger or equal to B say and Aslaug larger or equal and obviously there is no to else there's 83 00:08:19,110 --> 00:08:22,020 only one else in here and that is. 84 00:08:22,380 --> 00:08:31,410 And this law is smaller so because it is neither larger nor equal it will be a smaller and larger or 85 00:08:31,430 --> 00:08:32,630 each one. 86 00:08:32,730 --> 00:08:35,720 What if I wanted to test if they're exactly equal. 87 00:08:35,730 --> 00:08:37,390 And this is an important one. 88 00:08:37,470 --> 00:08:44,310 So I'm going to say two equal signs and that is the sign of equality. 89 00:08:44,430 --> 00:08:53,700 If you remember earlier on we talked about how a single equal sign is a sign of assigning values assign 90 00:08:53,970 --> 00:09:03,210 values and two equal signs is a sign of comparing equality. 91 00:09:03,210 --> 00:09:12,640 So if I say if a is equal to be two equal sides it means I'm asking are they equal against each other. 92 00:09:12,900 --> 00:09:20,070 If that's true is going to print equal and then indeed as I could again write the same thing say well 93 00:09:20,130 --> 00:09:31,860 if a is larger than B do something and say Steve and Alice the last one do something else. 94 00:09:31,860 --> 00:09:47,640 So so far because these guys one larger is smaller than larger or smaller or larger or equal smaller 95 00:09:47,700 --> 00:09:49,010 or equal. 96 00:09:49,020 --> 00:09:54,710 There is another sign which is the not equal and that is how we use it. 97 00:09:54,720 --> 00:09:59,030 Are you going to say going to get rid of all of these. 98 00:09:59,190 --> 00:10:06,230 I'm going to say if a is not equal to be so if this is not happening. 99 00:10:06,240 --> 00:10:13,000 So that sign you could use either alone which I would show you or I could use it with an equal sign. 100 00:10:13,140 --> 00:10:24,270 So we could either say if a is not equal to B or we could say if this condition which is being true 101 00:10:24,600 --> 00:10:26,580 is incorrect. 102 00:10:26,880 --> 00:10:29,040 So you could use either of the two ways. 103 00:10:29,040 --> 00:10:33,830 But for now we're going to use the easier one which is like this. 104 00:10:33,930 --> 00:10:41,700 So I'm going to say if a is not equal to B then say not equal to B. 105 00:10:42,030 --> 00:10:44,720 And that's another one. 106 00:10:45,020 --> 00:10:50,680 Finally we should see how we can compare multiple different conditions against each other. 107 00:10:50,710 --> 00:11:00,110 So I have another one and see and that is seven and I want to perceive a and b are equal but if B is 108 00:11:00,110 --> 00:11:01,530 larger than c. 109 00:11:01,580 --> 00:11:11,550 So what you're looking for A and B called for it to be larger than c. 110 00:11:11,570 --> 00:11:13,290 So let's see how we could do that. 111 00:11:13,520 --> 00:11:21,340 I could go ahead and say just like what we have done before if a is equal to B. 112 00:11:21,500 --> 00:11:31,160 That's the condition I'm looking for in here B is larger than c then say and Aslaug. 113 00:11:31,540 --> 00:11:34,340 Yea this is the one that we were looking for. 114 00:11:34,670 --> 00:11:36,310 So that obviously works. 115 00:11:36,320 --> 00:11:39,020 But again we are nesting if statements. 116 00:11:39,020 --> 00:11:41,120 Maybe there is an easier way for it. 117 00:11:41,120 --> 00:11:42,770 And it absolutely is. 118 00:11:43,010 --> 00:11:44,060 And that is called. 119 00:11:44,170 --> 00:11:57,440 And so I'm going to say if a is equal to B and to signs B is larger than c it means if both of these 120 00:11:57,440 --> 00:11:59,290 conditions are true. 121 00:11:59,480 --> 00:12:04,950 So I'm gonna copy that and I'm going to put it there. 122 00:12:05,050 --> 00:12:09,210 So it's these two pieces of course they're exactly identical. 123 00:12:09,220 --> 00:12:14,930 This one means if a is equal to B and also B is larger than c. 124 00:12:15,100 --> 00:12:20,790 Now another thing that from time to time comes up is two conditions that we want. 125 00:12:20,800 --> 00:12:28,510 One of them to be true and then I had this seen here so far we saw and as are now in this next example 126 00:12:28,570 --> 00:12:30,590 I want to see this thing happen. 127 00:12:34,290 --> 00:12:41,940 A and B are equal or B larger than c. 128 00:12:42,120 --> 00:12:46,610 So I'm going to say if a equals B. 129 00:12:47,340 --> 00:12:56,350 If this is the right thing or is B is larger than c so this is the sign of. 130 00:12:56,460 --> 00:13:02,880 Or it means if this is true or this is true I'm going to be OK. 131 00:13:03,060 --> 00:13:07,070 Either of these three if it is correct then this part is going to happen. 132 00:13:07,260 --> 00:13:12,160 If none of them is correct then obviously the else part is going to happen. 133 00:13:12,300 --> 00:13:19,350 Sometimes you care about the spot sometimes you don't care about the else you just want the if part 134 00:13:19,350 --> 00:13:22,690 of your heart condition to be to happen. 135 00:13:22,740 --> 00:13:24,950 So I'm going to add this one in here as well. 136 00:13:25,050 --> 00:13:28,560 So that's a sign of or. 137 00:13:29,110 --> 00:13:36,900 And one last thing before I close this lesson and that is some values they can be compared against each 138 00:13:36,900 --> 00:13:44,890 other such as six again sevens against seven their boss or numeric models they are both integer values. 139 00:13:45,030 --> 00:13:50,480 If too hot to floats if you hada the two billion nonuse you could compare them. 140 00:13:50,520 --> 00:13:55,750 But for instance if you have two strings you can't just compare them against each other. 141 00:13:55,770 --> 00:13:58,070 Let's see that in practice. 142 00:13:58,170 --> 00:14:08,650 So if I have one and there's this thing called a string of one which says hello and then I had another 143 00:14:08,670 --> 00:14:12,030 string that is called the string. 144 00:14:12,330 --> 00:14:15,030 And this one is a random set of characters. 145 00:14:15,030 --> 00:14:19,970 Let's say it's a or b or a unique ID for something. 146 00:14:20,010 --> 00:14:27,900 Now I couldn't just going here and say You first two and one is equal to a string two simply because 147 00:14:28,290 --> 00:14:31,870 there is no such operator between the strings. 148 00:14:31,870 --> 00:14:35,600 The strings cannot be compared against each other like this. 149 00:14:35,610 --> 00:14:38,280 There result of this will always fail. 150 00:14:38,280 --> 00:14:44,580 So for situations like that there is a string or an array or a dictionary or things that you will see 151 00:14:44,580 --> 00:14:44,880 down. 152 00:14:44,880 --> 00:14:45,560 That's right. 153 00:14:45,720 --> 00:14:53,520 You actually have to use the builtin equality features of these de-friend you know rivals for instance 154 00:14:53,550 --> 00:15:00,440 in year I would say if a string of it doesn't matter which one comes first. 155 00:15:00,450 --> 00:15:07,670 But if casting one is equal to a string string two. 156 00:15:08,230 --> 00:15:15,590 So it's saying if this thing is equal to that the string and then obviously if it was true this part 157 00:15:15,640 --> 00:15:17,730 will happen in the else. 158 00:15:17,740 --> 00:15:21,220 The other part of it will happen obviously in this case. 159 00:15:21,220 --> 00:15:28,530 The other part is happening now that we are here I should quickly mention something else as well. 160 00:15:28,720 --> 00:15:30,530 Let me rewrite this. 161 00:15:30,620 --> 00:15:32,030 That's not the. 162 00:15:32,030 --> 00:15:38,430 The false statements or conditionals it's about the use of methods things such as this one. 163 00:15:38,560 --> 00:15:42,370 We have a lesson about this coming up in this very section. 164 00:15:42,550 --> 00:15:46,040 But I thought quickly you know I'll tell you about that. 165 00:15:46,270 --> 00:15:55,000 So what's going on here say is string to 1 and you see what I'm typing and this menu pops which is the 166 00:15:55,000 --> 00:15:56,660 auto completion menu. 167 00:15:56,830 --> 00:16:05,980 It tells me if it is of the type and it's a string so string one is equal to a string and you see again 168 00:16:06,010 --> 00:16:11,660 when the auto completion shows up and I'm typing that is equal to a string. 169 00:16:11,710 --> 00:16:15,530 It tells me you have to give me another string. 170 00:16:15,580 --> 00:16:18,850 So Carl I'm not on the string. 171 00:16:18,880 --> 00:16:22,920 So you have two feet and not interesting to me that is not all. 172 00:16:23,050 --> 00:16:26,430 It does have a certain value inside it. 173 00:16:26,880 --> 00:16:33,940 And on the left side of it it says I will give you back a Boolean I will give you a blank either true 174 00:16:34,000 --> 00:16:35,340 or false. 175 00:16:35,360 --> 00:16:37,300 So is equal to a string. 176 00:16:37,300 --> 00:16:44,760 Obviously here we have to feed the string get to the reason I brought this up is because whenever you're 177 00:16:44,770 --> 00:16:51,540 using any of these methods you have to keep an eye on what you have to feed into them. 178 00:16:51,630 --> 00:17:01,530 And also what you get back from them for instance if I had this mutable string this card it's my mute 179 00:17:02,220 --> 00:17:04,260 that is just nothing for an hour. 180 00:17:04,510 --> 00:17:06,250 And then I had a minute. 181 00:17:06,250 --> 00:17:09,160 It says it is and not over the string. 182 00:17:09,160 --> 00:17:10,980 Append the string. 183 00:17:11,050 --> 00:17:18,640 It says Well if you want to use the append the string please feed me with this string and I will not 184 00:17:18,640 --> 00:17:22,920 give you back anything because it doesn't return anything. 185 00:17:22,930 --> 00:17:26,610 It just does something on my mutable thing. 186 00:17:26,690 --> 00:17:35,230 I didn't see this much further in the future and that of text for instance we'll talk further about 187 00:17:35,230 --> 00:17:39,130 formatting different methods and using methods. 188 00:17:39,130 --> 00:17:46,260 But for now just so that you get some sort of familiarity whenever you're using any of these methods. 189 00:17:46,450 --> 00:17:53,310 Make sure that you read these values lot when you're using this tell yourself oh I have to feed it it's 190 00:17:53,410 --> 00:17:57,770 a string and tell yourself oh it's going to return a boolean. 191 00:17:57,820 --> 00:18:04,580 It says returns a boolean value that indicates whether this thing is equal to the receiver using a literal 192 00:18:04,600 --> 00:18:06,160 Unicode based comparison. 193 00:18:06,160 --> 00:18:10,350 And I'm going to say what if this one is equal to a string to. 194 00:18:10,510 --> 00:18:19,090 Obviously if you said and if statement is always based on a true or false and then based on that you 195 00:18:19,110 --> 00:18:21,330 can decide what's going to happen. 196 00:18:21,790 --> 00:18:26,250 So that's about that about using IF statements. 197 00:18:26,290 --> 00:18:34,840 Once again really quick to reach out and if statements are conditionals based on true or false. 198 00:18:34,960 --> 00:18:42,190 And they always are a part of the control flow and we will see more of the contraflow such as loops 199 00:18:42,190 --> 00:18:44,640 on a solve in the future lessons. 200 00:18:44,650 --> 00:18:48,010 So let's keep it up to here and we'll move on to our next.