PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies by Steve Suehring & Janet Valade
Author:Steve Suehring & Janet Valade
Language: eng
Format: epub
Publisher: John Wiley and Sons, Inc.
Published: 2013-03-11T16:00:00+00:00
Multiply by 2
2 x 1 = 2
2 x 2 = 4
...
2 x 8 = 16
2 x 9 = 18
Multiply by 3
3 x 1 = 3
And so on.
Designing advanced for loops
The structure of a for loop is quite flexible and allows you to build loops for almost any purpose. Although the basic for loop discussed so far in this section has one statement in its starting, conditional, and increment sections, the general format allows more than one statement in each section. The general format is:
for (beginning statements; conditional statements;
ending statements)
{
block of statements;
}
The statements within a for loop have the following roles:
The beginning statements execute once at the start of the loop. They can be statements that set any needed starting values or other statements that you want to execute before your loop starts running.
The conditional statements are tested for each iteration of your loop.
The ending statements execute once at the end of the loop. They can be statements that increment your values or any other statements that you want to execute at the end of your loop.
Each statement section is separated by a semicolon (;). Each section can contain as many statements as needed, separated by commas. Any section can be empty.
The following loop has statements in all three sections:
$t = 0;
for ($i=0,$j=1;$t<=4;$i++,$j++)
{
$t = $i + $j;
echo "$t<br />";
}
In this example, $i=0 and $j=1 are the beginning statements, $t<=4 is the conditional statement, and $i++ and $j++ are the ending statements.
The output of these statements is as follows:
1
3
5
The loop is executed in the following order:
1. The beginning section containing two statements is executed.
$i is set to 0, and $j is set to 1.
2. The conditional section containing one statement is evaluated.
Is $t less than or equal to 4? Yes, so the statement is true. The loop continues to execute.
3. The statements in the statement block are executed.
$t becomes equal to $i plus $j, which is 0 + 1, which equals 1. Then $t is echoed to give the output 1.
4. The ending section containing two statements ($i++ and $j++) is executed.
Both $i and $j are incremented by 1, so $i now equals 1, and $j now equals 2.
5. The conditional section is evaluated.
Is $t less than or equal to 4? Because $t is equal to 1 at this point, the statement is true. The loop continues to execute.
6. The statements in the statement block are executed.
$t becomes equal to $i plus $j, which is 1 + 2, which equals 3. Then $t is echoed to give the output 3.
7. The ending section containing two statements ($i++ and $j++) is executed.
Both $i and $j are incremented by 1, so $i now equals 2, and $j now equals 3.
8. The conditional section is evaluated.
Is $t less than or equal to 4? Because $t now equals 3, the statement is true. The loop continues to execute.
9. The statements in the statement block are executed.
$t becomes equal to $i plus $j, which is 2 + 3, which equals 5. Then $t is echoed to give the output 5.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8264)
Azure Data and AI Architect Handbook by Olivier Mertens & Breght Van Baelen(6432)
Building Statistical Models in Python by Huy Hoang Nguyen & Paul N Adams & Stuart J Miller(6391)
Serverless Machine Learning with Amazon Redshift ML by Debu Panda & Phil Bates & Bhanu Pittampally & Sumeet Joshi(6282)
Data Wrangling on AWS by Navnit Shukla | Sankar M | Sam Palani(6055)
Driving Data Quality with Data Contracts by Andrew Jones(6020)
Learning SQL by Alan Beaulieu(5967)
Machine Learning Model Serving Patterns and Best Practices by Md Johirul Islam(5787)
Weapons of Math Destruction by Cathy O'Neil(5728)
Big Data Analysis with Python by Ivan Marin(5192)
Data Engineering with dbt by Roberto Zagni(4215)
Solidity Programming Essentials by Ritesh Modi(3855)
Time Series Analysis with Python Cookbook by Tarek A. Atwan(3704)
Pandas Cookbook by Theodore Petrou(3419)
Blockchain Basics by Daniel Drescher(3280)
Hands-On Machine Learning for Algorithmic Trading by Stefan Jansen(2893)
Feature Store for Machine Learning by Jayanth Kumar M J(2803)
Learn T-SQL Querying by Pam Lahoud & Pedro Lopes(2785)
Mastering Python for Finance by Unknown(2737)
