请问IFDEF...ELSE条件编译里如何使用 AND 、OR 处理多个条件

k39k39 2014-10-26 09:48:44
没有在DELPHI的帮助里找到说明,也没有看到有类似的例子。

DELPHI 的IF条件编译,不支持使用 AND、OR 这样的语法吗?那当对一个代码块需要做多个条件判断时,该怎么写?

{$IFDEF Debug}
XXX
{$ENDIF}
...全文
1361 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
s11ss 2014-10-31
  • 打赏
  • 举报
回复
引用
IF directive See also Type Conditional compilation Syntax {$IF expression} Remarks Compiles the Delphi source code that follows it if expression is true. expression must conform to Delphi syntax and return a Boolean value; it may contain declared constants, constant expressions, and the functions Defined and Declared. For example, {$DEFINE CLX} const LibVersion = 2.1; {$IF Defined(CLX) and (LibVersion > 2.0) } ... // this code executes {$ELSE} ... // this code doesn't execute {$IFEND} {$IF Defined(CLX) } ... // this code executes {$ELSEIF LibVersion > 2.0} ... // this code doesn't execute {$ELSEIF LibVersion = 2.0} ... // this code doesn't execute {$ELSE} ... // this code doesn't execute {$IFEND} {$IF Declared(Test)} ... // successful {$IFEND} The special functions Defined and Declared are available only within $IF and $ELSEIF blocks. Defined returns true if the argument passed to it is a defined conditional symbol. Declared returns true if the argument passed to it is a valid declared Delphi identifier visible within the current scope. If the identifiers referenced in the conditional expression do not exist, the conditional expression will be evaluated as false: {$IF NoSuchVariable > 5} WriteLn('This line doesn''t compile'); {$IFEND} The $IF and $ELSEIF directives are terminated with $IFEND, unlike other conditional directives that use the $ENDIF terminator. This allows you to hide $IF blocks from earlier versions of the compiler (which do not support $IF or $ELSEIF) by nesting them within old-style $IFDEF blocks. For example, the following construction would not cause a compilation error: {$UNDEF NewEdition} {$IFDEF NewEdition} {$IF LibVersion > 2.0} ... {$IFEND} {$ENDIF} $IF supports evaluation of typed constants, but the compiler doesn't allow typed constants within constant expressions. As a result, const Test: Integer = 5; {$IF SizeOf(Test) > 2} ... is valid, while const Test: Integer = 5; {$IF Test > 2 } // error ... generates a compilation error. If your code needs to be portable between various versions of Delphi or Kylix, you will need to test whether or not this directive is supported by the compiler. You can surround your code with the following directives: $IFDEF conditionalexpressions . // code including IF directive . // only executes if supported $ENDIF
木头刀 2014-10-30
  • 打赏
  • 举报
回复
支持 {$IF Defined(Condition1) or Defined(Condition2) and Defined(Condition3)} {$ELSE} {$IFEND} 和普通的and,or逻辑一样
武稀松 2014-10-29
  • 打赏
  • 举报
回复
const a = '12345'; {$IF Defined(Debug) or (sizeof(Integer)=4) or (Length(a) = 5)} {$endif}
yggk 2014-10-29
  • 打赏
  • 举报
回复
不支持。嵌套着用吧
k39k39 2014-10-29
  • 打赏
  • 举报
回复
谢谢2楼的回答,这么说IFDEF是不支持 多条件了
踏雪无痕 2014-10-29
  • 打赏
  • 举报
回复
看了代码,不明觉厉啊
huangtao0416 2014-10-28
  • 打赏
  • 举报
回复
学习了!正好需要
s11ss 2014-10-26
  • 打赏
  • 举报
回复
引用
ELSEIF directive See also Type Conditional compilation Syntax {$ELSEIF} Remarks The $ELSEIF directive allows multi-part conditional blocks where at most one of the conditional blocks will be taken. $ELSEIF is a combination of a $ELSE and a $IF. For example: {$IFDEF foobar} do_foobar {$ELSEIF RTLVersion >= 14} blah {$ELSEIF somestring = 'yes'} beep {$ELSE} last chance {$IFEND} Of these four cases, only one will be taken. If none of the first three conditions is true, then the $ELSE clause will be taken. $ELSEIF must be terminated by $IFEND. $ELSEIF cannot appear after $ELSE. Conditions are evaluated top to bottom like a normal "if ... else if ... else " sequence. In the example above, if foobar is not defined, RTLVersion is 15, and somestring = 'yes', only the "blah" block will be taken not the "beep" block, even though the conditions for both are true.

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧