Website Development Prices

Search Blog

Thursday, January 14, 2016

Petlja do...while (Loop do...while)


Petlja do...while ponasa se isto kao i petlja while, a razlika je u tome da se uslov proverava na kraju, a ne na pocetku petlje. 

do 
{iskaz}
while (izraz);

Primer:

<?php

$vrednost = 1;

do {
echo $vrednost, "<br>";
$vrednost *= 2;
} while ($vrednost < 12);


?>


Sacuvajte ovaj fajl kao do-while-petlja.php.

Rezultat:

1
2
4
8


Ako bi uslov bio netacan pre pocetka petlje, petlja while se ne bi izvrsila ni jednom.

Primer 2:

<?php

$vrednost = 24;

while ($vrednost < 12) {
$vrednost *= 2;
echo $vrednost, "<br>";
}


?>

A ako koristite petlju do...while, uslov se proverava na kraju, tako da ce se prikazati 24.

Primer 3:

<?php

$vrednost = 24;

do {
echo $vrednost, "<br>";
$vrednost *= 2;
} while ($vrednost < 12);

?>

Rezultat:

24


Loop do...while acts the same as the loop while, the difference is that the condition is checked at the end, not at the beginning of the loop.

do
{statement}
while (expression);

Example:

<?php

$value = 1;

do {
echo $value, "<br>";
$value *= 2;

} while ($value < 12);

?>

Save this file as do-while-loop.php.

Result:

1
2
4
8

If the condition was incorrect before the start of the loop, the loop while would not have executed anything.

Example 2:

<?php

$value = 24;

while ($value < 12) {
$value *= 2;
echo $value, "<br>";
}

$value = 24;

?>

And if you use do...while loop, the condition is checked at the end, so 24 will be shown.

Example 3:

<?php

do {
echo $value, "<br>";
$value *= 2;

} while ($value < 12);

?>

Result:

24

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.