while (C++)

De RAD Studio
Aller à : navigation, rechercher

Remonter à Liste alphabétique des mots clés - Index


Catégorie

Instructions

Syntaxe

while ( <condition> ) <instruction>

Description

Utilisez le mot clé while pour itérer une instruction de façon conditionnelle.

<instruction> est exécuté de façon répétitive jusqu'à ce que la valeur de <condition> soit false.

Le test prend place avant que <instruction> ne s'éxécute. C'est pourquoi, si <condition> est évalué à false à la première passe, la boucle ne s'exécute pas.

Cet exemple illustre l'utilisation du mot clé while avec do.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 TSearchRec sr;
 int iAttributes = 0;
 StringGrid1->RowCount = 1;
 iAttributes |= faReadOnly * CheckBox1->Checked;
 iAttributes |= faHidden * CheckBox2->Checked;
 iAttributes |= faSysFile * CheckBox3->Checked;
 iAttributes |= faVolumeID * CheckBox4->Checked;
 iAttributes |= faDirectory * CheckBox5->Checked;
 iAttributes |= faArchive * CheckBox6->Checked;
 iAttributes |= faAnyFile * CheckBox7->Checked;
 StringGrid1->RowCount = 0;
 if (FindFirst(Edit1->Text, iAttributes, sr) == 0)
 {
   do
   {
     if ((sr.Attr & iAttributes) == sr.Attr)
     {
       StringGrid1->RowCount = StringGrid1->RowCount + 1;
       StringGrid1->Cells[1][StringGrid1->RowCount-1] = sr.Name;
       StringGrid1->Cells[2][StringGrid1->RowCount-1] = IntToStr(sr.Size);
     }
   } while (FindNext(sr) == 0);
   FindClose(sr);
 }
}

Cet exemple illustre l'utilisation du mot clé while.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 int Result = 0;
 int I = 0;
 TColor RealColor = Graphics::ColorToRGB(Form1->Color);
 while (I < NumPaletteEntries)
 {
   TColor paletteColor =
   RGB(
     FPaletteEntries[I].peRed,
     FPaletteEntries[I].peGreen,
     FPaletteEntries[I].peBlue);
   if (RealColor == paletteColor)
   {
     Label1->Caption = IntToStr(I);
     RedEdit->Text = IntToStr(FPaletteEntries[I].peRed);
     GreenEdit->Text = IntToStr(FPaletteEntries[I].peGreen);
     BlueEdit->Text = IntToStr(FPaletteEntries[I].peBlue);
     break;
   }
   I++;
 };
}

Voir aussi