ifdef...elsifdef...elsedef...endifdef Statements



The ifdef Statement

The ifdef statement is executed at parse time not runtime. This allows you to change the way your program operates in a very efficient manner.

Since the ifdef statement works at parse time, runtime values cannot be checked, instead special definitions can be set or unset at parse time as well.

Syntax

The syntax of ifdef statement is as follows −

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

If the boolean expression evaluates to true then the block of code inside the if statement is executed. If not, then the first set of code after the end of the ifdef statement will be executed.

The ifdef checks the macros defined by using with define keywords. There are plenty of macros defined like WIN32_CONSOLE, WIN32, or LINUX. You can define your own macros as follows −

with define    MY_WORD    -- defines

You can un-define an already defined word as follows −

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

This produces the following result −

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

The ifdef...elsedef Statement

You can take one action if given macro is defined otherwise you can take another action in case given macro is not defined.

Syntax

The syntax of ifdef...elsedef statement is as follows −

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

When you run this program on Linux machine, it produces the following result −

This is not windows 32 platform

The ifdef...elsifdef Statement

You can check multiple macros using ifdef...elsifdef statement.

Syntax

The syntax of ifdef...elsifdef statement is as follows −

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

When you run this program on Linux machine, it produces the following result −

This is LINUX platform

All the above statements have various forms which provide you a flexibility and ease of use based on different situations.

euphoria_branching.htm
Advertisements