PHP, MySQL, JavaScript & HTML5 All-in-One For Dummies by Steve Suehring & Janet Valade

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



Copyright Disclaimer:
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.