if 条件语句

//if 条件语句 
条件语句通过条件检测,判断是否执行该条件语句中包含的语句。
条件语句可有两种基本形式:if语句和case语句。

If语句
if-then型语句,仅当条件满足时,语句才执行;
if-then-else型,if语句在两条语句中选择一条执行。

procedure TForm1.Button1Click(Sender: TObject);
begin
    // simple if statement
    if CheckBox1.Checked then
    ShowMessage ('CheckBox1 is checked')
end;

如果点击按钮后没有反应,表明复选框未被选中。
对于这种情况,最好能交代得更清楚些,为此在第二个按钮的代码中,我用了if-then-else 语句:

procedure TForm1.Button2Click(Sender: TObject);
begin
    // if-then-else statement
    if CheckBox2.Checked then
        ShowMessage ('CheckBox2 is checked')
    else
        ShowMessage ('CheckBox2 is NOT checked');
end;

要注意的是,不能在第一句之后、else 关键词之前加分号,否则编译器将告知语法错误。
实际上,if-then-else 语句是单纯的一条语句,因此不能在语句中间加分号。




原文地址:https://www.cnblogs.com/xe2011/p/2532899.html