C++ Builder

La Isla Bonita



Break Statement
Break Statement


Description

This example shows how to use the break statement to simplify your code
Program execution will leave the while loop via the break statement, only if the CONTROL key is pressed. Then, the program will display the time difference between the ocurrance of the two events:

Event1: Button1 pressed on Form 1, and
Event2: CTRL key pressed.
Form1 contains a TButton

BreakStatement.cpp


#include <vcl.h>
#pragma hdrstop

#include "BreakStatement.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    DWORD t2;
    DWORD t1 = GetTickCount();

    while (1)
        if ((GetAsyncKeyState(VK_CONTROL) & 0x8000) == 0x8000)
            break;

    t2 = GetTickCount();
    ShowMessage("Time Difference = " + String(t2-t1) + " mS");
}

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.