PHP-and-MySQL/C2/Loops-Do-While-Statement/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:00 Welcome again! In this tutorial, we will learn the do-while loop.
00:05 It's 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 equals "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 if statements inside if statement

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.

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 to 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 is 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 No, it's not.So, the name is not changed to "Billy", instead it'll carry on with the rest of our code.
03:58 The name is 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, let's execute this code. "dowhileloop", 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 changes 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.

Contributors and Content Editors

Minal, PoojaMoolya, Pratik kamble, Sandhya.np14