Hello and welcome to this lecture on while loop while loops are basically statements or code that executes
based on a certain condition.
So as long as the condition is true the code will continue to execute the code will only stop when that
condition becomes false.
So we will illustrate in this lecture someone to create a new project.
So I just go file new project and give it a Windows form on our call is loops.
So I change the default name from Windows Forms to loops
and click Okay.
What I'm gonna do I'm just gonna add a button to this form here.
So I'll go to view to view the tool box go to box and just drag it button in the
OK so to see the code behind the form you can click on View
you click on the form first then you click on view make sure the form is clicked on and then you select
code and to show you the code behind the form.
So let's go back to the form design and this song.
Want a double click on the button so it creates is top of the of an event handler.
Okay so we've got a stop here of a button click event handler.
So in between these curly braces is will write the code to handle the Click event for this button.
So we are going to write a code that we lost trade.
The while loop so the statement in the call the code will continually execute it will.
That's why it's called a loop.
It will keep running that code as long as the condition is true so it will execute the code inside the
block this block.
This is a block.
It will continue to execute that code as long as the condition is true.
It will only stop when that condition becomes false.
So we are going to write a code some code here that will be executed to respond to this click event
handler.
All right.
So this is done by illustration of the while loop.
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.
So we start with the type which is an integer and I'm going to call the variable count when I give it
a value close to 1 right.
So that's a variable that's a name.
And that's the value.
Okay so to set up a while loop you need to type in the word while
which indicates it's a while loop you say while the count which is a variable here
something while these count
is less than or equal to for
so I'd say while this count which is this variable is less than or equal to 4 I need you to execute
this code.
So the code I wanted to execute I need to specify the code inside curly braces.
Just like you would do with an if statement.
So inside this curly braces here is where I will define the code that will execute as long as this condition
here is true.
So I'm going want it to display a message so I'm going to use a message box.
So typing message box and I'm going to attach a show method so it will show the message and inside the
parentheses I need to specify the message so I'll say as long as that condition is true I'll see the
value
off high is
news single quotes just quotes in space.
Plus the name of the variable which is count
OK.
And so I am saying here that as long as this condition is true it is variable here is less than or equal
to four split.
Display this message.
The value of my eye here is kind of like a counter is where the count stands from.
So it says that the value of eye is plus this variable here count is plus 1.
All right.
And then what.
And then I'll add the count
equals to
count plus 1.
So what that means.
This count.
What I means each time the.
You click on the button it will increase the Count to 1 2 2 2 3 to 4.
Once as long as it count each time we click the button as long as the count is less than or equal to
4.
This code here will continue to run by the moment.
It is greater than 4.
The code will stop running.
OK so let's let's test this out so I click on Start
and wait for the code.
So this is a button.
So if I click the number I should increase does the value of eyes 1.
That's it for sale.
Kagan it goes and two three four.
It will not exceed four.
That is the code.
Stop running.
So the code stops when he gets to four.
That's what a wild look does.
It will continue to execute a block of code in this case.
This is a block of code.
It will continue to execute this as long as the condition set inside the WoW is met.
So this is a condition I've set I've said as long as this variable count is less than or equal to four
continue to execute this code.
All right.
Well that's basically how you implement a while.
Thank you so much for watching.
Take care.
Bye for now.