Instructiuni Repetitive

postat acum 4 ani de Stefanescu Mihai in categorie PHP

Instructiunile repetitive sunt folosite atunci cand dorim executia repetata a unei portiuni de cod, in mod controlat:

  • Pana cand o conditie este indeplinita sau nu mai este adevarata (TRUE).
  • De un anumit numar de ori

Avem urmatoarele tipuri de instructiuni:

  • for – se repeta de un anumit numar de ori
  • while – se repeta pana cand o anumita conditie este adevarata
  • do…while – se repeta pana cand o anumita conditie este adevarata
  • foreach – ruleaza pentru fiecare element al unui array

Instructiunea for

Instructiunea for este mai complexa si cuprinde 3 parti.

for (initializare; conditie; incrementare){
  Codeul ce va fi executat in mod repetat;
}

Initializarea este folosita pentru a seta valoarea de start a contorului (setam o variabila). Apoi punem o conditie si in ultimul rand setam operatie care se va executa dupa fiecare repetitie.
Exemplu:

for( $i=0; $i<5; $i++ ){
	echo $i."<br/>";
}

Rezultatul va fi acesta:

1
2
3
4
5

Instructiunea While

Acest tip de instructiune se repeta atat timp cat o expresie data este adevarata.

$i = 0;
while($i < 3){
   echo $i."<br/>";
   $i++;
}

Rezultatul:

1
2

3

Instructiunea Do…While

Acest tip de instructiune va executa un bloc de cod cel putin o data, iar apoi se va repeta de cate ori este necesar pana cand conditia devine adevarata.

$i = 0;
do
{
  $i++;
}while( $i < 4 );

Rezultatul:

1
2

3

4

Instructiunea foreach

Instructiunea foreach este folosita pentru a itera un array.

$a = array('a', 'b', 'c');
foreach($a as $litera){
    echo $litera;
}

Rezultatul:

a

b

c

Instructiunea break

Instructiunea break este folosita pentru a termina executarea unei instructiuni repetitive permanent.

Aceasta instructiune va fi situata in interiorul blocului cu codul de executat.

$i = 0;
while( $i < 10)
{
   $i++;
   if( $i == 3 ){
       break;
   }
}

Aceasta instructiune se va opri cand $i ajunge sa aiba valoarea 3.

Instructiunea Continue

Instructiunea continue este similara cu instructiunea break, doar ca in timp ce break iese complet din instructiunea repetitiva, continue va sari decat peste iteratia curenta.

$array = array( 1, 2, 3, 4, 5);
foreach( $array as $value )
{
  if( $value == 3 )continue;
  echo $value."<br />";
}

Rezultatul:

1

2

4

5

Sunt un tanar programator din Bucuresti ce lucreaza in PHP/Mysql (MySqli/PDO), Laravel, CodeIgniter, MySQL, PostgreSQL, Wordpress, HTML5/CSS3, Sass, Photoshop si multe altele.
Google+ Community Facebook Group
Acest articol a fost mutat de pe vechea platforma.
Pentru orice eroare aparuta la mutare va rog sa ma contactati!

Comentariu postate de free minecraft la data de 14.05.2019
Hey I am so thrilled I found your blog page, I really found you by accident, while I was searching on Bing for something else, Anyways I am here now and would just like to say cheers for a remarkable post and a all round thrilling blog (I also love the theme/design), I don’t have time to go through it all at the minute but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the awesome work.
Comentariu postate de curry 4 la data de 15.05.2019
I really wanted to type a simple message to be able to express gratitude to you for all the stunning guidelines you are sharing on this site. My long internet look up has finally been recognized with good knowledge to go over with my guests. I 'd admit that many of us website visitors are really endowed to dwell in a fantastic network with very many lovely professionals with beneficial guidelines. I feel somewhat lucky to have seen your website page and look forward to really more enjoyable minutes reading here. Thanks once more for a lot of things.
Comentariu postate de minecraft download for free la data de 16.05.2019
It's difficult to find experienced people for this subject, however, you seem like you know what you're talking about! Thanks
Comentariu postate de how to download minecraft free la data de 16.05.2019
Wonderful, what a blog it is! This web site provides helpful data to us, keep it up.
Comentariu postate de minecraft free download pc la data de 17.05.2019
It's going to be finish of mine day, however before finish I am reading this great post to improve my know-how.