Website Development Prices

Search Blog

Thursday, December 17, 2015

Petlja for (Loop for)

for petlja u svakom prolazu izvrsava iskaz koji je u telu petlje (to moze biti i slozeni iskaz).

for(izraz1; izraz2; izraz3) iskaz

Objasnjenje: izraz1 omogucava da pripremite svoju petlju, cesto inicijalizacijom brojaca petlje (koji se jos naziva i indeks petlje), koji prati koliko je puta petlja izvrsena. Izraz se izvrsava pre izvrsenja petlje. Izraz2 sluzi za proveru. Petlja se ponavlja sve dok je njegova vrednost tacno. Ovde se obicno proverava vrednost brojaca petlje, Izraz3 se izvrsava posle izvrsenja iskaza u svakom prolazu petlje. Ovde se obicno povecava vrednost brojaca petlje. 

Primer: upotrebicemo petlju for da 3 puta odstampamo tekst. Petlja pocinje podesavanjem brojaca petlje ($loop_counter) na 0. Petlja u svakom prolazu povecava ovaj brojac, a onda proverava da li je brojac petlje prosao 3.


<?php

for ($brojac_petlje=0; $brojac_petlje < 3; $brojac_petlje++) { 
echo "Uradicu ovo tri puta.<br>";
}

?>

Ovaj fajl sacuvajte kao for-petlja.php.

Rezultat:

Uradicu ovo tri puta.
Uradicu ovo tri puta.
Uradicu ovo tri puta.


Izrazi u for petlji mogu da obradjuju i vise indeksa. Potrebno je da ih odvojite zarezom.

Primer 2: for petlja koja koristi dva indeksa petlje.

<?php

for($var1=2, $var2=2; $var1 < 5 && $var2 < 5; $var1++, $var2++){
echo "$var1 x $var2 = ", $var1 * $var2, "<br>";
}


?>

Rezultat:

2 x 2 = 4
3 x 3 = 9
4 x 4 = 16


The for loop in each pass executes the statement that is in the body of the loop (this can be a complex statement).

for (expr1; expr2; expr3) statement

Explanation: expr1 allows you to prepare your loop, often initializing counter loop (which is also called the loop index), which tracks the number of times the loop has been done. The expression is executed prior to the execution of the loop. Expr2 is used for verification. The loop is repeated until its value is true. Here, usually checks the value of the counter loops. Expr3 is executed after the execution of the statement in each pass of the loop. This usually increases the value of the counter loop.

Example: we are going to use a loop for that will print out 3 times some text. The loop starts by setting the loop counters ($loop_counter) to 0. The loop in each pass increases the counter, and then checks whether the counter loops passed 3.

<?php

for ($loop_counter=0; $loop_counter < 3; $loop_counter++) { 
echo "I will do this three times.<br>";
}

?>

Save this file as for-loop.php.

Result:

I will do this three times.
I will do this three times.
I will do this three times.

The expressions in a for loop can process more indexes. It is necessary to separate them with a comma.

Example 2: the for loop that uses two indexes loop.

<?php

for($var1=2, $var2=2; $var1 < 5 && $var2 < 5; $var1++, $var2++){
echo "$var1 x $var2 = ", $var1 * $var2, "<br>";
}


?>

Result:

2 x 2 = 4
3 x 3 = 9
4 x 4 = 16

No comments:

Post a Comment

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