Aztec® Programming Language
Version 1.1 Alpha 2

Copyright © 2010-2017, Aztec Development Group, All Rights Reserved

Download Aztec

Search        Contact Us

If you return me to my home port...

I will kiss you... Mother Earth.

- Mark Farner

 

The If statement provides a mechanism to conditionally execute a block of statements based on the value of one or more boolean expressions. The following table shows the syntax for the If statement.

Aztec If Statement Syntax

 

♦ Conditionally executes a single block of statements one time based on the value of one or more boolean expressions, as shown above.

♦ The first block of statements is executed if the value of the first boolean expression is true, and it is not executed if it is false.

♦ The statement supports any number of separate 'else if' clauses, and a single 'else' clause at the end, both of which are optional.

♦ If the first boolean epxression is false, the system evaluates each 'else if' boolean expression in top to bottom order until it finds a match.

♦ If a match is found, the corresponding block of statements is executed and the 'if' statement terminates.
♦ If no match is found, the 'else' statement block is executed, if one is provided.

♦ The 'branch' statement can also be used to perform similar logic with multiple test conditions.

♦ An Aztec Statement Block can be one or more valid Aztec statements. If more than one statement, they must be enclosed in curly braces (‘{‘  and ‘}’).

♦ A compiler error is generated if the expression inside the parentheses is not a boolean expression.

♦ The ‘if’ statement is unique amongst Aztec flow control statements.

♦ In addition to being used inside a method, the 'if' statement can also be used at the module level and within the class definition statement for ‘compile-time logic’.

♦ All other flow control statements can only be used inside a method.

♦ An example script showing Aztec 'if' statements at the module level, class level and within methods.

#===================================================================================================
# Example Script: If Statement
# Demonstrates the use of the 'if' statement in normal methods, compiler methods, classes and at
# the module level.
#===================================================================================================

data<string> const MyClassChoice = "wxyz"

# Enumerations to match OS command line 'flags' at compile-time and 'arguments' at run-time.
enum Weather { sunny, rainy, cloudy, stormy } # Used at compile-time for OS 'flag' option
enum ProceedOption { go, stay, retreat, surrender } # Used at run-time for OS '-arg' option

# Compiler variable used during compile logic but also carries "weather" over to run-time.
data<Weather> compiler CompilerWeather = Weather.sunny

# Compiler method compares incoming string and returns an int. Called from within class definition.
method<int> compiler CompilerClassOption(string ClassOptionStr)
{
data<int> ClassOptionId

# At compile-time, we can compare input string to enumeration "names".
if ( ClassOptionStr == "abc" )
{
ClassOptionId = 1
}
else if ( ClassOptionStr == "wxyz" )
{
ClassOptionId = 2
}
else
{
ClassOptionId = 3
}

return(ClassOptionId)
}

#---------------------------------------------------------------------------------------
# Compile-time logic at module level, where 'if' is allowed. This logic gets the string
# representation of a Weather enum, and checks to see if it matches a flag from the
# command line. It keeps checking each one until it finds a match, or 'else'.
#---------------------------------------------------------------------------------------
if ( CompilerIsFlag(CompilerEnumStr(Weather.sunny)) )
CompilerWeather = Weather.sunny
else if ( CompilerIsFlag(CompilerEnumStr(Weather.cloudy)) )
CompilerWeather = Weather.cloudy
else if ( CompilerIsFlag(CompilerEnumStr(Weather.rainy)) )
CompilerWeather = Weather.rainy
else
CompilerWeather = Weather.stormy

# Run-time entry point for the script.
method Main()
{
data<string> OptionStr = GetScript().GetArg(1).Lwr()
data<ProceedOption> OptionId

# At run-time, choose option id based on lower case of 1st command line arg.
if ( OptionStr == "go" )
OptionId = ProceedOption.go
else if ( OptionStr == "stay" )
OptionId = ProceedOption.stay
else if ( OptionStr == "retreat" )
OptionId = ProceedOption.retreat
else
OptionId = ProceedOption.surrender

# Possibly modify option based on weather from compile-time logic and class method.
if ( CompilerWeather == Weather.stormy )
{
OptionId = ProceedOption.surrender
}

# Print the result along with a confidence factor from the shared class method.
StdIO.Write("Weather is " + CompilerWeather.Str() + " and final option is " + OptionId.Str())
Test.PrintConfidence()
}

# Class definition which uses compile-time logic to define a method.
class Test()
{
#-----------------------------------------------------------------------------------------
# Logic occurs at compile-time within class to create different shared method logic based
# on option. Obviously we don't need a separate method definition to handle the simple
# differences below, but hopefully it gets the point across.
#-----------------------------------------------------------------------------------------
if ( CompilerClassOption(MyClassChoice) != 3 )
{
method shared PrintConfidence()
{
StdIO.Write("Value from Test class suggest results are valid.")
}
}
else
{
method shared PrintConfidence()
{
StdIO.Write("Value from Test class suggest results might be invalid.")
}
}
}

Page UpPage DownCopyright © 2010-2017
Aztec Development Group
All Rights Reserved

Download Aztec