Hello and welcome.
In this lecture I'm going to explain how the while loop works in the Ruby programming language.
What does a a while loop do.
Basically the one on loop statement will execute repeatedly as long as the condition is true.
So it will execute a block of code as long as specified condition is true.
So the syntax for a while loop is stuck with the key word while thing is specify the condition.
Then you've got the do word which is the reserve key word in Ruby that is optional.
But while loop will cease to work even if you know you do.
And there was you specify the condition then the block of code will execute and now be the day you specify
an end for Ruby to know that you have recently finished this statement.
So we're going to see how this works.
I have got the Ruby interpreter so I was going to declare a few variables first variable is x.
I was sent to a value of one present to create and unknown a variable.
Now why.
And when I send this to 10 I press enter.
I'm going to implement the while loop now and when to say Wow.
So I'm going to specify the condition now.
While X is less down and y.
I want this to happen.
So now I have to specify what I want.
Specify the condition in the while.
Now I need to specify the could block that will execute.
So I'm going to say print.
This is what I want to happen.
Print X comma.
Quotes because I want to be a full stop after each of the line or statement and then I'll say show me
the money.
Sure mean the money
and I'm going to skip that so that he can print it from the statement in separate lines where you use
in the print command.
It doesn't leave spaces.
So if I was there and want it from the execution to be on a separate line I asked why escaping in the
n bear.
Okay now I need to specify the loop condition.
How many times I wanted to loop the big code here.
I wanted to print this.
Now how meta specify if this condition is true.
I wanted to print this X amount of time so one I'll see X..
So when you use in a loop you need to specify a counter so waste that count him from.
So I'll say a while x.
Plus thought it was 2 1.
What that means is that his time loops true.
This 13 count is one.
Well each time he goes true it will had one each time until it gets to until he becomes less than 10.
Less time.
Why.
Why is Ted was he becomes less and why the loop will stop next to I need to do it.
Typing the word.
And so what's going to happen here is that the cord is going to print numbers from zero to nine.
The condition I have said is that as long as X is less than Y it would check that condition and that
condition is true.
He checks it before the loop is started before he starts in the loop.
Then the body of the loop execute which is this bit here bit will execute them if condition is checked
again when the condition resolves is false.
The loop terminates and this time it goes to the loop.
That's why this makes this plot a quasi means one.
It's time it goes around.
It will and one add one each time until it gets to less than 10.
So you can see his printed out.
Show me the money spent at it nine times because I've said here the condition as long as X is less help
why I want it to print.
MS. Okay.
And each time it is true to check they could have told it to add one.
So that is the first time he goes round it checks the condition.
That's one in Gore's run again plus one.
He has another one.
Gore's run again unsworn until he gets to nine.
It doesn't show 10 because by 10 it will have been force for as long as a condition this true.
It will keep execute in a court.
So that's basically how the while loop works.
Thanks for watching.
And by phone.