Aztec® Programming Language
Version 1.1 Alpha 2

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

Download Aztec

Search        Contact Us

I sing my song to the free.

- Pete Townshend

 

The Iterate statement provides a mechanism to execute a single block of statements in a loop. A looping object is used to iterate through a specified range of values, with the block of statements being executed for each value of the iterator. The following table shows the syntax for the Iterate statement.

Aztec Iterate Statement Syntax

 

♦ The object must be a valid primitive object (int, float, string, bool or user defined enumeration), and the scope of the object can be local, global, shared class or instance class. The primitive object must be used as a "value" in the iterate statement itself (within the parentheses).

♦ Prior to executing the first loop, the primitive variable is set with the value specified by Value1.

♦ A check is then performed to see if the value is within the range, and the loop is executed only if it is.

♦ At the end of the loop, if the value of Variable is less than the max limit specified in the 'iterate' statement, Variable is incremented and the loop is executed again.

♦ The automatic incrementing of the iterator follows the same rules as the Inc() method for that primitive data type. This operation is also valid for string objects and enumerations.

♦ There is special logic in place at the end of each 'iterate' loop to check the current value before performing the increment process. This is necessary for enumerations, in particular, because a Math Domain exception occurs if an attempt is made to increment an enumeration past its highest value.

♦ The 'iterate' statement can only be used inside a "compiler" method or a "normal" method.

♦ An example script showing 'iterate' statement usage in a compiler method and a normal (run-time) method.

#===================================================================================================
# Example Script: Iterate Statement
# Demonstrates the use of the 'iterate' statement in normal methods and compiler methods.
#===================================================================================================

# Simple enumeration for iterate demonstration.
enum Vowels { a, e, i, o, u }

# Source code is created dynamically at compile-time and compiled to create arrays with these names.
data<string> const VowelArrayName = 'VowelArray'
data<string> const ConsonantArrayName = 'ConsonantArray'

# Call the compiler method to create the vowel and consonant arrays.
CompilerCreateArrays()

#---------------------------------------------------------------------------------------------
# Compiler method to loop through character list and create vowel array and consonant array.
# It creates the lists dynamically in Aztec source code and then calls the LoadModule method
# to compile each of the two code strings as separate modules. That creates the Aztec arrays.
#---------------------------------------------------------------------------------------------
method compiler CompilerCreateArrays()
{
data<string> Char
data<string> CharCode
data<string> VowelArrayCode = "public data<string[]> " + VowelArrayName + " = { "
data<string> ConsonantArrayCode = "public data<string[]> " + ConsonantArrayName + " = { "
data<bool> VowelListEmpty = true
data<bool> ConsonantListEmpty = true

# Loop through all characters in the alphabet.
iterate ( Char in 'a'..'z' )
{
# Determine if vowel or consonant and attach code for appropriate list.
if ( IsVowel(Char) )
{
if ( VowelListEmpty )
CharCode = "'" + Char + "'"
else
CharCode = ",'" + Char + "'"

# Add this item to the vowel array creation code and turn off empty flag.
CompilerStrAdd(@VowelArrayCode,CharCode)
VowelListEmpty = false
}
else
{
if ( ConsonantListEmpty )
CharCode = "'" + Char + "'"
else
CharCode = ",'" + Char + "'"

# Add this item to the consonant array creation code and turn off empty flag.
CompilerStrAdd(@ConsonantArrayCode,CharCode)
ConsonantListEmpty = false
}
}

# Attach the ending brace to each of the code strings.
CompilerStrAdd(@VowelArrayCode,'}')
CompilerStrAdd(@ConsonantArrayCode,'}')

# Now compile each of the source code strings to create the actual Aztec arrays.
CompilerLoadModule(VowelArrayCode,false)
CompilerLoadModule(ConsonantArrayCode,false)
}

# Compiler method to determine if a character is a vowel or consonant.
method<bool> compiler IsVowel(string Character)
{
data<bool> Result = false
data<string> UpperCase = CompilerStrUpr(Character)

if ( (UpperCase == 'A') | (UpperCase == 'E') | (UpperCase == 'I') |
(UpperCase == 'O') | (UpperCase == 'U') )
{
Result = true
}

return(Result)
}

#-----------------------------------------------------------------------------------
# Run-time entry point for the script. This method loops through each of the two
# array that were created with the above compile-time logic (VowelArray and
# ConsonantArray). It creates a formatted string for each to dump out the contents.
#-----------------------------------------------------------------------------------
method Main()
{
data<int> Count
data<string> ArrayString
data<Vowels> MyVowel

# Loop through enumerations using 'iterate'.
StdIO.Write("Vowels extracted from enumeration")

iterate ( MyVowel in Vowels.MinValue..Vowels.MaxValue )
{
StdIO.Write(" " + MyVowel.Str())
}

# Loop through the vowel array and create a formatted string to write them out.
iterate ( Count in 1..VowelArray.Size() )
{
if ( Count == 1 )
ArrayString = VowelArray[Count]
else
ArrayString.Add(", " + VowelArray[Count])
}

StdIO.Write("Vowel array contents: " + ArrayString)

# Loop through the consonant array and create a formatted string to write them out.
iterate ( Count in 1..ConsonantArray.Size() )
{
if ( Count == 1 )
ArrayString = ConsonantArray[Count]
else
ArrayString.Add(", " + ConsonantArray[Count])
}

StdIO.Write("Consonant array contents: " + ArrayString)
}

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

Download Aztec