1 00:00:01,300 --> 00:00:06,410 In this lesson we will be looking at conditions statements in C sharp. 2 00:00:06,700 --> 00:00:09,970 By now I'm sure we all have the hang of creating new projects. 3 00:00:10,010 --> 00:00:16,360 So I've already created a new project and I'm calling this one condition statements and I already cleaned 4 00:00:16,390 --> 00:00:21,010 out the main function to put in some comment and so you can pause the video here. 5 00:00:21,010 --> 00:00:24,730 Go ahead and create your new project inside your solution. 6 00:00:24,730 --> 00:00:28,140 And once again it is called condition statements. 7 00:00:28,140 --> 00:00:28,510 No. 8 00:00:28,540 --> 00:00:32,260 A little prelude before I get into what condition statements really mean. 9 00:00:32,380 --> 00:00:37,000 I want us to examine something about programming in general. 10 00:00:37,060 --> 00:00:38,460 Most if not all. 11 00:00:38,500 --> 00:00:42,690 Well every programming language has three control structures. 12 00:00:42,760 --> 00:00:51,100 They are sequence decisions or selection and repetition sequence basically refers to the fact that the 13 00:00:51,100 --> 00:00:56,410 program is going to execute the commands in the exact order that you placed them. 14 00:00:56,410 --> 00:01:03,430 So in previous examples are we printed a prompt and then we waited for input and then we prompted again 15 00:01:03,730 --> 00:01:06,870 notice that it went in the exact sequence that we wrote. 16 00:01:06,940 --> 00:01:08,370 So that's the sequence. 17 00:01:08,410 --> 00:01:14,350 However when we get to our roadblock and we need to maybe make a decision to see if something meets 18 00:01:14,350 --> 00:01:20,550 a certain criteria then take this action or take on other action then that is another control structure 19 00:01:20,560 --> 00:01:22,930 called selection or decisions. 20 00:01:22,930 --> 00:01:27,980 Knowing this lesson we'll be exploring two condition statements that help us to make selections. 21 00:01:28,000 --> 00:01:34,180 They are if statements and switch statements and then we're going to look at ternary statements as a 22 00:01:34,180 --> 00:01:35,370 bonus. 23 00:01:35,440 --> 00:01:41,830 Let's get started looking at if statements are typical reason what we would need to use and if statement 24 00:01:41,830 --> 00:01:46,570 is to determine something or some action based on some condition. 25 00:01:46,570 --> 00:01:54,910 So let's just say that I wanted to bring something to the screen if something was the way that I think 26 00:01:54,910 --> 00:01:56,290 it may or may not be right. 27 00:01:56,620 --> 00:02:03,400 So let's just say I wanted to see if and when to keep it very simple and I'm going to say something 28 00:02:03,400 --> 00:02:06,550 like If tool is less than 3. 29 00:02:06,830 --> 00:02:10,650 So notice this is a new operator we did arithmetic operators earlier. 30 00:02:10,750 --> 00:02:18,490 We'll have the comparison operators where we're comparing is to greater than or less than this is less 31 00:02:18,490 --> 00:02:19,760 than three. 32 00:02:19,780 --> 00:02:23,430 All right so tool is less than three if two is less than three. 33 00:02:23,500 --> 00:02:25,160 Then I want to do something. 34 00:02:25,270 --> 00:02:29,620 Please note the syntax behind this statement. 35 00:02:29,620 --> 00:02:38,170 The key word is if then I open parenthesis then I place the condition inside of the open parenthesis 36 00:02:38,200 --> 00:02:38,890 and then I close. 37 00:02:38,890 --> 00:02:45,920 So inside of the parentheses I should have a condition is something maybe equivalent to something. 38 00:02:45,970 --> 00:02:50,710 Is it less than is it greater than there are so many ways we can compare things. 39 00:02:50,800 --> 00:02:54,360 But I'm just checking if 2 is less than 3. 40 00:02:54,430 --> 00:02:59,860 Then I want to print something to the screen so I can see a console dot right. 41 00:02:59,890 --> 00:03:07,900 Line and then went to say yes it is all right. 42 00:03:08,090 --> 00:03:13,310 So then I can see if 2 is less than 3 then print. 43 00:03:13,340 --> 00:03:14,810 Yes it is. 44 00:03:14,810 --> 00:03:19,520 What if there was another condition or if I didn't if that wasn't the condition then I wanted to take 45 00:03:19,520 --> 00:03:20,270 another action. 46 00:03:20,270 --> 00:03:22,400 So if it is then print print. 47 00:03:22,430 --> 00:03:23,720 Yes it is. 48 00:03:23,720 --> 00:03:25,690 And then I can see else. 49 00:03:25,700 --> 00:03:28,460 And then after this I need my current braces again. 50 00:03:28,460 --> 00:03:33,170 And then I want to see what what would I do if this condition is not met. 51 00:03:33,200 --> 00:03:40,130 Someone to take this console that line and duplicate it just to expedite this and I'm going to say no 52 00:03:40,280 --> 00:03:42,300 it is not. 53 00:03:42,320 --> 00:03:48,090 So once again I am seeing if two is less than three then print. 54 00:03:48,110 --> 00:03:49,190 Yes it is. 55 00:03:49,190 --> 00:03:50,120 Else print. 56 00:03:50,120 --> 00:03:51,040 No it's not. 57 00:03:51,110 --> 00:03:57,620 Now you notice that this line kind of looks a bit green dot are kind of dull and Visual Studio is basically 58 00:03:57,620 --> 00:04:02,810 just telling me that based on the condition that I specified here I would never ever get to the OS which 59 00:04:02,810 --> 00:04:08,210 is true because every time when winter on that unless I change these values to will always be less than 60 00:04:08,210 --> 00:04:08,450 three. 61 00:04:08,450 --> 00:04:10,900 So yes it is will always get printed. 62 00:04:11,030 --> 00:04:14,960 The Ls would never get printed but I'm still going to leave the L's because then I'm going to show you 63 00:04:14,960 --> 00:04:20,740 different conditions that can go in here and hold the code would be executed. 64 00:04:20,900 --> 00:04:23,530 So let's execute this and see the end result. 65 00:04:23,540 --> 00:04:28,800 So once again it's going to run in the sequence and it's going to see if 2 is less than 3. 66 00:04:28,820 --> 00:04:29,810 What should I do. 67 00:04:30,680 --> 00:04:32,970 So let's check that out. 68 00:04:33,290 --> 00:04:36,920 So remember that you can change your starter project using this dropdown. 69 00:04:37,010 --> 00:04:43,130 Let's just make sure that it's on condition statements and go ahead and click Start and the end result 70 00:04:43,130 --> 00:04:44,000 is that it prints. 71 00:04:44,030 --> 00:04:44,860 Yes it is. 72 00:04:44,930 --> 00:04:48,020 And that's because the condition was true. 73 00:04:48,020 --> 00:04:48,650 No. 74 00:04:48,710 --> 00:04:58,230 If I change these values to something that I want to say no it's not then I can say is 10 less than 75 00:04:58,710 --> 00:05:02,240 maybe three are. 76 00:05:02,280 --> 00:05:03,040 I'll leave the three. 77 00:05:03,060 --> 00:05:06,760 So if 10 is less than three then we want to print. 78 00:05:06,790 --> 00:05:07,720 Yes it is. 79 00:05:07,720 --> 00:05:08,830 Else print. 80 00:05:08,830 --> 00:05:09,640 No it's not. 81 00:05:09,880 --> 00:05:10,390 No. 82 00:05:10,390 --> 00:05:17,260 Just looking at it we know that 10 is not less than 3 so we expect that this condition will not evaluate 83 00:05:17,260 --> 00:05:23,330 to True those it won't do what is in the first block but it will do what's in the else block. 84 00:05:23,380 --> 00:05:28,490 So else is basically the force for any condition that is outlined instead of the if. 85 00:05:28,510 --> 00:05:29,460 So let's try that again. 86 00:05:29,470 --> 00:05:35,620 I want to run the program and the expectation is that it will say no it's not because 10 is not less 87 00:05:35,680 --> 00:05:37,960 than 3 and there we go. 88 00:05:37,960 --> 00:05:40,270 No it's not all right. 89 00:05:40,300 --> 00:05:47,530 So that's a very very simple example let's put up a bit more meat on it so I'm going to find some variables 90 00:05:47,890 --> 00:05:52,420 I'm going to define number one and number two also int num 1. 91 00:05:52,500 --> 00:05:57,730 And please note with variables I may not have mentioned this before but when you're using variables 92 00:05:57,730 --> 00:06:03,640 or when you're declaring variables you want to avoid spaces in the names and you want to avoid special 93 00:06:03,640 --> 00:06:08,090 characters almost the same rules as when I suggested how you name your projects. 94 00:06:08,170 --> 00:06:14,410 You can chemically case the names if you have multiple words some people use underscores but ultimately 95 00:06:14,410 --> 00:06:19,460 just avoid any space and any special character. 96 00:06:19,550 --> 00:06:19,910 All right. 97 00:06:19,930 --> 00:06:34,180 So no one is going to be equal to two and Noam two is equal to five and see in doing that I violated 98 00:06:34,180 --> 00:06:36,660 my own rules and Visual Studio is telling me that I'm wrong. 99 00:06:36,670 --> 00:06:40,560 So int num 2 is equal to 5. 100 00:06:40,690 --> 00:06:45,880 And before I use these two variables in the example I just want to take this time to list all the other 101 00:06:45,880 --> 00:06:50,310 comparison operators that you have to your advantage. 102 00:06:50,340 --> 00:06:51,130 So you have. 103 00:06:52,190 --> 00:06:57,850 And I'm just putting the brakes there so you can see them so you have the less than you have greater 104 00:06:57,850 --> 00:07:01,390 than you have equivalents. 105 00:07:01,390 --> 00:07:08,530 So notice 2 equals science or the equal sign by itself is an assignment operator where you're assigning 106 00:07:08,530 --> 00:07:12,130 a value from the right to the variable on the left. 107 00:07:12,130 --> 00:07:18,850 However if you want to compare if two things are equal to each other then you have the equal equal all 108 00:07:18,850 --> 00:07:19,110 right. 109 00:07:19,120 --> 00:07:23,480 So that's the distinction between a single equals and a W equals side. 110 00:07:23,620 --> 00:07:25,220 So we have equal equal. 111 00:07:25,510 --> 00:07:35,450 We have hybrid ones like greater than and equal or less than an equal. 112 00:07:35,530 --> 00:07:41,680 And then you have one that say is not equal which is an exclamation sign an equal sign. 113 00:07:41,680 --> 00:07:47,140 All right so those are operators that you can use inside of your if statements when you want to compare 114 00:07:47,170 --> 00:07:49,840 the value on the left with the value on the right. 115 00:07:50,210 --> 00:07:50,490 Okay. 116 00:07:50,500 --> 00:07:52,730 So don't we have no one to name two. 117 00:07:52,840 --> 00:07:59,000 We did this with static values but we can also write an if statement with variables. 118 00:07:59,020 --> 00:07:59,580 All right. 119 00:07:59,650 --> 00:08:08,280 So I could be comparing number one with the number three so I couldn't say if no one is less than three. 120 00:08:08,380 --> 00:08:13,990 Then I want to take this action otherwise I want to take that action. 121 00:08:14,020 --> 00:08:14,260 All right. 122 00:08:14,500 --> 00:08:19,360 So in this situation I have no one declared to have a value of two. 123 00:08:19,390 --> 00:08:25,210 So if I execute this then two is going to be less than three or verify I said No I'm five here based 124 00:08:25,210 --> 00:08:30,880 on the value of num five then num five oh sorry num two sorry. 125 00:08:30,910 --> 00:08:33,220 Number two has the value of five my butt. 126 00:08:33,310 --> 00:08:39,550 So if I say no two is less than three as long as Num two retains the value of five then we we'll know 127 00:08:39,550 --> 00:08:41,890 that it is not less than three. 128 00:08:41,920 --> 00:08:44,580 And so it will go to the else block and print. 129 00:08:44,590 --> 00:08:46,270 No it's not. 130 00:08:46,270 --> 00:08:51,800 So you can use variables to compare with static values and you can also compare two variables. 131 00:08:51,910 --> 00:08:54,310 Number one and number two. 132 00:08:54,310 --> 00:08:54,700 All right. 133 00:08:55,030 --> 00:08:56,290 So here's what I'm going to do. 134 00:08:56,290 --> 00:09:02,110 We're going to write a full program where we're accepting data from a user and then we will run an if 135 00:09:02,110 --> 00:09:07,670 statement to know what to print or based on the input from the user. 136 00:09:07,720 --> 00:09:13,240 So I'm going to write a program that's going to ask our users to tell us how many apples they have and 137 00:09:13,240 --> 00:09:15,750 how many oranges they have someone to console a lot. 138 00:09:15,750 --> 00:09:16,310 Right. 139 00:09:16,360 --> 00:09:23,500 The first problem to see how many apples do you have. 140 00:09:23,500 --> 00:09:34,150 And then we'll do a console dot read line where we get the inputs but then of course we can just console 141 00:09:34,150 --> 00:09:40,180 that read line this value needs to go even to a variable someone's use no one to store the number of 142 00:09:40,240 --> 00:09:40,620 apples. 143 00:09:40,630 --> 00:09:43,540 So no one is equal to the console that read line. 144 00:09:43,810 --> 00:09:46,570 And then we know that no one is an integer. 145 00:09:46,660 --> 00:09:52,810 So if we're putting something into the integer from the console inputs we need to convert it someone 146 00:09:52,810 --> 00:10:03,670 to say convert dot to int 32 and then open for in the CIS and close parentheses around that and then 147 00:10:03,670 --> 00:10:09,220 I'm just going to copy this line and settle for typing it from scratch and then I want to say how many 148 00:10:09,640 --> 00:10:15,470 oranges do you have and we're storing oranges in Gnome 2. 149 00:10:15,490 --> 00:10:22,030 So no I have user input for gnome 1 and user input for gnome which of course will override the values 150 00:10:22,030 --> 00:10:27,700 that they were initialized with because I'm not going to trouble the initialization here or at least 151 00:10:27,700 --> 00:10:33,670 I would always recommend that if you're initializing you always initialize to a safe enough value that 152 00:10:33,730 --> 00:10:36,870 you know it doesn't tamper with your operation going forward. 153 00:10:36,880 --> 00:10:43,810 So we're initializing gnome 1 to 0 and num 2 to 0 and then we're prompting for the number of apples 154 00:10:43,810 --> 00:10:48,990 and starting that's in num 1 and then I'm prompting for the number of oranges and storing that in num 155 00:10:49,000 --> 00:10:57,280 to know my if statement is seeing if number two is less than number one then I want to take a particular 156 00:10:57,310 --> 00:11:04,070 action but then I feel probably more comfortable saying if gnome 2 is greater than no one it really 157 00:11:04,070 --> 00:11:04,670 doesn't matter. 158 00:11:04,670 --> 00:11:11,140 So based on your context and based on how you probably would want to if you can see it in English or 159 00:11:11,150 --> 00:11:13,710 see it yourself how would I compare these two. 160 00:11:13,790 --> 00:11:18,900 Then you can just translated into code using the appropriate operator. 161 00:11:18,940 --> 00:11:26,720 All right so I'm going to say if no one is greater than num 2 then that means that I would have more 162 00:11:26,870 --> 00:11:28,280 oranges. 163 00:11:28,280 --> 00:11:34,550 So gnome 2 represents oranges so if num to is greater than the one that I have more oranges than I have 164 00:11:34,730 --> 00:11:45,360 apples so then my console or a lion is going to be you have more oranges and then if I if that is fours 165 00:11:45,360 --> 00:11:52,580 meaning num two is not greater than no one then I'm going to print you have more apples 166 00:11:55,240 --> 00:11:55,630 not less. 167 00:11:55,630 --> 00:11:57,730 The quick test and see the Oh come off that. 168 00:11:57,730 --> 00:11:59,620 So how many apples do you have. 169 00:11:59,620 --> 00:12:03,520 I'm going to say I have two apples and I have five oranges. 170 00:12:03,550 --> 00:12:07,850 So the expectation is that it brings you have more oranges. 171 00:12:07,880 --> 00:12:15,540 Alright let's do another test where I flip the script this time I have ten apples and I have five oranges. 172 00:12:15,610 --> 00:12:22,330 And so when that evaluates in my if statement I should be getting you have more apples. 173 00:12:22,330 --> 00:12:23,800 There we go. 174 00:12:23,800 --> 00:12:29,170 So that's basically what a selection does it looks at a condition and then it makes a decision based 175 00:12:29,200 --> 00:12:32,920 on the outcome whether it's true or false for that condition. 176 00:12:32,920 --> 00:12:38,260 Now the if statement can be expanded a bit more and I'm just going to do this and then we leave if statements 177 00:12:38,260 --> 00:12:44,470 for no because if statements are very useful and as we go along we will be using them and see more purposes 178 00:12:44,500 --> 00:12:51,190 but I just want us to have an appreciation for how they work and why we will be meeting them going forward. 179 00:12:51,610 --> 00:12:57,860 But the if statement can also have more than one or more than two blocks so right now we're just looking 180 00:12:57,870 --> 00:13:06,850 at if condition and then else but then you do have the facility to have multiple conditions in one big 181 00:13:06,910 --> 00:13:15,760 if statement so I can see if not two is greater than no one then print you have more oranges then I 182 00:13:15,760 --> 00:13:25,060 can see else if I then state another condition so I'm going to see if no one is greater than gnome 2 183 00:13:25,900 --> 00:13:29,040 no one being apples and no to being oranges. 184 00:13:29,050 --> 00:13:35,140 So first I'm taking to have more oranges than apples that I can see otherwise if there are more apples 185 00:13:35,140 --> 00:13:42,430 and oranges then I want to print you have more apples What if I wanted to add on another statement and 186 00:13:42,430 --> 00:13:50,590 other condition I said else if and then state another condition and then for that part I would want 187 00:13:50,590 --> 00:13:53,130 to see if they are equivalent. 188 00:13:53,160 --> 00:14:05,110 So I can see if no one is equivalent to Gnome 2 then open Bruce console the right line and then I'm 189 00:14:05,110 --> 00:14:19,760 going to see you have booths or let me say the same you have the same number of apples and oranges. 190 00:14:19,820 --> 00:14:25,610 Now we have one if statement doing three different conditions. 191 00:14:25,610 --> 00:14:25,900 All right. 192 00:14:25,910 --> 00:14:34,640 So I've if num two is greater than num 1 then 4 in something else if no one is agreed to then to then 193 00:14:34,760 --> 00:14:39,740 print something else if no one is equivalent to Gnome 2. 194 00:14:40,040 --> 00:14:44,710 So there are plenty of ways you could have written this but then I mean there are so many ways or at 195 00:14:44,710 --> 00:14:46,880 least I can negotiate every single scenario. 196 00:14:46,880 --> 00:14:51,030 Once you appreciate the concept then you can be more flexible. 197 00:14:51,050 --> 00:14:56,620 So in this situation once again I'm just checking if num two is greater than no one then I want to print 198 00:14:56,630 --> 00:14:58,220 you have more oranges. 199 00:14:58,220 --> 00:15:04,750 And then if that doesn't evaluate to True then moves on to the next statement to see console the red 200 00:15:04,750 --> 00:15:07,380 line you have more apples right. 201 00:15:07,400 --> 00:15:13,040 And if that evaluates the falls and he moves onto the next condition where it says okay if they're equivalent 202 00:15:13,100 --> 00:15:16,150 then print this statement. 203 00:15:16,160 --> 00:15:21,230 No there are situations where none of these would ever match and then that's why you have the L so the 204 00:15:21,230 --> 00:15:22,510 S is that a catch. 205 00:15:22,560 --> 00:15:23,590 All right. 206 00:15:23,660 --> 00:15:26,620 So if we have an S. 207 00:15:26,680 --> 00:15:32,560 You usually use that to say whether when every other condition fails then take some action. 208 00:15:32,560 --> 00:15:34,930 And this is once again is a catch all. 209 00:15:34,960 --> 00:15:39,510 So I went to see final action. 210 00:15:39,530 --> 00:15:44,240 Ah well invalid parameters No. 211 00:15:44,440 --> 00:15:50,380 Based on this example I don't know what I could possibly put in that none of these would get met. 212 00:15:50,530 --> 00:15:57,730 And further to that the comparison far greater than an equal to unless that an equal to would not fit 213 00:15:57,730 --> 00:15:58,990 into this scenario. 214 00:15:58,990 --> 00:16:06,340 And well we always expect it if one of these would be met then clearly they're not equal right but then 215 00:16:06,340 --> 00:16:11,800 the case study or the use cases for these rather would be like if you are comparing grades. 216 00:16:11,800 --> 00:16:12,100 Right. 217 00:16:12,130 --> 00:16:20,170 So if the grades start at maybe an A starts at 90 then you can see if you have agreed that is greater 218 00:16:20,170 --> 00:16:22,180 than or equal to 90. 219 00:16:22,180 --> 00:16:23,880 Then you have an E. 220 00:16:24,010 --> 00:16:30,700 If they fail a failing grade is fifty nine then you can see if you have agreed that is less than or 221 00:16:30,850 --> 00:16:37,300 equal to fifty nine then you are a failing student so you could have done the greater than an equal 222 00:16:37,300 --> 00:16:38,680 to for that A. 223 00:16:38,710 --> 00:16:44,380 Or I could have just said if your grade is greater than eighty nine because then we know that anything 224 00:16:44,380 --> 00:16:46,050 greater than 89 is probably 90. 225 00:16:46,080 --> 00:16:52,060 But then based on the scenario once again you may or may not need to use it or you can use multiple 226 00:16:52,260 --> 00:16:54,140 to accomplish the same thing. 227 00:16:54,280 --> 00:17:00,340 Anyhow let's run a test on this so I'm going to put in some scenarios where I'm going to put in some 228 00:17:00,340 --> 00:17:05,870 values and then we should put in values that will invoke each statement as we go along. 229 00:17:05,890 --> 00:17:12,550 Once again the L statement may never get called because one of these conditions will be met anyway so 230 00:17:12,550 --> 00:17:13,560 let's try it out. 231 00:17:13,570 --> 00:17:18,140 So firstly we want a bigger number for num 2 than for an on one. 232 00:17:18,190 --> 00:17:21,230 So we want more oranges than apples. 233 00:17:21,510 --> 00:17:26,310 So 5 apples and then I have 10 oranges. 234 00:17:26,380 --> 00:17:28,830 So num 2 is greater than no one. 235 00:17:28,830 --> 00:17:31,110 So you have more oranges all right. 236 00:17:31,360 --> 00:17:38,080 Let me run this theme and this program again this time I have more apples than oranges of ten apples 237 00:17:38,110 --> 00:17:41,910 and I only have five oranges then it is you have more apples. 238 00:17:41,920 --> 00:17:47,480 So this condition for no one being greater than num num 2 being greater than on one field. 239 00:17:47,620 --> 00:17:51,400 So it went on to the else if and then this condition was met. 240 00:17:51,460 --> 00:17:53,250 So it printed that statement. 241 00:17:53,290 --> 00:17:57,970 Now if I have the same number between the two fruit types. 242 00:17:57,970 --> 00:18:01,660 So that means I have ten oranges and ten apples when I press enter. 243 00:18:01,660 --> 00:18:02,960 Then it went to the else. 244 00:18:02,980 --> 00:18:08,790 If they are equivalent then you have the same number of apples and oranges. 245 00:18:08,850 --> 00:18:09,220 All right. 246 00:18:09,310 --> 00:18:13,420 So there are some rules surrounding whole if statements should look. 247 00:18:13,420 --> 00:18:16,780 Notice the special toolkits that I've been using along the way. 248 00:18:16,780 --> 00:18:24,220 The key words if and then the parentheses and then the condition inside the parentheses and then notice 249 00:18:24,220 --> 00:18:26,830 no semicolon at the end of this line. 250 00:18:26,920 --> 00:18:31,900 And then I opened the curly brace and then the C sharp syntax as we've come to Lloyds with all the consular 251 00:18:31,900 --> 00:18:38,740 rights consular read line whatever it is that we're doing that those can proceed but then only between 252 00:18:38,740 --> 00:18:47,410 the braces so I can put any code in this line I can't be console the right line in between the if block 253 00:18:47,470 --> 00:18:48,190 just like that. 254 00:18:48,190 --> 00:18:49,060 Right. 255 00:18:49,120 --> 00:18:49,990 That is illegal. 256 00:18:50,020 --> 00:18:54,790 So when it whatever operation it is you need to carry out a notice of that red line. 257 00:18:54,790 --> 00:19:00,520 So it's saying that it's expecting it's incorrect syntax for a bill until you details about the incorrect 258 00:19:00,520 --> 00:19:06,730 syntax but just follow these rules and you will should feel comfortable in writing your statements. 259 00:19:06,730 --> 00:19:15,460 So once again you have a condition you have an if statement sorry you create the condition inside of 260 00:19:15,460 --> 00:19:21,160 the parentheses and then you open the curly brace and then you proceed to write all your code inside 261 00:19:21,160 --> 00:19:26,740 of your curly braces whatever you're acting instead of the curly braces is what gets done when that 262 00:19:26,740 --> 00:19:29,030 particular condition is met. 263 00:19:29,050 --> 00:19:34,830 So if num two is greater than on one then I want to take this set of actions. 264 00:19:34,840 --> 00:19:41,920 Else if this other condition is met meaning this one failed but then this one gets met then I want to 265 00:19:41,920 --> 00:19:50,410 carry out these actions and then I have another s if if this condition is met then I have my specific 266 00:19:50,710 --> 00:19:56,840 actions and then my eldest is the catch all meaning that none of these came to pass. 267 00:19:56,860 --> 00:20:02,740 None of them matched my requirements so that else would be that a catch all. 268 00:20:02,770 --> 00:20:08,670 So you know based on the situation then it may be good it may not be but so in this situation if it 269 00:20:08,680 --> 00:20:12,060 will go to the else I know that that's bad because that's not what I expect. 270 00:20:12,070 --> 00:20:16,830 However in our first example you saw that I only had if condition and then else. 271 00:20:16,900 --> 00:20:23,260 So when it's a simple condition where it's either R it's it's either this condition is true or it's 272 00:20:23,260 --> 00:20:28,120 not true then you can just use an if else if you have five different scenarios that you need to test 273 00:20:28,120 --> 00:20:34,810 or in this case three different scenarios you need to test then you can write if elusive else if and 274 00:20:34,810 --> 00:20:38,730 if you know that they're the only three then you really don't even need the else. 275 00:20:38,740 --> 00:20:45,110 So it can work without the in this scenario like I said this is an introduction and so we're just scratching 276 00:20:45,110 --> 00:20:50,750 the surface with statements but I'm going to move on to switch statements which help us to make selections 277 00:20:50,780 --> 00:20:56,660 but just do it a bit differently so that your statement gives us the ability to make more complex decisions 278 00:20:56,660 --> 00:21:03,680 where we can see if something is greater than we can compare to values or some variable to some other 279 00:21:03,680 --> 00:21:09,410 value whatever it is or comparing and then make an action the switch statement over we have to look 280 00:21:09,410 --> 00:21:16,070 at a variable and then make a decision based on just the value of the variable. 281 00:21:16,070 --> 00:21:17,770 So I'm going to give you an example. 282 00:21:17,780 --> 00:21:25,340 So the switch statements look something like switch and then it takes a variable inside of the parentheses. 283 00:21:25,460 --> 00:21:29,780 So what I'm going to do is to find another variable and I'm going to call this one num three so into 284 00:21:29,780 --> 00:21:33,340 num three and it's going to be equal to zero. 285 00:21:33,350 --> 00:21:40,850 All right so I'm going to see a switch num three which means assess the value inside of num 3 then I'm 286 00:21:40,850 --> 00:21:45,730 going to see keys and then I can give a value. 287 00:21:45,740 --> 00:21:51,960 So case 1 then call on so please note the syntax because this is very important that you follow the 288 00:21:51,960 --> 00:21:59,960 syntax properly so you see a switch open various or open parentheses put a variable in there and then 289 00:22:00,200 --> 00:22:07,070 you open your current embrace and then you make a key CSA case then the value that you want to assess 290 00:22:07,370 --> 00:22:08,540 this variable for. 291 00:22:08,540 --> 00:22:16,640 So I'm seeing if the value of num three is one then I say call on what comes after the call on is the 292 00:22:16,650 --> 00:22:25,910 action I intend to take so that I'm want to see console that right line and let's keep it simple and 293 00:22:25,910 --> 00:22:26,430 just save. 294 00:22:26,430 --> 00:22:29,400 Value is one. 295 00:22:29,590 --> 00:22:30,360 All right. 296 00:22:30,420 --> 00:22:36,260 And then after I say that then I need a keyword so I can list as many actions underneath this. 297 00:22:36,260 --> 00:22:41,460 So what comes after the case would be just that what comes inside the curly braces for the if statement 298 00:22:41,700 --> 00:22:46,620 but then I have to end the case with a keyword that says break. 299 00:22:46,620 --> 00:22:47,280 All right. 300 00:22:47,280 --> 00:22:55,920 So I can see Case 1 consular land value is 1 so I'm just want to copy this I can retype it then what 301 00:22:55,920 --> 00:23:04,170 if I wanted to do something else if the value was to so I have case to call on then I state my action 302 00:23:04,350 --> 00:23:09,630 someone to console the right line and I don't want to change it from values 1 and say something like 303 00:23:09,990 --> 00:23:12,780 I have to know. 304 00:23:12,830 --> 00:23:13,540 All right. 305 00:23:13,560 --> 00:23:19,590 So I have to know if the value is to print I have to know to the screen and then once again I can print 306 00:23:19,710 --> 00:23:28,110 as many things as I want or take as many other actions as I need to someone to print this three times 307 00:23:28,290 --> 00:23:29,560 or three more times. 308 00:23:29,580 --> 00:23:34,650 So and this is just for example I'm just showing you that you can have multiple lines coming. 309 00:23:34,680 --> 00:23:41,700 Often these after the case and the value and the call on and then when you're done you just make sure 310 00:23:41,700 --> 00:23:48,660 you put your key word that is break if you don't put the brake keyword and let's say I forgot the brake 311 00:23:48,660 --> 00:23:54,840 keyword then what would happen is that it would just run on so he can have as many cases as you feel 312 00:23:54,840 --> 00:23:56,040 the need for it. 313 00:23:56,040 --> 00:24:03,830 So as many situations or cases that you think are values that you want to compare this variable again 314 00:24:03,930 --> 00:24:09,000 you can have case now whatever that value is since we're using integer we just put the value as integer 315 00:24:09,030 --> 00:24:14,370 if this was a string then I would be seeing keys and then putting in the string. 316 00:24:14,880 --> 00:24:20,130 But then we have to keep it type sensitive so if it's an integer variable that we're assessing then 317 00:24:20,130 --> 00:24:25,590 the key is value must be an integer if it's a double then well it has to be numeric if it's integer 318 00:24:25,620 --> 00:24:32,460 double if it's a string then it must be a string but then otherwise we have similar to the Ls in the 319 00:24:32,460 --> 00:24:41,820 if statement we have a default situation that says well if none of the cases match then I am going to 320 00:24:41,820 --> 00:24:42,450 do something. 321 00:24:42,480 --> 00:24:50,130 So I'm went to inside of the default this console right line and see invalid value. 322 00:24:50,130 --> 00:24:57,780 All right so I'm only comparing 4 1 and 2 which means that if if GNOME 3 is never 1 and not 1 3 is never 323 00:24:57,780 --> 00:25:05,950 2 then I want the default to come into play and see invalid value and then once again I break once I've 324 00:25:05,970 --> 00:25:08,990 done that everything is inside of that curly brace. 325 00:25:09,090 --> 00:25:13,160 So I'm just going to put in a prompt for gnome three at the same time. 326 00:25:13,170 --> 00:25:20,250 So after we do with our apples and oranges I'm going to see how many eyes do you have. 327 00:25:20,250 --> 00:25:21,130 That's a good one. 328 00:25:21,130 --> 00:25:21,690 That's right. 329 00:25:21,720 --> 00:25:28,290 It's either one eye or two eyes but then I I'm just going to say invalid value if you put any other 330 00:25:28,290 --> 00:25:30,570 number that's not one or two. 331 00:25:30,630 --> 00:25:30,900 Right. 332 00:25:30,930 --> 00:25:35,010 So how many eyes do you have. 333 00:25:35,010 --> 00:25:38,420 And then I'm going to console the right. 334 00:25:38,470 --> 00:25:39,080 Right. 335 00:25:39,170 --> 00:25:41,910 Red line sorry into num three. 336 00:25:42,180 --> 00:25:43,370 So let's test that one out. 337 00:25:43,380 --> 00:25:49,710 So I don't want off to do all of this again so I'm just going to comment oat my if statement. 338 00:25:50,190 --> 00:25:54,590 So the shortcut to comment is control key and see. 339 00:25:54,610 --> 00:25:54,890 Right. 340 00:25:54,900 --> 00:26:01,410 So holding on after highlighting the lines I just held on control of press K press C and then all of 341 00:26:01,410 --> 00:26:06,540 those lines that were highlighted got commented so no when I execute my program is going to ignore the 342 00:26:06,540 --> 00:26:11,190 comments on lines and just go straight to asking me how many eyes do I have. 343 00:26:11,190 --> 00:26:15,400 And then taking an action based on our values. 344 00:26:15,410 --> 00:26:20,700 So I have one eye then I want to say value is one. 345 00:26:20,720 --> 00:26:21,160 All right. 346 00:26:21,350 --> 00:26:23,140 Maybe I can make it a bit more intuitive. 347 00:26:23,150 --> 00:26:26,620 Some want to change it and see you have one. 348 00:26:26,630 --> 00:26:26,950 I 349 00:26:30,940 --> 00:26:36,630 write this is just for educational purposes it's not really necessary. 350 00:26:36,630 --> 00:26:42,130 So if you don't feel like changing it then that's fine but you have one eye and then I want to try it 351 00:26:42,130 --> 00:26:44,780 again and then I'm want to put in the value too. 352 00:26:44,890 --> 00:26:50,770 So with two we expect to have four statements being printed and I have to know it's printed four times 353 00:26:50,770 --> 00:26:56,350 so once again you can have as many lines as required underneath the key. 354 00:26:56,360 --> 00:27:03,010 So in between the case definition and the keyword break everything in there will get executed. 355 00:27:03,310 --> 00:27:09,880 So let's do another one and we will put in the value of three so three since there is no case defined 356 00:27:09,880 --> 00:27:15,270 for a value of three we'll go to the default and tell us in valid values. 357 00:27:15,280 --> 00:27:18,380 So that's basically how such statements work. 358 00:27:18,380 --> 00:27:20,170 A good use case for us suits them. 359 00:27:20,170 --> 00:27:25,690 It would be like in a menu setting where you're expecting certain values so if you say like Press 1 360 00:27:25,690 --> 00:27:35,020 to enter grade press 2 to view grade press 3 to exit program then you could just see get the user's 361 00:27:35,020 --> 00:27:43,060 input into the variable and then you were a switch against that value and then based on which case was 362 00:27:43,090 --> 00:27:46,240 chosen or which value was selected you run a case for that. 363 00:27:46,390 --> 00:27:52,690 You take the actions accordingly and then if they enter a menu option of 5 when you only have three 364 00:27:52,690 --> 00:27:55,370 menu options then you have the default. 365 00:27:55,410 --> 00:27:56,460 All right. 366 00:27:56,490 --> 00:28:01,510 Now as a bonus I'm going to show you ternary statements and I left this one for last because this one 367 00:28:01,510 --> 00:28:07,510 can be a bit difficult to grasp and they are always our own using it but it can be a bit difficult but 368 00:28:07,510 --> 00:28:08,980 it's very very convenient. 369 00:28:08,980 --> 00:28:10,630 Once you understand how it works. 370 00:28:10,630 --> 00:28:16,150 So I'm going to find another variable and I'm going to call this one result. 371 00:28:16,180 --> 00:28:20,920 So this one is going to be string and I'm calling this results all right. 372 00:28:21,370 --> 00:28:22,120 No. 373 00:28:22,120 --> 00:28:27,970 The purpose of the Turner operator is to run a quick comparison and it allows you to decide right there 374 00:28:27,970 --> 00:28:35,190 on the spot what value to assign to a variable when that condition is met are not met. 375 00:28:35,200 --> 00:28:41,800 So in other words with our if statement if I said no to greater than no one I could have done an assignment 376 00:28:41,830 --> 00:28:47,860 to a variable in here but then I would have to say number tool is equal to the num num 1 groups that 377 00:28:47,860 --> 00:28:52,820 I known to and then do another assignment here and then on this one after do another assignment. 378 00:28:52,900 --> 00:29:02,710 Whereas in a ternary operator I can see the result is equal to and then what I want to compare is num 379 00:29:03,100 --> 00:29:06,140 to being greater than no one. 380 00:29:06,220 --> 00:29:07,690 All right so this is my condition. 381 00:29:09,020 --> 00:29:17,100 And then I can use a call on and then I see and I think usually this needs to go inside parentheses. 382 00:29:17,100 --> 00:29:19,070 So as this puts it in parentheses. 383 00:29:19,200 --> 00:29:22,340 So then I say this is my condition. 384 00:29:22,380 --> 00:29:24,320 And then question mark sorry. 385 00:29:24,360 --> 00:29:30,980 Question Mark and then I'm going to see no one greater then no to greater than no one. 386 00:29:31,050 --> 00:29:37,110 Then I'm going to say what what did we assign more oranges. 387 00:29:37,110 --> 00:29:44,280 You have more oranges when that is so you have more oranges and then call on and then we're going to 388 00:29:44,280 --> 00:29:47,980 say you have more apples all right. 389 00:29:48,280 --> 00:29:49,830 So let me just start over. 390 00:29:49,840 --> 00:29:50,980 I just happened. 391 00:29:51,120 --> 00:29:59,340 So we have a variable and then I'm seeing what the contents of this variable depend on the outcome of 392 00:29:59,340 --> 00:30:00,270 this comparison. 393 00:30:00,990 --> 00:30:07,790 So my variable result is going to be assigned some value based on the result of this compiler. 394 00:30:07,790 --> 00:30:16,080 So what you see is num to agree to that number one then I have the question mark to see if this is true. 395 00:30:16,140 --> 00:30:23,220 So all of this is seeing if this comparison is true then I must say ending that value to the inside 396 00:30:23,220 --> 00:30:29,130 of result and then the colon says otherwise it is this value. 397 00:30:29,160 --> 00:30:33,260 So the quick thing the good thing about this is that in one line I just run a minute. 398 00:30:33,270 --> 00:30:35,240 If else statement. 399 00:30:35,270 --> 00:30:35,730 All right. 400 00:30:35,730 --> 00:30:42,780 The downside is that that's all I can do I can't of a third scenario to say if if the greater than are 401 00:30:42,930 --> 00:30:45,900 their equivalent sorry I can't of a third scenario. 402 00:30:45,900 --> 00:30:51,660 So I just want on one condition and then I say based on the outcome what if it's true then I want the 403 00:30:51,660 --> 00:30:55,360 first one to be done if it's false and I want the last one to be done. 404 00:30:55,720 --> 00:30:57,510 Now once again this comes in handy. 405 00:30:57,510 --> 00:31:01,380 A lot of the times but the if statements are very easy to do. 406 00:31:01,440 --> 00:31:04,420 Such statements are very quick also. 407 00:31:04,530 --> 00:31:11,580 But then this if you understand the concept you can use this at any point and expedite completing your 408 00:31:11,580 --> 00:31:13,800 program that you are authoring. 409 00:31:13,800 --> 00:31:21,850 So I'm going to leave this program went to on commend all of these lines so that if you get your hands 410 00:31:22,000 --> 00:31:28,830 on the source code then you have all of these examples for you and so on comments that's control k you. 411 00:31:28,870 --> 00:31:30,330 So let me just show you that. 412 00:31:30,340 --> 00:31:35,530 So if I am on another line and I want to come into it I can just say control Casey and then it commands 413 00:31:35,530 --> 00:31:40,930 it and then if I want to on comments I can just say control key you and there you go. 414 00:31:40,930 --> 00:31:44,630 So you press K and you can see while holding don't control. 415 00:31:45,020 --> 00:31:48,250 All right so that's it for condition statements. 416 00:31:48,250 --> 00:31:50,470 That's a brush over. 417 00:31:50,470 --> 00:31:52,010 I hope you understand the concepts. 418 00:31:52,030 --> 00:31:53,840 If you don't feel free to reach out to me. 419 00:31:53,920 --> 00:32:01,210 But as you go along we're going to be using them more and more so you will get to appreciate real scenarios 420 00:32:01,210 --> 00:32:07,660 where you will need to make selections and decisions based on values that are coming into your system.