PHP – While and do-while
The PHP while statement is used to run a code in loop over and over again. This is very useful and helps you minimize the code for rapid edition and maintenance. There are several loop statements that can be used depending on your needs
- while – repeats a block of code as long as the condition is met
- do / while – executes a block of code once, and then repeats it as long as the condition is met
- for – executes a block of code a given number of times
- foreach – executes a block of code for each element of an array
PHP while loop
Here is a simplified example of the while statement
PHP
while (expression == true) {
// run this block of code;
}
This is a practical example of the PHP while loop. We need to execute the code 10 times in a loop. Here is how we do it.
PHP
<?php
$counter = 1;
$max = 5;
while($counter <= $max) {
echo "This is number $counter loop, we have ".($max - $counter)." left.<br />";
$counter++;
}
?>
The above PHP code will print something similar to this
PHP
This is number 1 loop, we have 4 left.
This is number 2 loop, we have 3 left.
This is number 3 loop, we have 2 left.
This is number 4 loop, we have 1 left.
This is number 5 loop, we have 0 left.
How to use PHP do-while statement
Since we already learned the while syntax, the do-while is practically the same with one exception. The first loop will always execute no matter if the condition is true or false. This is great to use, or example, when you need to repeat the code operation depending on the result of the first iteration. Remember in a do-while statement the condition is evaluated after the first loop
PHP
do {
/* execute this code first and then check the condition */
} while (expression == true);
So let’s take our previous example and transform it to a do-while syntax instead of a simple while loop.
PHP
<?php
$counter = 1;
$max = 0;
do {
echo "This is number $counter loop, we have ".($max - $counter)." left.<br />";
$counter++;
} while($counter <= $max);
?>
You wold expect that the echo will not execute since the $max = 0. But this is not the case since we are checking the $max value after executing the do loop
While and do-while alternative syntax
The PHP while statements has an alternative syntax for you to use. Take a look at the next example. We are using the colon punctuation mark and the endwhile to end the syntax
PHP
<?php
$i = 1;
while ($i <= 5):
print $i;
$i++;
endwhile;
?>
On the other hand, there is NO alternative syntax for the do-while loop statement. Easy as that.
Python Example for Beginners
Two Machine Learning Fields
There are two sides to machine learning:
- Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
- Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.
Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes
Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!
Latest end-to-end Learn by Coding Recipes in Project-Based Learning:
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Google –> SETScholars