Sign in Join
Wiki copytemporary

For/fr

Deutsch · English · Français · Русский

keyword utilisé avec "to" ou "downto" et "do" pour parcourir une boucle dans laquelle la variable de contrôle est incrémentée ou décrémentée de 1 à chaque fois :

pascal
FOR _control_variable_??:= _start_ [[To/fr|TO]] _final_ DO _statement_

qui augmente control_variable de 1 à chaque exécution de la boucle jusqu'à ce que la valeur soit supérieure ou égale à final ou

pascal
FOR _control_variable_??:= _start_ [[Downto/fr|DOWNTO]] _final_ do _statement_

qui diminue control_variable de 1 à chaque exécution de la boucle jusqu'à ce que la valeur soit inférieure ou égale à final

où control_variable Where control_variable est la variable mise à la valeur start au départ.

pascal
For I:=1 To 100 Do statement;

(répète statement 100 fois, en augmentant I de 1 à 100)

pascal
for I:=100 downto 1 do ''statement'';

(répète statement 100 fois, en diminuant I de 100 à 1)

After the loop, the value of control_variable will be final. If the loop was not executed, the value of control_variable does not change.

Variables enumérées

Pascal permet d'employer une variable de contrôle d'un type énuméré (par exemple un booléen) au lieu d'un variable entière :

pascal
program Fruits;
    type
      TFruit = (fPomme, fPoire, fFraise, fMirabelle, fPrune, fFramboise, fCassis);
    const
      FruitTextes: array[TFruit] of String =
        ('Pomme', 'Poire', 'Fraise', 'Mirabelle', 'Prune', 'Framboise', 'Cassis');
    begin
      WriteLn('** Liste des fruits **');
      for f := Low(TFruit) to High(TFruit) do
        WriteLn(FruitTextes[f]); 
    End;
    
    End.

Extension de Pascal Objet

L'exemple des fruits devient :

pascal
program Fruits;
    type
      TFruit = (fPomme, fPoire, fFraise, fMirabelle, fPrune, fFramboise, fCassis);
    const
      FruitTextes: array[TFruit] of String =
        ('Pomme', 'Poire', 'Fraise', 'Mirabelle', 'Prune', 'Framboise', 'Cassis');
    begin
      WriteLn('** Liste des fruits **');
      for f in TFruit do
        WriteLn(FruitTextes[f]); 
    End;
    
    End.

Ce qui est plus joli, non ?

Voir aussi

Mots-clés: begin — do — else — end — for — if — repeat — then — until — while


From the Free Pascal wiki, page "For/fr", as the Internet Archive kept it on 2025-01-14, last changed on 2020-02-16. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 4, last changed by WikiArchive .

Edited on wiki.freepascal.org by 2 people, 2016–2020

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
E-ricCreated page
E-ric (minor)
TrevFixed syntax highlighting; deleted category included in page template (minor)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.