目录

ifdef statement

ifdef声明

ifdef语句在分析时而不是运行时执行。 这允许您以非常有效的方式更改程序的运行方式。

由于ifdef语句在分析时工作,因此无法检查运行时值,而是可以在分析时设置或取消设置特殊定义。

语法 (Syntax)

ifdef语句的语法如下 -

ifdef macro then
   -- Statements will execute if the macro is defined.
end if

如果布尔表达式的计算结果为true,则执行if语句中的代码块。 如果没有,那么将执行ifdef语句结束后的第一组代码。

ifdef检查使用with define关键字定义的宏。 有许多宏定义如WIN3​​2_CONSOLE,WIN32或LINUX。 您可以按如下方式定义自己的宏 -

with define    MY_WORD    -- defines

您可以取消定义已定义的单词,如下所示 -

without define OTHER_WORD -- undefines

例子 (Example)

#!/home/euphoria-4.0b2/bin/eui
with define DEBUG
integer a = 10
integer b = 20
ifdef DEBUG then
   puts(1, "Hello, I am a debug message one\n")
end ifdef
if (a + b) < 40 then
   printf(1, "%s\n", {"This is true if statement!"})
end if
if (a + b) > 40 then
   printf(1, "%s\n", {"This is not true if statement!"})
end if

这会产生以下结果 -

Hello, I am a debug message one
This is true if statement!

ifdef...elsedef语句

如果定义了给定的宏,则可以执行一个操作,否则,如果未定义宏,则可以采取其他操作。

语法 (Syntax)

ifdef...elsedef语句的语法如下 -

ifdef macro then
   -- Statements will execute if the macro is defined.
elsedef
   -- Statements will execute if the macro is not defined.
end if

例子 (Example)

#!/home/euphoria-4.0b2/bin/eui
ifdef WIN32 then
   puts(1, "This is windows 32 platform\n")
elsedef
   puts(1, "This is not windows 32 platform\n")
end ifdef

在Linux机器上运行此程序时,它会产生以下结果 -

This is not windows 32 platform

ifdef...elsifdef声明

您可以使用ifdef...elsifdef语句检查多个宏。

语法 (Syntax)

ifdef...elsifdef语句的语法如下 -

ifdef macro1 then
   -- Statements will execute if the macro1 is defined.
elsifdef macro2 then
   -- Statements will execute if the macro2 is defined.
elsifdef macro3 then
   -- Statements will execute if the macro3 is defined.
   .......................
elsedef
   -- Statements will execute if the macro is not defined.
end if

例子 (Example)

#!/home/euphoria-4.0b2/bin/eui
ifdef WIN32 then
   puts(1, "This is windows 32 platform\n")
elsifdef LINUX then
   puts(1, "This is LINUX platform\n")
elsedef
   puts(1, "This is neither Unix nor Windows\n")
end ifdef

在Linux机器上运行此程序时,它会产生以下结果 -

This is LINUX platform

以上所有陈述都有各种形式,可根据不同情况为您提供灵活性和易用性。

↑回到顶部↑
WIKI教程 @2018