Difference between revisions of "PHP-and-MySQL/C2/Loops-Do-While-Statement/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
Line 4: | Line 4: | ||
|- | |- | ||
|00:00 | |00:00 | ||
− | |Welcome again! In this tutorial, we will learn the | + | |Welcome again! In this tutorial, we will learn the '''do-while''' loop. |
|- | |- | ||
|00:05 | |00:05 | ||
− | | | + | |It's also called the '''do-while''' statement. You can choose to call it a '''loop''' or a '''statement'''. |
|- | |- | ||
|00:12 | |00:12 | ||
− | |The base is similar to the | + | |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 | |00:20 | ||
− | |We have | + | |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 | |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 | + | |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 | |00:41 | ||
− | |Now the condition - when the number reaches 10, I want a variable called name | + | |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 | |01:00 | ||
− | |I'll type num = 1 to start with. | + | |I'll type '''num = 1''' to start with. |
|- | |- | ||
|01:04 | |01:04 | ||
− | |Then I'll type my name is Alex. | + | |Then I'll type "my name is Alex". |
|- | |- | ||
|01:09 | |01:09 | ||
− | |The condition of the loop I want is - while the name = Alex. | + | |The '''condition''' of the '''loop''' I want is - '''while''' the '''name = Alex'''. |
|- | |- | ||
|01:15 | |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. | + | |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 | |01:31 | ||
− | |Now, we will include an | + | |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 | + | * '''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. | + | 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 | |01:55 | ||
− | |Now what we type is | + | |Now what we type is '''do'''. |
|- | |- | ||
|01:57 | |01:57 | ||
− | |Firstly, echo out the value of the number. | + | |Firstly, '''echo''' out the value of the number. |
|- | |- | ||
|02:00 | |02:00 | ||
Line 50: | Line 50: | ||
|- | |- | ||
|02:05 | |02:05 | ||
− | |Here I type num++ which is the same as num +1 | + | |Here I type '''num++''' which is the same as '''num +1'''. |
|- | |- | ||
|02:14 | |02:14 | ||
− | |Then my | + | |Then my '''if''' statement - '''if num''' is greater than or equal to 10 then no '''echo'''. |
|- | |- | ||
|02:26 | |02:26 | ||
− | |I want to change the name to Billy. | + | |I want to change the name to "Billy". |
|- | |- | ||
|02:30 | |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 | + | |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 | |02:42 | ||
Line 68: | Line 68: | ||
|- | |- | ||
|02:51 | |02:51 | ||
− | |This is my number variable, this can increment and can be echoed out to the user. | + | |This is my '''number''' variable, this can increment and can be echoed out to the user. |
|- | |- | ||
|02:57 | |02:57 | ||
− | |I've got my name set to Alex. | + | |I've got my name set to "Alex". |
|- | |- | ||
|03:00 | |03:00 | ||
− | |We start our | + | |We start our '''do'''. |
|- | |- | ||
|03:02 | |03:02 | ||
− | |The name is still Alex. | + | |The name is still "Alex". |
|- | |- | ||
|03:04 | |03:04 | ||
− | |There's no condition so this will run regardless of anything | + | |There's no '''condition'''; so this will run regardless of anything. |
|- | |- | ||
|03;07 | |03;07 | ||
− | |So we echo out the number which is 1 | + | |So, we '''echo''' out the number which is 1. |
|- | |- | ||
|03:10 | |03:10 | ||
− | |We increment it by 1 which will equal 2 | + | |We increment it by 1 which will equal to 2. |
|- | |- | ||
|03:14 | |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 | + | |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 | |03:26 | ||
− | |It isn't. So skip this. It'll go on to say name = Alex. And then go back to the top. | + | |It isn't. So skip this. It'll go on to say '''name = Alex'''. And then go back to the top. |
|- | |- | ||
|03:34 | |03:34 | ||
− | |This will still be 2.That means the loop | + | |This will still be 2. That means the loop is stuck at that block of code. |
|- | |- | ||
|03:41 | |03:41 | ||
− | |It'll echo out 2 | + | |It'll '''echo''' out 2. |
|- | |- | ||
|03:43 | |03:43 | ||
− | |It'll add one and say 3 | + | |It'll add one and say 3. |
|- | |- | ||
|03:46 | |03:46 | ||
− | |And then it'll say | + | |And then it'll say: is 3 bigger than or equal to 10? |
|- | |- | ||
|03:51 | |03:51 | ||
− | | | + | |No, it's not. |
|- | |- | ||
|03:52 | |03:52 | ||
− | |So, the name is not changed to Billy instead it'll carry on with the rest of our code. | + | |So, the '''name''' is not changed to "Billy" instead it'll carry on with the rest of our code. |
|- | |- | ||
|03:58 | |03:58 | ||
− | |The name | + | |The name is still "Alex". |
|- | |- | ||
|04:00 | |04:00 | ||
− | |So, the loop continues on. In this case it will go on until it reaches 10 | + | |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 | |04:09 | ||
− | |Now num will become 10 | + | |Now '''num''' will become 10. |
|- | |- | ||
|04:11 | |04:11 | ||
− | |The | + | |The '''if''' condition will be '''True'''. |
|- | |- | ||
|04:13 | |04:13 | ||
− | |The name would be set to Billy and in the while condition it doesn't equal Alex.So the | + | |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 | |04:25 | ||
− | |So | + | |So, let's execute this code. Do our loop. Click on that. |
|- | |- | ||
|04:31 | |04:31 | ||
− | |OK, we've got 1 2 3 all the way up to 9. | + | |OK, we've got 1, 2, 3 all the way up to 9. |
|- | |- | ||
|04:35 | |04:35 | ||
− | |Obviously, our condition has been met. Our name has changed to Billy. Our name doesn't equal Alex anymore. | + | |Obviously, our '''condition''' has been met. Our '''name''' has changed to "Billy". Our name doesn't equal "Alex" anymore. |
|- | |- | ||
|04:43 | |04:43 | ||
− | |So, our loop here has stopped. | + | |So, our '''loop''' here has stopped. |
|- | |- | ||
|04:45 | |04:45 | ||
− | |Now change the | + | |Now change the '''if''' to 11 or you can change '''num''' to 0. |
|- | |- | ||
|04:50 | |04:50 | ||
− | |Now this won't work and you'll see why | + | |Now this won't work and you'll see why. |
|- | |- | ||
|04:54 | |04:54 | ||
− | |We've got 0 to 9 | + | |We've got 0 to 9. |
|- | |- | ||
|04:57 | |04:57 | ||
− | |The reason being is your starting number | + | |The reason being is your starting number. |
|- | |- | ||
|05:02 | |05:02 | ||
− | |What this will do is, like I said before, it'll echo out the current number, then | + | |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 | |05:13 | ||
Line 158: | Line 158: | ||
|- | |- | ||
|05:16 | |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. | + | |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 | |05:23 | ||
Line 164: | Line 164: | ||
|- | |- | ||
|05:27 | |05:27 | ||
− | |If we refresh this, we can see there's 1 to 10 now. | + | |If we '''refresh''' this, we can see there's 1 to 10 now. |
|- | |- | ||
|05:31 | |05:31 | ||
− | |That is basically the | + | |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 | |05:44 | ||
Line 173: | Line 173: | ||
|- | |- | ||
|05:52 | |05:52 | ||
− | |There will be more tutorials on loops shortly | + | |There will be more tutorials on loops shortly; so keep watching. |
|- | |- | ||
|05:56 | |05:56 | ||
− | |This is Anoushka dubbing for the Spoken Tutorial | + | |This is Anoushka, dubbing for the Spoken Tutorial project. |
Revision as of 10:06, 21 May 2015
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=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 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. |
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 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. 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 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. |