PHP-and-MySQL/C2/Loops-Do-While-Statement/English-timed
From Script | Spoken-Tutorial
Time | Narration |
---|---|
00:00 | Welcome again! In this tutorial, we will learn the DO-WHILE loop. |
00:05 | Its also called the DO-WHILE statement. You can choose to call it a loop or a statement. |
00:12 | The base is similar to the WHILE loop, although the condition is checked at the END of the loop as opposed to the START. |
00:20 | We have DO, our block with the curly brackets, and WHILE at the end. Then a condition in here. So this is the condition. |
00:29 | 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. |
00:41 | 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. |
01:00 | I'll type num = 1 to start with. |
01:04 | Then I'll type my name is Alex. |
01:09 | The condition of the loop I want is - while the name = Alex. |
01:15 | 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. |
01:31 | Now, we will include an IF statement inside the DO loop. Remember you can put
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. |
01:55 | Now what we type is DO. |
01:57 | Firstly, echo out the value of the number. |
02:00 | You can concatenate that with a brief HTML code to break the line. |
02:05 | Here I type num++ which is the same as num +1 |
02:14 | Then my IF statement - If num is greater than or equal to 10 then no echo. |
02:26 | I want to change the name to Billy. |
02:30 | 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 |
02:42 | Therefore I just need one line code because it looks neat. |
02:46 | So, let me recap what I've done. I've got the number set to 1. |
02:51 | This is my number variable, this can increment and can be echoed out to the user. |
02:57 | I've got my name set to Alex. |
03:00 | We start our DO. |
03:02 | The name is still Alex. |
03:04 | There's no condition so this will run regardless of anything |
03;07 | So we echo out the number which is 1 |
03:10 | We increment it by 1 which will equal 2 |
03:14 | 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 |
03:26 | It isn't. So skip this. It'll go on to say name = Alex. And then go back to the top. |
03:34 | This will still be 2.That means the loop's stuck at that block of code. |
03:41 | It'll echo out 2 |
03:43 | It'll add one and say 3 |
03:46 | And then it'll say, is 3 bigger than or equal to 10. |
03:51 | Now, it's not. |
03:52 | So, the name is not changed to Billy instead it'll carry on with the rest of our code. |
03:58 | The name's still Alex |
04:00 | 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 |
04:09 | Now num will become 10 |
04:11 | The IF condition will be True. |
04:13 | 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. |
04:25 | So lets execute this code. Do our loop. Click on that. |
04:31 | OK, we've got 1 2 3 all the way up to 9. |
04:35 | Obviously, our condition has been met. Our name has changed to Billy. Our name doesn't equal Alex anymore. |
04:43 | So, our loop here has stopped. |
04:45 | Now change the IF to 11 or you can change num to 0 |
04:50 | Now this won't work and you'll see why |
04:54 | We've got 0 to 9 |
04:57 | The reason being is your starting number |
05:02 | 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. |
05:13 | So, you're comparing what you can't see. |
05:16 | If you change this to 11, you'll compare it to 11, then change it to Billy and then it'll end the loop. |
05:23 | We never see the value of 11, it's only an inside comparison. |
05:27 | If we refresh this, we can see there's 1 to 10 now. |
05:31 | 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. |
05:44 | So, practice this, try and enter some values. Also, try to recreate the program I've just created. |
05:52 | There will be more tutorials on loops shortly So keep watching. |
05:56 | This is Anoushka dubbing for the Spoken Tutorial Project. |