1 00:00:00,690 --> 00:00:13,860 Hello and welcome to this lecture on while loop while loops are basically statements or code that executes 2 00:00:14,430 --> 00:00:17,360 based on a certain condition. 3 00:00:17,850 --> 00:00:27,540 So as long as the condition is true the code will continue to execute the code will only stop when that 4 00:00:27,540 --> 00:00:30,020 condition becomes false. 5 00:00:30,270 --> 00:00:38,060 So we will illustrate in this lecture someone to create a new project. 6 00:00:38,070 --> 00:00:50,210 So I just go file new project and give it a Windows form on our call is loops. 7 00:00:50,520 --> 00:00:55,050 So I change the default name from Windows Forms to loops 8 00:00:58,370 --> 00:01:01,040 and click Okay. 9 00:01:03,350 --> 00:01:09,840 What I'm gonna do I'm just gonna add a button to this form here. 10 00:01:10,520 --> 00:01:20,720 So I'll go to view to view the tool box go to box and just drag it button in the 11 00:01:29,010 --> 00:01:34,160 OK so to see the code behind the form you can click on View 12 00:01:38,440 --> 00:01:44,950 you click on the form first then you click on view make sure the form is clicked on and then you select 13 00:01:45,250 --> 00:01:51,250 code and to show you the code behind the form. 14 00:01:51,250 --> 00:01:54,430 So let's go back to the form design and this song. 15 00:01:54,430 --> 00:02:02,310 Want a double click on the button so it creates is top of the of an event handler. 16 00:02:03,640 --> 00:02:09,820 Okay so we've got a stop here of a button click event handler. 17 00:02:10,330 --> 00:02:18,160 So in between these curly braces is will write the code to handle the Click event for this button. 18 00:02:20,230 --> 00:02:25,400 So we are going to write a code that we lost trade. 19 00:02:25,450 --> 00:02:35,030 The while loop so the statement in the call the code will continually execute it will. 20 00:02:35,260 --> 00:02:36,370 That's why it's called a loop. 21 00:02:36,370 --> 00:02:44,380 It will keep running that code as long as the condition is true so it will execute the code inside the 22 00:02:44,380 --> 00:02:45,940 block this block. 23 00:02:45,940 --> 00:02:47,040 This is a block. 24 00:02:47,040 --> 00:02:51,790 It will continue to execute that code as long as the condition is true. 25 00:02:51,790 --> 00:02:57,280 It will only stop when that condition becomes false. 26 00:02:57,640 --> 00:03:09,010 So we are going to write a code some code here that will be executed to respond to this click event 27 00:03:09,220 --> 00:03:10,080 handler. 28 00:03:10,210 --> 00:03:10,650 All right. 29 00:03:10,660 --> 00:03:13,350 So this is done by illustration of the while loop. 30 00:03:13,510 --> 00:03:19,980 So to begin we have to define a variable so I'm going to define a variable and it's going to be an integer. 31 00:03:20,110 --> 00:03:27,040 So we start with the type which is an integer and I'm going to call the variable count when I give it 32 00:03:27,040 --> 00:03:33,280 a value close to 1 right. 33 00:03:33,290 --> 00:03:36,500 So that's a variable that's a name. 34 00:03:36,620 --> 00:03:37,570 And that's the value. 35 00:03:38,600 --> 00:03:44,450 Okay so to set up a while loop you need to type in the word while 36 00:03:48,670 --> 00:03:56,430 which indicates it's a while loop you say while the count which is a variable here 37 00:04:04,730 --> 00:04:06,830 something while these count 38 00:04:10,120 --> 00:04:16,120 is less than or equal to for 39 00:04:20,760 --> 00:04:32,170 so I'd say while this count which is this variable is less than or equal to 4 I need you to execute 40 00:04:32,320 --> 00:04:33,430 this code. 41 00:04:33,610 --> 00:04:39,490 So the code I wanted to execute I need to specify the code inside curly braces. 42 00:04:39,970 --> 00:04:43,190 Just like you would do with an if statement. 43 00:04:43,570 --> 00:04:51,760 So inside this curly braces here is where I will define the code that will execute as long as this condition 44 00:04:51,760 --> 00:04:53,680 here is true. 45 00:04:54,400 --> 00:05:01,060 So I'm going want it to display a message so I'm going to use a message box. 46 00:05:01,810 --> 00:05:13,300 So typing message box and I'm going to attach a show method so it will show the message and inside the 47 00:05:13,300 --> 00:05:21,040 parentheses I need to specify the message so I'll say as long as that condition is true I'll see the 48 00:05:21,040 --> 00:05:21,750 value 49 00:05:27,510 --> 00:05:33,540 off high is 50 00:05:39,340 --> 00:05:45,340 news single quotes just quotes in space. 51 00:05:45,810 --> 00:05:50,670 Plus the name of the variable which is count 52 00:05:56,080 --> 00:05:57,160 OK. 53 00:05:57,400 --> 00:06:06,460 And so I am saying here that as long as this condition is true it is variable here is less than or equal 54 00:06:06,610 --> 00:06:08,950 to four split. 55 00:06:08,970 --> 00:06:10,230 Display this message. 56 00:06:10,270 --> 00:06:20,410 The value of my eye here is kind of like a counter is where the count stands from. 57 00:06:20,950 --> 00:06:30,820 So it says that the value of eye is plus this variable here count is plus 1. 58 00:06:30,910 --> 00:06:31,790 All right. 59 00:06:31,900 --> 00:06:32,760 And then what. 60 00:06:32,920 --> 00:06:34,990 And then I'll add the count 61 00:06:38,230 --> 00:06:39,160 equals to 62 00:06:41,890 --> 00:06:48,910 count plus 1. 63 00:06:48,930 --> 00:06:50,280 So what that means. 64 00:06:50,280 --> 00:06:54,760 This count. 65 00:06:55,170 --> 00:06:58,220 What I means each time the. 66 00:06:58,220 --> 00:07:05,390 You click on the button it will increase the Count to 1 2 2 2 3 to 4. 67 00:07:05,580 --> 00:07:11,610 Once as long as it count each time we click the button as long as the count is less than or equal to 68 00:07:11,610 --> 00:07:12,470 4. 69 00:07:12,540 --> 00:07:16,560 This code here will continue to run by the moment. 70 00:07:16,560 --> 00:07:20,890 It is greater than 4. 71 00:07:20,970 --> 00:07:23,620 The code will stop running. 72 00:07:23,730 --> 00:07:28,290 OK so let's let's test this out so I click on Start 73 00:07:31,680 --> 00:07:32,490 and wait for the code. 74 00:07:32,490 --> 00:07:34,060 So this is a button. 75 00:07:34,110 --> 00:07:40,090 So if I click the number I should increase does the value of eyes 1. 76 00:07:40,110 --> 00:07:42,020 That's it for sale. 77 00:07:42,020 --> 00:07:46,590 Kagan it goes and two three four. 78 00:07:47,810 --> 00:07:51,020 It will not exceed four. 79 00:07:51,020 --> 00:07:51,870 That is the code. 80 00:07:51,860 --> 00:07:52,500 Stop running. 81 00:07:52,520 --> 00:07:55,400 So the code stops when he gets to four. 82 00:07:55,440 --> 00:07:57,820 That's what a wild look does. 83 00:07:57,860 --> 00:08:02,030 It will continue to execute a block of code in this case. 84 00:08:02,030 --> 00:08:03,110 This is a block of code. 85 00:08:03,110 --> 00:08:11,690 It will continue to execute this as long as the condition set inside the WoW is met. 86 00:08:11,700 --> 00:08:18,180 So this is a condition I've set I've said as long as this variable count is less than or equal to four 87 00:08:18,720 --> 00:08:21,300 continue to execute this code. 88 00:08:21,300 --> 00:08:21,830 All right. 89 00:08:21,850 --> 00:08:27,500 Well that's basically how you implement a while. 90 00:08:28,610 --> 00:08:30,740 Thank you so much for watching. 91 00:08:30,740 --> 00:08:31,550 Take care. 92 00:08:31,550 --> 00:08:32,430 Bye for now.