In this video we'll be looking at repetition statements in C sharp.
I have already created the project and this time the project's name is repetition statements and I have
already set up the comments to guide what we will be looking at.
You may pause a video here and go ahead and create the project and set up your code file to look at
mine.
Now we've already reviewed two cons control structures.
We looked at sequence where that is the order in which the code will execute.
So from top to bottom the way you write it the way it will be executed.
Then we looked at selection where we get to a situation where we need to compare two values and then
take an action based on the outcome of that comparison.
And that selection more making a decision.
So no we're going to look at how we repeat an action without having to type the line more than once.
So in our previous example where we were looking at selection and I'm just going to go over to my condition
statements project and click on that program not see us.
We printed the same line four times.
We were literally wrote this line four times.
Now there is a way that I can repeat this action without needing to repeat the line because no if I
wanted to change the sentence then I have to manually go in and change it for different times whereas
if I could repeat printing this sentence four times without changing the sentence or rather repeated
four times.
But then I need to change a sentence then I would only need to change a sentence once but it would still
go four times.
So that's the advantage that loops bring to us the ability to repeat an action.
So you see that I have outlined three types of rules that we'll be looking at today.
There there is a fourth and I think there is also a fifth but then for no I'm just looking at these
three because these three are mainstays and they're in every single language that you'll ever do.
And the other one kind of has special applications which we'll be looking at later on.
So the for loop is what we call a counter control loop.
So counter controlled meaning I am setting the number of times that I want this repetition for.
So if I wanted something to happen four or five times then I can specify that I want to type in five
times no fewer and no more.
So let us look at the syntax for a for loop.
So it starts off with the keyword for and then we open parentheses and then inside of the parentheses
we have a few things that we need to do one we need to define what we call the counter so a counter
is going to keep track of the number of times it is being executed.
So we need to me at the column to have a base value.
And I'm just going to say I I short for iteration does that really matter.
You're going to variable can have any name you would call it count.
But just make sure you have a variable there.
So I'm calling it I so into I is equal to zero.
So I'm initializing my counter to something that is very basic very low in value and I'm starting at
zero.
Then I have a semicolon then I see I less then and then I'm going to give it the value that it should
always run to be less done.
So here's where it can get tricky and I just I'm going to try my best to explain it so that you understand
why it's going to be written this way.
So I would just say I.
Which is going to have an initial value of zero less than five and then semicolon again and then I close
plus.
Now let me explain what's happening here.
So I have a call center and I'm initializing it to zero.
Then I have a condition where I'm seeing repeat as long as this condition is what as long as it's met
then continue to repeat yourself.
And then I have this section which is the incrementing seeing that the counter variable is counting
how many times it runs.
So I wanted to run five times on the first thrown it will have a value 0 0 then it will do the comparisons
is 0 is less than 5.
Okay then it runs and then it's increments.
So no I is 1 then it compares is 1 less than 5.
It is then it runs again and then it will do that and do that until we get to I being equal to five
and then it will take a five is less than five which we all know is not true.
Five is not less than five five is equal to five.
So then it will exit the loop.
Now you're probably wondering okay.
So why the zero and why compared to let's say less than five.
Well if you put your hand all right.
No and do the math and you count zero.
That's one finger.
One two three four.
Then you would have counted all five fingers on your hand.
So it's typical practice to actually start off your column to zero and then just let it that the com
pirates are on it until it is less than the number that you wish to stop at.
Right.
The alternative would be if I started this at one then I would have to change my condition to see that
I should be less than or equal to 5.
So we would have looked at the less than or equal.
Very very briefly in the selection section.
But what I'm trying to point out is that typically if you look at any other example if you pause this
video and go and google a for loop example you're going to see more often than not that the initialize
the contour to zero and then they made the condition run until it is less than the intended number of
executions.
So it is zero then turns to one that it turns into it and it turns to three then it turns the four at
the point it turns the four the loop would have executed five times.
So when it becomes five it does this comparison I realize that this is not violated.
And then it will exit the loop.
So that just part one off writing are for loop.
So then after we do that then we need our braces so you notice that curly braces are like a mean state.
Functions need them if statements need them.
I know we see that loops need them.
Right.
So whatever we write inside of the curly braces would constitute the action that is being done each
time.
So let's just say I want it to print I am a winner five times so I want to say console Dot.
Right.
Line right line and then that takes them writing to the screen is I.
Winner with an exclamation sign in case the all caps wasn't clear enough.
All right so I am a winner and I want that printed five times to the screen so remember that when in
the early example I did it manually by writing the line multiple times.
This time I'm writing it once but I wanted to print out five times so I'm going to just switch my starter
project quickly to repetition statements and then we're going to look at this and see if how many times
is going to print.
All right so then we see I am a winner being printed five times so this was the time he got printed
while I was 0 and then it went around.
So did the action then it hit the current to brace.
Then he came back to 1 increments.
So he did and I plus plus it incremented from zero to one and then he did the comparison to see is I
still less than five.
It was so rare and again and then it did the same thing came back incremented.
So no I was too.
And then that comparison happened.
I realize it was less than five and then he came back and then it went to three and then it went to
four.
And then when it hit the four and then it incremented and no I had a value of five.
It did the comparison and five is not less than five.
So this this condition was not violated.
So it came back to the curly brace and then exited.
So whatever else would've been on the here and let's just say I said to console that right line.
And then this is a nice cool way to get like some space.
So if you want like a blank space between two lines you know a console.
Right.
Line I wanted empty space again just a console.
Right.
Line and it gives you a blank space right.
And I just duplicated by pressing control D so just by being on the line control D and I will duplicate
the line and that I'm going to see for loop finished.
All right so let's try that again somewhere to execute this one more time and then here we see that
it executed everything and then when it exited it continued to execute the rest of the court.
So that's once again sequence.
So it won't move on until all of what it needed to do inside the loop was done which was to print this
five times to run five times and do whatever I told it to do four or five times.
And then once it finished it went on to print the blank space.
That's a empty console the right line and then print for loop finished.
Now I'm going to do a little modification to what those inside a for loop I'm went to print another
line so remember that as many commands as you want to repeat we can put as long as they're inside the
tool brace someone to console that right line.
And this time I want to print the value off the the incrementing variable or the column to write.
So this time I'm going to see a counter and I'm just going to put a call on and then I'm going to use
my interpolation and then plug in that value.
So I'm just going to see I.
All right.
So each time that the loop runs whatever value I has I am going to be printing it to the screen just
so you can see that it starts off with zero and goes up to four.
But it would ever run five times.
All right.
So let me try that again.
All right.
So now we see I am a winner and a calendar value is zero then I am a winner again.
Clinton value goes to 1 then to two then to three then to four.
So zero one two three four equals five executions.
All right.
So it's good practice to understand why we start to zero because when we get to arrays and collections
then you're going to understand that the computer actually starts counting from zero in as human beings
we tend to ignore zero and we start counting out one sure but the computer starts counting at zero.
So it's good to appreciate why we would start counting from zero.
That being said I didn't necessarily have to start from zero.
I could have started from 50 encode to 55.
And so I could have said the Coalition should start off as fifty five.
And it should execute until it hits 80.
All right.
So whatever number of times it's going to go between 55 and 80 or if you do the math and say 80 minus
fifty five whatever number you get that's the number of times it's going to execute.
All right.
So if I just do that quickly the end result of that is this line I am a winner and the columns are being
printed from fifty five which is a valued started with up on to seven to nine which means that when
it hit 80 it violated this condition and jumped out and that's pretty much it for for loop.
So this is a very simple example but I hope that you appreciate it.
So I'm just going to reset this code so that when you look at the example you see it and I'm just going
to add sample printing five times all right.
So now we have some comments to guide what this is doing.
All right.
So let's move on to the wild.
So the while loop is what we call a condition control loop and then I appended that with the expression
free chick.
So when we do the do while then you'll see what the pre vs. the post really mean.
But right now I'm just going to focus on the condition control so the condition control portion of it
means that it is going to continue executing until a condition is violated or it will continue to execute
while a condition holds true.
So with our for loop it was called into our control which means that we're just executing for a finite
number of times because we know we wanted to do five times so we specify or we rotate in a way to make
sure that it doesn't exceed five times.
However if we want something to go on and on and on and on until a particular condition is met or are
violated then we would use something like a while loop.
So the thing is know that with a while loop you can run into what we call an in finite loop where if
you write the code for the while loop and condition never gets violated it will never exit that loop.
And so it will run on and on and on.
Right.
As long as a computer is on and whatever conditions are required for it to be running out there it will
just continue to execute.
So I would just start off by showing you an example of an in finite loop with the while loop Fred.
So I want to declare a simple variable and call it and I give it to value 0 0 that I would to say a
while n is equivalent to 0 and I noticed the syntax we have the keyword while we open the parentheses.
And then we put in our condition just like we would have done with the if statement.
Right.
So the only difference between it and the statement is that instead of saying if we see a while.
All right.
So we see a while open bar indices put in the condition and then open curly braces with our actions
right.
So the way I've written this and is not going to change from zero.
So this is going to say a while and has the value or is equivalent to the value zero.
I want to do this.
Someone to write two lines and just copy existing lines.
Someone to print a blank line and then I'm going to see in while loop.
All right.
So while the loop is running it's going to print I would say in a while loop and then the blank line.
So it's going to see in side to say inside while loop and we're printing a blank line after that.
No I'm just going to comment this out quickly so that it doesn't get executed while we run this program
because no I want to focus on the while loop and you're going to notice that it's just going to continuously
print and never stop.
So let me execute initially.
And that's what it looks like.
So yes he's just printing printing printing printing printing and seeing inside while loop and you can
see that it can as a blank space you look at a scroll bar I see that it's just going on and on and on
and on and on it's never stopping.
And that's what do we call an infinite loop these infinite loops are known to break computers.
So if your computer crashes who and you do this I'm sorry.
But I have a slightly powerful machine so at least I know that this won't break my machine.
But the fact is if you're right poor code in big applications and you get this or end up with this situation
these situations are in finite loops can bring down systems.
All right.
So you want to make sure that you understand whole while loops work so that you don't make this mistake
at any point.
All right.
So let's exit that and look at how we should write while loops to make the most of it or to take advantage
of the condition why not leading to an infinite loop.
No.
Our condition is based on the fact that n is equivalent to zero.
So it's going to run away.
And sorry is equivalent to zero.
So what if I change this and I said something like While n is less than five.
All right.
So while the value of end is less than five I wanted to do this.
No based on the way it's written this will still be an infinite loop because any 0 0 is less than 5.
And I'm not doing anything to change the value of n.
And so it's going to always be less than five until the continent executes.
So that means the one thing I can do to make sure that this gets violated eventually is to make sure
that I am changing the value or putting something in place that the value changes at some point.
And this comparison will get broken or this condition will be met or violated or other at some point.
That is convenient to how we want our program to work.
So I want this to print five times.
Yes we accomplished something that that with a for loop but I'm just showing you that I can accomplish
something that that with the while loop but the the path to doing so is going to be very different.
So I need to know make sure that I am changing the value of an to be something that eventually will
exceed a value that makes this condition true.
So I'm just going to add a third line to inside my while loop and then I'm going to see an is equal
to N close 5 or sorry n is equal to any plus 1.
So when I say n is equal to N plus 1 remember we discussed that this could be shortened into just seeing
n flows equal one.
So no it's going to be adding 1 to itself each time.
Right.
So it's going not by a one until eventually it will execute and n is going to have a value that is not
one to make.
That's not one to be less than five.
So our while loop will exit.
So let's this.
That's 0.
So I mean execute.
And here we see that's literally printed the right number of times.
So just like with the for loop where we started off a zero and went to it was less than five.
We started off with n being zero and then we're just checking on the condition that n is less than five.
But there is nothing in the loop or in the structure of the loop that at least not naturally that will
help us to change the value of.
And so we had to do that manually by putting an accumulation so we'll call this an accumulator.
When you make a variable increment by itself plus another value.
Right.
So when I said n equals N plus 1 that's making it an accumulator are plus equal.
So now I'm accumulating value each time it runs and then eventually we'll be will meet this condition.
Now this is a very simple and not very practical example I mean there are situations you may use this
but a more useful way for using the while loop is to see compare user input and let that user input
violate the condition and continue to do this until the user violates the condition and the loop will
exit.
All right.
So so let's take your experience with an ABM for example on ABM will probably always ask you do you
want to continue if you say yes it continues if it says no.
And then it repeats the same steps on as long as you keep on seeing yes.
All right.
So literally all they have is really a while loop that is allowing you to do carry out operations and
then you can indicate when you want it to stop.
And then when you give it the condition that C is stopped then you will say OK that condition has been
violated.
Let me move on.
So I'm going to modify this and I'm going to see console the right line and I want to tell the user
input no all right.
And I'm going to say zero to four.
So I'm telling the user to input some number between 0 to 4 and then I'm going to do I read line where
I'm going to see and is equal to convert to sorry spell converts wrong convert to in thirty two and
then I'm going to say a console dot read line so I'm accepting user input and I'm not going to increment
because I have the user determining the value of and each time right and I'm going to take oh this console
that right line Well let me put it back and then I'm going to print the screen you entered and I want
to see you enter that number.
All right.
So let me just put on my dollar sign interpolation.
So what this loop is expected to do is that or it's going to from the user to enter a number if I enter
0 then if I enter 0 then it will print to you entered 0 and then it's going to come back and it's going
to check the condition is 0 less than 5.
No it's not so it's going to see in turn number again and then it's going to continue doing that until
I put in a number that clearly is not less than five or will clearly violate this condition.
So let's try that so input a number and I put in three press Enter you In you entered three and then
it's prompting me again.
So I entered three.
It printed that I entered three and then it came.
Did the comparison realize that this condition is still true because three is less than five.
So it went ahead and asked me for another number.
If I put you in for it we'll do it again.
If I put two in one if I put you negative five as long as a number is not let alone as the number is
less than five then it will continue to do this operation.
If I put in ten it will exit.
All right so let me put in a proper statement to say it has exited.
So I'm just taking this previously written one on commenting it I see keyboard chakras just make it
easy to maneuver right.
So while loop finished all right.
So let's try that again.
So we have zero to four output zero.
I put five and then oil.
OK.
So I entered five.
I did that prematurely but that's fine.
So I entered zero and it just said OK zero is this and five from Tim again.
Then I entered five.
It came to the comparison five is not less than five.
And so it just went down to the square to brace and then continued with the execution where it just
said while loop finished.
All right.
So that's basically how we can use while lose because once again it's going to run against a condition
and we want to make sure that this condition can get violated and legitimately so because if we don't
do that then all the operations that the while loop is repeating will be repeated until that computer
shuts off.
And that can lead to lead to some buffer or a memory overload and all sorts of things that we don't
want.
So when the writing condition controls loops make sure that the condition can be violated at some point
and legitimately so.
Now we move on to our do while and I did say that I would mention the pre vs. post.
So the reason we call The while loop the pre check is that is going to check this condition before it
executes.
So if I initialized n to have a value of five or let's say 10 then the first thing is going to do is
check is this condition true.
That's the pre check is the condition true is if any 10 and 10 is not less than 5 then nothing here
will get done it will just move on to C while loop finished.
So that's a pre check the post check will do it before.
So it will do the action and then check the condition.
Right.
So let me just write the do while and I'm going to do the same operation inside to do.
But then we'll see the difference in the execution.
So the syntax for our do is the word do the or we open and close the curly braces and then on the close
currently brace you have to see while and then you put in your condition check.
So the wild part looks just like the regular while.
So I'm just going to copy this and pasted down here it looks the same way right.
So the only difference is that we see a while and then opened the curly brace the do while we say do
this action do whatever is inside the curly braces and then check the condition afterwards.
And then this one requires a semicolon at the end of it.
So that's where it can get and can get kind of tricky because then you see that oh no semicolon and
semicolon for this one on there and there.
So reinvention is just practice that will make you get used to where to put the semicolons and where
we're not to write and visual studio is always ready to give you that encouraging red line to remind
you that hey there's something missing here.
So then it's telling you semicolon is expected you put it there and all is well in the universe after
that.
So I'm just going to copy these actions quickly put inside the do and put that into space so we can
see exactly what's happening and then output the statement that tells us when the do while loop is finished.
So I'm not actually not want to come in this autumn winter to run it.
So the value of n is 10.
Remember that the pre check is going to check the condition if the condition is true then it will do
what it's what what we told it to do and then it will come back and check the condition again if the
condition is not true then it will not do anything.
All right.
Whereas with the door while it will do something and then check.
So that means I have the opportunity in this one to change the value of n from 10 and then it can change
whether you can violate that or it can make it true so it will just say oh this condition is true let's
do it again.
So we'll do it at least once.
So let's try that.
So I click Start I notice the first thing that's printed the screen is something to tell us to do the
while loop is finished.
So you know it just skipped the doorway as I said it just check the condition 10 less than five.
That's false.
Ah.
Don't bother with that.
Print the blank line and then give me the while loop finished.
But then the door while is asking me for the number so it's doing the action and then it's giving me
the opportunity to put in the number and then it will check the condition.
All right.
So if we want to do something at least once it's a repeatable thing well you want to make sure that
it happens at least once.
Then you want to use a do while.
All right.
So then I want to input a number and let's say I input three it will repeat.
All right.
So when I entered three on prompt then it hit the condition and three is less than five.
So it said we'll do it again and then this on with to enter five or what I've said fifty six.
That's fine.
And then entered 56 which is clearly not less than five.
And so it just went on to say while loop do while loop is finished.
So that's the only difference between the while and the do while the do does it at least once and takes
after whereas a while loop will do it will check before it does.
And then we remember that our for loop and others on Comment is before we wrap this up.
The for loop is set to run for how many times we want it to run.
That's it for petition statements in C sharp.
Remember to practice and think of scenarios where you may need them.
We're going to be using them more in our project.
But for know this is an introduction.