首页 > 解决方案 > 仅当在 if then 中满足条件时才检查条件

问题描述

我有一个代码示例,但不确定最好使用哪种方式。

例如我有

if (x = 1) and (y = 2) and (if abc = false then check if z = 3) then
begin
  ...

只检查

if x = 1
if y = 2
if abc = false check z = 3. if abc = true then dont check z = 3

我不确定我是否解释得最好,但希望人们能理解。

我想知道这是否可行或最好的方法。请记住,而不是示例中的 x、y、z 和 abc。我可以使用更多。

我目前的结构...我认为这不实用,并认为有更好的方法,但我不确定

if (abc = false) then
begin
  if (x = 1) and (y = 2) and (z = 3) then
  begin
    ...  
end
else
begin
  if (x = 1) and (y = 2) then
  begin
    ...

提前致谢

标签: delphi

解决方案


您的代码示例都无法编译,因为它们都没有使用正确的语法。

这应该让你开始:

if (x = 1) and (y = 2) then
begin
  if (abc) then 
    // Handle abc = True
  else 
  begin
    if (z = 3) then
      // Handle abc = false and z = 3
    else
      // Handle abc = false and z <> 3
  end;
end;

推荐阅读