Saturday, 6 August 2016

PHP Introduction - Lecture #7

PHP LOOPS Contd.

Do While Loop

Hello All... welcome every one. I'm going to start PHP lectures for Very Beginners, who wants to learn PHP from very beginning. No matter if you do not have any basic programming concept. If you follow my tutorials I hope you will surely get some how good knowledge of PHP.
Today our Topic is

Do-while LOOP

The simple difference between while and do-while loop is that while loop checks the condition then it at least once executes the code but it do-while loop the block of code is executed then condition is checked. Even if the condition is false then also code will be executed at least once. Here is the simple syntax of do-while loop.
do
  {
  code to be executed;
 
}
while (condition);
Let’s have an example of do-while loop


From the above figure the result comes 2-10 even in last when it comes to 10 the condition becomes false despite of false condition it prints out to 10. Actually that’s the only difference between while and do-while that even condition becomes false at least one time code will be executed.
Description of above code
First of all we have declared a variable named `a` and assigned value of 1 after that we are saying in our code to `do` the increment by 1 in the above value of `a`, it becomes 2 then there is echo command that prints what the value is in variable `a` and it is 2 because before printing we are doing increment so it becomes 2. After that condition is checked either $a is less than 10 or not, it’s true so again it goes back to the `do` command and says to do increment by 1 and becomes 3 then it prints 3 after that it checks condition and so on.
You keep trying yourself with different conditions and examples. Hope you will get the concept.


Thanks Guys. Any thing missing or any question from this topic you can comment below, I will be trying to solve and answer your question.
Our Next topic will be `For Loop`.
Good Luck J

No comments:

Post a Comment