Welcome again! In this tutorial, we will learn the DO-WHILE loop.
Its also called the DO-WHILE statement. You can choose to call it a loop or a statement.
The base is similar to the WHILE loop, although the condition is checked at the END of the loop as opposed to the START.
We have DO, our block with the curly brackets, and WHILE at the end. Then a condition in here. So this is the condition.
Now I'm going to type a little program - I want the numbers to increment each time and echo on each line as I've done in my WHILE loop.
Now the condition - when the number reaches 10, I want a variable called name, to be changed to another name in which the loop will stop.
I'll type num = 1 to start with.
Then I'll type my name is Alex.
The condition of the loop I want is - while the name = Alex.
As long as the name=Alex this will loop. So somewhere we need to say on a specific condition - change the name to Billy and then the loop won't continue any more because the name is not equal to Alex.
Now, we will include an IF statement inside the DO loop. Remember you can put IF statements inside IF statements IF statements inside loops loops inside loops and there's really no limit to what you do. As long as your code works and flows properly and doesn't produce infinite values, you will be fine.
Now what we type is DO.
Firstly, echo out the value of the number.
You can concatenate that with a brief HTML code to break the line.
Here I type num++ which is the same as num +1
Then my IF statement - If num is greater than or equal to 10 then no echo.
I want to change the name to Billy.
Let me recap. Remember, I'm not using curly brackets here because I have one line of code that needs to be executed in the block after the IF statement
Therefore I just need one line code because it looks neat.
So, let me recap what I've done. I've got the number set to 1.
This is my number variable, this can increment and can be echoed out to the user.
I've got my name set to Alex.
We start our DO.
The name is still Alex.
There's no condition so this will run regardless of anything
We increment it by 1 which will equal 2
Now, we'll say if the number which is currently 2 is bigger than or equal to 10, (which it isn't) then continue through this
It isn't. So skip this. It'll go on to say name = Alex. And then go back to the top.
This will still be 2.That means the loop's stuck at that block of code.
It'll echo out 2
It'll add one and say 3
And then it'll say, is 3 bigger than or equal to 10.
Now, it's not.
So, the name is not changed to Billy instead it'll carry on with the rest of our code.
The name's still Alex
So, the loop continues on. In this case it will go on until it reaches 10, but 9 will be echoed out to the user
Now num will become 10
The IF condition will be True.
The name would be set to Billy and in the while condition it doesn't equal Alex.So the WHILE loop will stop and the code down here will continue.
So lets execute this code. Do our loop. Click on that.
OK, we've got 1 2 3 all the way up to 9.
Obviously, our condition has been met. Our name has changed to Billy. Our name doesn't equal Alex anymore.
So, our loop here has stopped.
Now change the IF to 11 or you can change num to 0
Now this won't work and you'll see why
We've got 0 to 9
The reason being is your starting number
What this will do is, like I said before, it'll echo out the current number, then change it to an increment of 1 and then it'll compare it in the IF statement.
So, you're comparing what you can't see.
If you change this to 11, you'll compare it to 11, then change it to Billy and then it'll end the loop.
We never see the value of 11, it's only an inside comparison.
If we refresh this, we can see there's 1 to 10 now.
That is basically the DO-WHILE loop. Even though they are pretty similar,the Do-WHILE loop is more useful than the WHILE loop when you run into a programming sort of logic. It may be more useful in some cases.
So, practice this, try and enter some values. Also, try to recreate the program I've just created.
There will be more tutorials on loops shortly So keep watching.
This is Anoushka dubbing for the Spoken Tutorial Project.