while (C++)

From RAD Studio
Jump to: navigation, search

Go Up to Keywords, Alphabetical Listing Index


Category

Statements

Syntax

while ( <condition> ) <statement>

Description

Use the while keyword to conditionally iterate a statement.

<statement> executes repeatedly until the value of <condition> is false.

The test takes place before <statement> executes. Thus, if <condition> evaluates to false on the first pass, the loop does not execute.

This example illustrates the use of the keyword while with 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);
 }
}

This example illustrates the use of the keyword 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++;
 };
}

See Also