Learning PHP, MySQL & JavaScript with Jquery, css and html5 - 5th Edition 2018.
Eaxample exercise 4-36. Trapping division-by-zero erros using continue.
In this example we learn about the Continue Statement.
All the code have been tested with PHP Version 7.2.4 on lampp / xampp for linux mint
Here is all the code if you don't bother to read the whole blo
Eaxample exercise 4-36. Trapping division-by-zero erros using continue.
In this example we learn about the Continue Statement.
All the code have been tested with PHP Version 7.2.4 on lampp / xampp for linux mint
Here is all the code if you don't bother to read the whole blo
<?php // Learning Php, MySQL and JavaScript 5th edition // page 89-90 // Example 4-36. Trapping division-by-zero erros using continue // $j = 10; // $iterations = 0; // while ($j > -10) // { // $j--; // if ($j == 0) continue; // echo (10 / $j) . "<br>"; // } // Visual improvement =============== // $j = 10; // $iterations = 0; // while ($j > -10) // { // $j--; // $iterations++; // if ($j == 0) continue; // echo "Loop " . $iterations . " = " . (10 / $j) . "<br>"; // } // Visual improvement with curly braces and the word "loop" have been replaced with "iteration" $j = 10; $iterations = 0; while ($j > -10) { $j--; $iterations++; if ($j == 0) { echo "iteration " . $iterations . ' = $j = 0 <br>'; } else { echo "iteration " . $iterations . " = " . (10 / $j) . "<br>"; } } ?>
The original code in the book look like this
<?php $j = 10; while ($j > -10) { $j--; if ($j == 0) continue; echo (10 / $j) . "<br>"; } ?>
Browser result:
1.1111111111111
1.25
1.4285714285714
1.6666666666667
2
2.5
3.3333333333333
5
10
-10
-5
-3.3333333333333
-2.5
-2
-1.6666666666667
-1.4285714285714
-1.25
-1.1111111111111
-1
1.25
1.4285714285714
1.6666666666667
2
2.5
3.3333333333333
5
10
-10
-5
-3.3333333333333
-2.5
-2
-1.6666666666667
-1.4285714285714
-1.25
-1.1111111111111
-1
As you can see this will only give you the direct result where it will skip/continue if the $j variable is equal to 0. I like to make the browser output with some text that make the result a bit more visual understandable.
I added some simple visual text improvement
<?php
$j = 10; $iterations = 0; while ($j > -10) { $j--; $iterations++; if ($j == 0) continue; echo "Loop " . $iterations . " = " . (10 / $j) . "<br>"; }
?>
Browser result:
Loop 1 = 1.1111111111111
Loop 2 = 1.25
Loop 3 = 1.4285714285714
Loop 4 = 1.6666666666667
Loop 5 = 2
Loop 6 = 2.5
Loop 7 = 3.3333333333333
Loop 8 = 5
Loop 9 = 10
Loop 11 = -10
Loop 12 = -5
Loop 13 = -3.3333333333333
Loop 14 = -2.5
Loop 15 = -2
Loop 16 = -1.6666666666667
Loop 17 = -1.4285714285714
Loop 18 = -1.25
Loop 19 = -1.1111111111111
Loop 20 = -1
Loop 2 = 1.25
Loop 3 = 1.4285714285714
Loop 4 = 1.6666666666667
Loop 5 = 2
Loop 6 = 2.5
Loop 7 = 3.3333333333333
Loop 8 = 5
Loop 9 = 10
Loop 11 = -10
Loop 12 = -5
Loop 13 = -3.3333333333333
Loop 14 = -2.5
Loop 15 = -2
Loop 16 = -1.6666666666667
Loop 17 = -1.4285714285714
Loop 18 = -1.25
Loop 19 = -1.1111111111111
Loop 20 = -1
Here you see the result with loop/iteration number but it wont show us where the Continue Statement skiped the number 0
Here is another improment but I removed the Continue Statement in the code to visual show where the variable $j is equal to 0
<?php
$j = 10; $iterations = 0; while ($j > -10) { $j--; $interations++; if ($j == 0) { echo "Iteration " . $iterations . ' = $j = 0 <br>'; } else { echo "Iteration " . $iterations . " = " . (10 / $j) . "<br>"; } }
?>
As you can see I change the the word "Loop" to "Iteration" and at "Iteration 10" you can se where variable $j is equal to 0. I know we are using "if else" statement but it's only to show where the Continue Statement would have continue with While loop
Browser result:
Iteration 1 = 1.1111111111111
Iteration 2 = 1.25
Iteration 3 = 1.4285714285714
Iteration 4 = 1.6666666666667
Iteration 5 = 2
Iteration 6 = 2.5
Iteration 7 = 3.3333333333333
Iteration 8 = 5
Iteration 9 = 10
Iteration 10 = $j = 0
Iteration 11 = -10
Iteration 12 = -5
Iteration 13 = -3.3333333333333
Iteration 14 = -2.5
Iteration 15 = -2
Iteration 16 = -1.6666666666667
Iteration 17 = -1.4285714285714
Iteration 18 = -1.25
Iteration 19 = -1.1111111111111
Iteration 20 = -1
Iteration 2 = 1.25
Iteration 3 = 1.4285714285714
Iteration 4 = 1.6666666666667
Iteration 5 = 2
Iteration 6 = 2.5
Iteration 7 = 3.3333333333333
Iteration 8 = 5
Iteration 9 = 10
Iteration 10 = $j = 0
Iteration 11 = -10
Iteration 12 = -5
Iteration 13 = -3.3333333333333
Iteration 14 = -2.5
Iteration 15 = -2
Iteration 16 = -1.6666666666667
Iteration 17 = -1.4285714285714
Iteration 18 = -1.25
Iteration 19 = -1.1111111111111
Iteration 20 = -1
I know the whole shebang is not correct but it will give a more visualy understanding of how a Continue Statemen works. Sometime it can be hard to understand what you can't see
Ingen kommentarer:
Send en kommentar