1 00:00:00,540 --> 00:00:05,160 Hello and welcome to this lecture on condition of statements. 2 00:00:05,250 --> 00:00:13,950 Sometimes when you write code you may not want the code to run all the time or you may want certain 3 00:00:13,950 --> 00:00:21,640 parts of the code to execute only when certain conditions have been met. 4 00:00:21,690 --> 00:00:29,550 So that is what is referred to as conditional statements that means code that will run based on certain 5 00:00:29,550 --> 00:00:31,150 conditions. 6 00:00:31,150 --> 00:00:41,920 And the most common conditional statements are if statements and also else statements so the simplest 7 00:00:42,460 --> 00:00:45,130 conditional statement is an if statement. 8 00:00:45,130 --> 00:00:54,040 So basically you specify a condition that if the condition is met then a piece of code will execute 9 00:00:54,610 --> 00:01:03,310 if the condition is not met then you can also attack and else statement an L statement basically will 10 00:01:03,550 --> 00:01:12,340 execute the statement if the statement in the if condition is not met then he executes the else statement. 11 00:01:12,670 --> 00:01:16,150 So I'm going to show you an example how this works. 12 00:01:16,420 --> 00:01:20,680 So I've got visual studio open I'm going to open up a new project going to create a new producer I'll 13 00:01:20,680 --> 00:01:31,090 go file new project and wait for it to load I'm gonna make it a Windows form and I'll just give you 14 00:01:31,090 --> 00:01:32,650 the name of conditional 15 00:01:39,400 --> 00:01:50,230 click okay and just wait for it to generate the project so Visual Studio has generated a basic form 16 00:01:50,230 --> 00:01:59,920 for us what will it do I'm gonna add a button control so I'm going to click on the tool box and just 17 00:01:59,920 --> 00:02:04,930 drag a button control into the tool box. 18 00:02:04,930 --> 00:02:13,300 Okay I'm just going to write create and change the button name click on the properties and I'll change 19 00:02:13,300 --> 00:02:14,290 it to 20 00:02:18,930 --> 00:02:21,310 greats. 21 00:02:21,330 --> 00:02:22,620 Okay. 22 00:02:22,710 --> 00:02:23,400 This 23 00:02:28,690 --> 00:02:35,190 Okay so double click on that button and when we double click it will generate an event. 24 00:02:35,220 --> 00:02:43,430 Angela OK so we have got an event and here an event handler method. 25 00:02:43,430 --> 00:02:46,580 This here's an event handler method. 26 00:02:46,700 --> 00:02:50,340 The event it's going to respond to is a click. 27 00:02:50,420 --> 00:02:58,420 So the event basically is something that a user or visitor performs on a Web site. 28 00:02:58,430 --> 00:03:03,260 The common event on the button is a click when you see a button you click on it 29 00:03:06,050 --> 00:03:08,090 when the button is clicked. 30 00:03:08,090 --> 00:03:16,370 We want a piece of code to execute so that we can write the code in between this curly these two curly 31 00:03:16,370 --> 00:03:22,550 braces here's where we're going to specify the code what I want to happen. 32 00:03:22,830 --> 00:03:32,780 I have called the I want to create a some conditional code here based on certain grades that a student 33 00:03:32,870 --> 00:03:36,310 will get in the exams. 34 00:03:36,320 --> 00:03:40,270 So what I'll do I will define an initial mark. 35 00:03:41,300 --> 00:03:51,320 And then if the total mark is too then gets is higher than the initial mark specified in message box 36 00:03:51,320 --> 00:03:53,900 would display in the button showing. 37 00:03:54,860 --> 00:04:06,070 If the user the student go to first class got a higher first class got a pass or got a fail or failed. 38 00:04:06,080 --> 00:04:09,760 So these are the various messages are like to display. 39 00:04:09,770 --> 00:04:14,810 So to start with I need to define a variable 40 00:04:17,720 --> 00:04:19,210 or define variable. 41 00:04:19,310 --> 00:04:31,480 You need a variable for the if statement to work with and I'll call the variable marks or just say final 42 00:04:31,520 --> 00:04:32,210 marks. 43 00:04:36,780 --> 00:04:37,900 Okay. 44 00:04:38,000 --> 00:04:41,390 And I've set given an initial value of say 45 00:04:44,500 --> 00:04:49,900 make eight fifty nine okay. 46 00:04:50,630 --> 00:04:58,370 So now I am going to specify some conditional code. 47 00:04:58,370 --> 00:05:09,340 So to do that I'll type in if and when use right conditional code you need to specify a condition so 48 00:05:09,350 --> 00:05:13,400 the condition here is that I've defined a variable called final marks. 49 00:05:13,490 --> 00:05:22,640 Some saying if the final marks this variable here of this student if the final mark 50 00:05:25,480 --> 00:05:35,060 is greater than or equal to say 75 51 00:05:37,940 --> 00:05:47,060 then this Scully this rest quickly line is complaining because with an if statement you need to have 52 00:05:47,060 --> 00:05:56,420 curly braces and in between these curly braces it is where you write the code that will execute if this 53 00:05:56,420 --> 00:05:59,960 condition I have specified here is met. 54 00:05:59,990 --> 00:06:07,910 So if this student Mark is greater than 70 is greater or equal to 75 it will execute this code if it 55 00:06:07,910 --> 00:06:11,740 is not it will execute another block of code. 56 00:06:11,930 --> 00:06:20,150 So I say if the final marks of the student is greater than or equal to 75 I want the message to be displayed. 57 00:06:20,180 --> 00:06:22,770 So to do that I'm going to use a message box. 58 00:06:22,790 --> 00:06:29,980 This is a built in function or method in C sharp. 59 00:06:30,410 --> 00:06:31,760 So I'll type a message 60 00:06:35,180 --> 00:06:40,230 C notice I'm trying to type a message box or type a message box. 61 00:06:40,360 --> 00:06:41,040 All right. 62 00:06:41,040 --> 00:06:45,590 Dot need to assign a method is called a show method. 63 00:06:45,620 --> 00:06:53,540 So message box to show and I need to specify what message it would display. 64 00:06:53,540 --> 00:07:01,370 So if these students score is greater or equal to 75 I wanted to display 65 00:07:04,490 --> 00:07:05,630 first class 66 00:07:13,600 --> 00:07:15,130 or you got first class 67 00:07:20,770 --> 00:07:26,770 okay then puts a semicolon there to end that statement. 68 00:07:26,770 --> 00:07:38,830 So if this condition is met this code here will execute if the condition here specified is not met then 69 00:07:38,830 --> 00:07:41,440 I can specify an else statement. 70 00:07:41,440 --> 00:07:44,130 So this is how you specify an L statement. 71 00:07:44,360 --> 00:07:46,030 You type in else 72 00:07:48,800 --> 00:07:59,060 okay and insert and then you type in else this quickly line is complaining because I've not included 73 00:07:59,060 --> 00:08:05,220 my curly braces so I need to specify the condition for the L statement inside here. 74 00:08:05,300 --> 00:08:12,490 So if this condition here is not met it will display this message box ignore it all. 75 00:08:12,500 --> 00:08:15,740 If this condition is not met it will skip this block of code. 76 00:08:15,740 --> 00:08:17,630 It will not execute this at all. 77 00:08:18,020 --> 00:08:19,720 Thus why is a conditional statement. 78 00:08:19,730 --> 00:08:23,310 It will now jump on to this else statement. 79 00:08:23,480 --> 00:08:26,010 So I'm going to define a code here I'm going to say. 80 00:08:26,150 --> 00:08:26,890 Message 81 00:08:33,750 --> 00:08:39,560 message box don't show. 82 00:08:41,000 --> 00:08:43,110 I'm going to say you got 83 00:08:48,990 --> 00:08:49,470 a 84 00:08:51,930 --> 00:08:52,950 second 85 00:08:55,800 --> 00:08:56,430 class 86 00:09:02,030 --> 00:09:13,100 so this is a typical classic conditional statement saying if this condition is met. 87 00:09:13,220 --> 00:09:19,040 If the student gets greater or higher than 75 it would display this. 88 00:09:19,160 --> 00:09:24,710 If not it will display this message so I'll save that and execute the code. 89 00:09:27,750 --> 00:09:30,610 OK I'll just wait for it to run. 90 00:09:30,950 --> 00:09:34,610 So if I click on the button that says grades 91 00:09:38,110 --> 00:09:42,460 it tells me you got a second class. 92 00:09:42,600 --> 00:09:51,910 So because the condition was not met because initially final marks he offset is 59. 93 00:09:51,910 --> 00:09:59,410 So I've said if the final mark is greater than 75 which is not because the math final marks is this 94 00:10:00,220 --> 00:10:02,560 the reason is not greater than that. 95 00:10:02,560 --> 00:10:09,290 That's why this block of code was skipped and he showed this block of code instead. 96 00:10:09,290 --> 00:10:20,950 If I was to change that to say I change that to 78 then this if statement this condition will become 97 00:10:20,950 --> 00:10:26,510 true and it will execute this and not even bother by the second code. 98 00:10:26,530 --> 00:10:28,380 All right let's try that. 99 00:10:29,510 --> 00:10:31,570 Okay. 100 00:10:32,300 --> 00:10:39,700 So if I click button it says you got a first class okay because the first condition which is the if 101 00:10:39,700 --> 00:10:41,590 statement has been met. 102 00:10:41,590 --> 00:10:46,110 So he didn't bother executing the second part of the code. 103 00:10:46,180 --> 00:10:50,740 So this is a very simple basic conditional statement. 104 00:10:50,740 --> 00:10:57,380 Conditional statements are very important in your work as a programmer or developer.