make money free online
star business online
12:16 AM

while and for structure

Posted by Business

online business said this structure can help us write code to be simple,
The while structure provides a way to repetitively loop through a statement block.
The number of times the statement block is executed depends on the total times the
expression evaluates to true. The general form of the while loop is:

while (expression) :
statement block
endwhile;

Let's consider an example of the computation of n-factorial (n!), where n = 5:

$n = 5;
$ncopy = $n;
$factorial = 1; // set initial factorial value
while ($n > 0) :
$factorial = $n * $factorial;
$n-; // decrement $n by 1
endwhile;

print "The factorial of $ncopy is $factorial.";

how about for structure, online business said The for loop is simply an alternative means
for specifying the duration of iterative loops.

It differs from the while loop only in the fact that the iterative value is updated
in the statement itself instead of from somewhere in the statement block. As is the
case with the while loop, the looping will continue as long as the condition being
evaluated holds true. The general form of the for construct is:

for (initialization; condition; increment) {
statement block
}

This example illustrates the basic usage of the for loop:

for ($i = 10; $i <= 100; $i+=10) :
print "\$i = $i
"; // escaping backslash to suppress
// conversion of $i variable.
endfor;
which results in:

$i = 10
$i = 20
$i = 30
$i = 40
$i = 50
$i = 60
$i = 70
$i = 80
$i = 90
$i = 100

0 comments:

Post a Comment