Les Boucles itérations énumérations

While

Javascript

Javascript
let i=0;
while (i < 100) {
  alert(i);
  i++;
 }

Do...While

Javascript

Javascript
let i=0;
do {
  alert(i);
  i++;
} while (i < 100);

PHP

Php
$x = 1;
do {
  echo "The number is: $x <br>";
  $x++;
} while ($x <= 5);
?>

Pascal

While do

Pascal
while number>0 do
begin
   sum := sum + number;
   number := number - 2;
end;

Repeat Until

Pascal
 a := 10;
   repeat
      writeln('value of a: ', a);
      a := a + 1
   until a = 20;

For...To

Javascript

Javascript
for (let i=0; i<100; i++) {
  alert(i);
  i++;
}

Pascal

Pascal
for vI:= 20 to 200 do
  begin
      ...
      if vI = vA then break; // Si la condition est vérifiée, la boucle se termine
      ...
  end;
 
  // Variante DownTo
  for vI:= 200 downto 20 do
  begin
      ...
  end;

Foreach

Equivalents for…of ou for..in' en javascript

PHP

php
foreach ($tickets as $ticket) {
          ...
        }

For...In

Javascript

Avec un tableau

Javascript
let fruits = ["Pomme","Poire","Banane"];
for (let i in fruits) {
  alert(i+" : "+fruits[i]);
}
 
--- Résultat ---
0 : Pomme
1 : Poire
2 : Banane

Avec un Objet

Javascript
let coord = {  
  "x": 256,
  "y":125,
  "z":584}
for (let c in coord) {
  alert(c+" : "+coord[c]);
}
 
--- Résultat ---
x : 256
y : 125
z : 584

For...Of

Javascript

Avec un tableau

Javascript
let fruits = ["Pomme","Poire","Banane"];
for (let fruit of fruits) {
  alert(fruit);
}
 
--- Résultat ---
Pomme
Poire
Banane

Contrôles des boucles

Pascal

Break

break sort de la boucle

Code
   a := 10;
   while  a < 20 do
 
   begin
      writeln('value of a: ', a);
      a:=a +1;
      if( a > 15) then break;
   end;
Résultat
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15

Continue

continue retourne au début de la boucle :

Code
   a := 10;
   repeat
      if( a = 15) then
      begin
         a := a + 1;
         continue;
      end;
      writeln('value of a: ', a);
      a := a+1;
   until ( a = 20 );
Résultat
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Vous pourriez laisser un commentaire si vous étiez connecté.