OK, let's just looping.
So what we're going to do is a little example is we're going to have a program that can draw lines and
they can draw lines either horizontally, vertically or diagonally.
And the way we want to do this, and that's just demonstrating in code is a vertical line would look
something like this.
So it's just a series of characters going down the screen while the horizontal line would look something
like this.
So a series of characters coming across the screen and then a diagonal line would look like this one
and down.
Over one.
Down, you know, over.
Two spaces.
And then over three spaces and so on to draw a diagonal line, so we have our three choices and we can
say how long we want the line to be within reason.
So we want to do is to first of all, it's like our Frys example.
We're going to ask, how many do you want a vertical and horizontal line or diagonal and then how many
how long do you want it to be in length being the number of characters?
So how do we do that?
Let's start with a prompt and an answer.
We'll say let trade equals prompt.
What type of line and what do we do with the Fry's example and say, let's see each
horizontal, the vertical, the diagonal.
So we're going to prompt for that and then we will also prompt for how long you want it to be.
What does the tribute to LVM equals prompt?
How long do you want to our line and we won't bother you right now.
I could type in one million and that would be a little annoying.
But we won't bother to check for it now.
And then we'll say, OK, depending on whether it's HVAD, we're going to do something a little bit
different.
So let's say let's do a switch like we did or say switch on the type that to upper.
And then we'll say Case
H, we're going to do something.
But we're going to end it with a break case study and we're going to do something with a break case.
The we're going to do something.
We're going it with a break.
Default.
And just to clarify, we use the prompt to get information from the user, but we're going to still
really use council dialogue in this program to send information back out.
So we'll say, counsel, that if I can use a consultant error, I'm not really sure what kind of big
difference it makes, but we'll say cancel that error and we will say
you did not pick a valid line type.
All right, so we our choices.
So for right now, let's just see if it works.
And so we'll just do some consider logs,
horizontal line.
And so this could be a vertical line
and this was going to be a diagonal line.
All right, let's run this.
And we need to open up the death tolls, so we'll run a world with that line will say H how long you
want to be 12 horizontal line.
OK, looks like it's working.
We can check the other possibilities.
So let's do our horizontal line to start with.
And what we do is use a for loop and we want it to be this length.
Now, one thing, when we enter this prompt, we're going to get a string.
And so the string might be the string 23.
We have to convert it to a number.
So let's convert it to a number before we get any further down here and say let let's call this Elián
String and we'll said that we're not going to change it.
So let's make it a token one equals.
And then we can say we use a method on no, we can say no that pass it.
And what that does is says send a string and we'll give you a number back.
And I'm not sure right offhand what it does if you send us something that's not a number.
But for right now, we'll pretend like we're always going to set a number of songs and strength.
And if you go back to one of the first examples, the type of loan string would be string and then the
type of Elian would be no.
All right.
So we want to do in here then.
It's a horizontal line.
So let's say for what I and traditionally people use I in for loops.
I'm not sure why maybe for index or something like that.
So for I equals zero and we want to do it the length times.
So we'll say I less than Leon I was plus.
OK, and then also want to do is to we can't say cos of that log and then do it multiple times because
every time you consider a log it goes down to the next line.
So we're going to need to do is, is build this result will build a string and then I'll put it in the
end.
So we'll say let.
Blind string equals an empty string.
So then we will say a line string, and if you recall, you can use the carbonation operator, which
is a plus sign, but you can say plus equals to add on to a string.
So I'll just add plus equals star.
And then at the end it will say carful, that log
line string.
All right.
So let's take a look at this.
We only have the horizontal workings, so let's let's test that and H will say 22 and then we go horizontal
line.
All right, so now we have to do the same thing except with the vertical line.
So let's do more or less the same thing.
Now, let me tell you one thing that hasn't really been covered yet, which is that there are nonprofitable
characters are non visible characters, and one of them is called a new line.
In the way that you put it in a new line is it's just added to our line string, just as an example.
Why is our horizontal line, as you put it, in a backslash end?
And that's the new line.
So if I put in a few backslash ends, we'll see if we run this example.
It's going to put us in lines before we get to our I'm messing up with calling things the same thing,
but it's go put in some new lines before we get to the line that we're trying to output.
So also horizontal.
Twenty two.
So you see, it went down a few lines before it got there.
So with the vertical line, we wanted the exact same thing.
We want to, but we want to say start new lines, start new lines, start new line.
And that way it's going to go down instead of a cross.
So here we're essentially going to repeat this, except that we're going to put in the new line.
So why don't we go copy this and we're going to cancel that.
Load it out.
And then we're going to start a new line and then each one that adds to it, it's going to go down instead
of across.
So let's take a look at that.
That's the vertical run.
So say we all say 12 and we go so good that instead of a cross now we can mix this up and do the diagonal
line.
It's going to be just a little bit trickier because it's going to have to get down and then it's going
to have to go across a certain number before we can put it out the star.
So we need to keep track of how many rows, which is the eye, and do that number.
So let's copy this for a loop again.
And back to the beginning, so the follow up is going to cause us to go down, but we also want to go
across that number.
So we're going to need a nested for loop.
So we'll say for Jay and Jay is going to start at zero.
But each time, you know, we're going to be going down rows.
So each time is going to go down one over one down to over two, down three over three.
So we need to put in the very first time we're going to go over zero because it could be on the left
border and the next time it's going to need to go over one.
So that corresponds to the value of so we start to set I instead of instead of zero and then we're going
to need to go that number over.
So we want to go over until J.
Equals J plus I think or while it's less than that, what J is less than J plus I j plus plus and we'll
do a little bit of debugging to show how this works.
So again, we want to do this, this line string.
Well, first of all, we're going to add spaces to the line because we're going to go over that amount
of outside line string plus equals a space.
And so it's going to push it over until it gets to where it's ready to stop.
And then we're going to add the star and the backslash at.
All right, so let's see what this does may maybe may not work, but we'll try it so diagonal, I will
say five to make it short and did absolutely nothing.
Uh, we need to put in our counsel.
That reminds.
So I see what I'm doing wrong here, which is that this case less than J.
Plus I well, J is going to keep going up, so J.
Plus is going to keep going up.
So it's never going to get there.
So it's actually I times to I believe so.
Let's try that.
OK, let's run this diagonal five.
I was told that a vertical line, so the question is why?
So let's introduce debugging and chrome, which can help us discover what's wrong.
So what we do is I have my chrome open up here and you can see that there's a tad sources and we have
our source code in here.
And so what we do is find this position.
And I'm going to go over here to the line 24 and do a little click here, and that's going to set a
break in the code.
So let's run.
Hello, world again.
It's going to say, do you want diagonal five that's going to get down here and it's going to pause
in the code here.
And I'm going to make this extra big so it you a little bit better.
So this is different windows.
We have what's called call stack, which we don't worry about.
And then there's local variables into that.
Elian is five.
Alien string is string five.
Our line string is a string.
This is undefined, which won't worry about in our type is the.
And you see we have one breakpoint to find this mother things down here too.
But we're really interested in what's going on with these local variables.
Now you can step in here and you see that the step over the next function call is F ten step into and
step out of.
But we're really right now interested in just this next function call.
So if I click this, it's going to step down a line and it's kind of actually working its way across
the floor right now, says checking the condition.
And now it's coming out of the next slide.
So our eye is zero and you see that it's defining blocks that saying, OK, you got a block, I zero
your local variables.
Are this because they're outside of this block.
So I'm using the F ten to step because it's a little more convenient.
So then our eye is zero or eight times to zero.
RJ is also zero.
Right, because it's going to initialize jaja zero and it's going to say while J is less than eight
times two.
So that's not it is what we want because we don't want to go for any spaces.
So it's going to go over and it's not going to go into that loop at all.
It's going to put in the star all right.
Now or is one and it's because said what J equals one and continue while J is less than eight times
two, which is two.
So it should give us one space, oh, you know what, I could see what I did wrong here and use the
double cross instead of a plus equals.
So let's go back and change this and then we'll step through it again.
So six plus equals so safe,
just needed this this resume, script, execution, know, reload,
so we'll do our run Hello world again.
This kind of say diagonal will say five stops down here.
So now we'll step in to it.
See, our eye is zero.
We don't get a J because the first one doesn't get one.
So we're going to output our line string.
We're going to add star backslash into it.
And you can see it's put a little right or left arrow in here, which is just the symbol for a new line.
So we got to star in a new line and then we get back up here, our eye is now going to be one and our
Jay is going to get the value of one and we're going to add a space to it.
So you can see in our line stringers of space and Jay has the value now to and that is no longer less
than three times to, which is also to just kind of exit and add the star new line to it.
So you can see over here now we have our star new line, Space Star, New Line.
So we continue on.
And we had a space, and this time because our eye is three, we can go one more in another space.
And our star, so you see it's adding one more space each time.
So now it's added more spaces and I'm just going to let it go and click over the console and you can
see we to our diagonal line.
The final thing I want to do with this program is a lot to be executed more than once.
And of course, that's perfect for looping.
So in here, we want to start with the concept that clear always and we know we want to do this at least
once.
So we start with the do, we're just going to say do left curly brace.
So we do it once.
And then down here we're going to say a while and we have to put a condition in there and we have to
finish this with a curly brace as well.
Let's do a reformat so we have to have a wall down here.
The wall does end with a semicolon.
So what's the while?
Well, the wall is we're just going to prompt the user.
We're going to say, let me see.
Twenty four continue equals
confirm
draw another line question mark
and got this one line to load.
Can't be between the wall and the curly brace.
And we're going to have a problem of continuing here because of scoping our continue will not be in
scope, so we're going to find it outside of the loop.
So I'm going to go up here outside the loop and I'm going to say let's continue equals false.
And we'll put this in here, and so this is just going to allow us to keep drawing lines while they
confirm that they want to do it.
So let's go back to our program run hello world with Llamosa Horizontal or say 55 and draws our line
55.
Draw another line.
Sure.
Still vertical and we'll say 22 on the line.
OK, and in this case, say diagonal.
And I'm going to go back to our source and end this breakpoints.
So I'm just going to.
Right.
Click on it and say remove all breakpoints.
And back to the council.
How long do you want your line was, say, 22 as well and just cross our diagonal line, 22 line.
OK, well, that's our introduction to looping and how to do things multiple times, whether it's with
a for loop a while loop or do while loop.