 faq | docs | tutorials | guide | reference | contact | home

 TUTORIAL - CONTROL STRUCTURES Previous | Next | Tutorial Home

The FGL language supports a number of standard conditional statements used for the control and flow of program logic. Each of these control structures has the ability to alter or redirect program flow during the execution of FGL programs or Active Pages. Additionally, several options are provided that enhance the interaction with Active Pages and web content. Available control structures include:

• For/End
• While/End
• Repeat/Until
• If/Else/ElseIf/End
• Switch/Case/Default/End
• Break/Continue
• Goto/Label
• Try/Catch
• Return
• Inline Operator

The syntax of the FGL controls structures is similar to comparable structures in numerous other languages. Consult the FGL documentation for precise usage.

FGL supports standard comparison operators, including:
 a == b a equals b a != b a does not equal b a <> b a does not equal b a <= b a less than or equal b a >= b a greater than or equal b
 a < b a less than b a > b a greater than b a && b a and b are true a || b a or b is true

 FGL control structure example:
 ``` Control Structure Sample <[ ! "

FOR/NEXT example:
" a = { "One", "Two", "Three", "Four", "Five", "Six" } cnt = len( a ) for ( i=1; i<=cnt; i++ ) ! "" + i + ". " + a[i] + "
" end ! "

WHILE/END example:
" i = cnt while ( type( a[i] ) != "U" ) ! "" + i + ". " + a[i] + "
" i-- end ! "

REPEAT/UNTIL example:
" i=2, s = "" repeat s += "" + i + ", " i *= 2 until ( i > 8194 ) ! left( s, len( s ) - 2 ) ! "

IF/ELSE/ELSEIF/END example:
" hour = 0 + strextract( time( ), ":", 1 ) if ( hour < 8 ) s = "really early" elseif ( hour < 12 ) s = "morning" elseif ( ( hour >= 12 ) && ( hour <= 13 ) ) s = "lunch time" elseif ( hour < 18 ) s = "afternoon" elseif ( ( hour >= 18 ) && ( hour <= 19 ) ) s = "dinner time" elseif ( hour < 22 ) s = "evening" else s = "really late" end ! "The time is " + timetostr( time( ), 0 ) + "m, which is '" + s + "'" ! "

SWITCH/CASE/END example:
" month = 0 + date( "MM" ) switch ( month ) case 12 case 1 case 2 s = "winter" break case 3 case 4 case 5 s = "spring" break case 6 case 7 case 8 s = "summer" berak default s = "fall" end ! "The current season (for the Northern Hempisphere) is: " + s ! "

BREAK/CONTINUE example:
" i=1, s="" while ( 1 ) i++ if ( i < 50 ) continue end if ( i > 65 ) break end s += "" + i + ", " end ! left( s, len( s ) - 2 ) ! "

GOTO/LABEL example:
" if ( seconds( ) % 2 ) goto EVEN else goto ODD end label EVEN ! "the time is an even number. " goto DONE label ODD ! "the time is an odd number. " goto DONE label DONE ! "done." ! "

TRY/CATCH example:
" x = ( seconds( ) % 2 ) try y = 1 / x s = "No divide by zero" catch( e ) s = "Divide by zero error caught." end ! s ! "

RETURN example:
" ExitFlag = 0 if ( ExitFlag ) ! "returned" return( 0 ) end ! "not returned" ! "

INLINE OPERATOR example:
" x = seconds( ) IsOddNumber = ( x % 2 ) // using the modulus (remainder) operator ! "" + x + " is an " + ( IsOddNumber ? "odd number" : "even number" ) ]> ```

 ```<[ // note: embedded libarary references, classes, and functions // must be defined first, before any other FGL statements CLASS objContact local fname, lname, phone END ]> Control Structure Sample <[ obj = new( "objContact" ) obj.fname = "Steve", obj.lname = "Repetti" a = { { "one", "two", "three", "four", "five", "six" }, { 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048 }, { 1, "two", 3, "four" }, { null, 1, 1, null, null, 1, 1, 1 }, { obj }, { "H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d" } } ! "" ! "
i-valuej-valuedata typevalue
" + a[i,j] break case "C" s = "character" + a[i,j] break case "O" s = "object" + a[i,j].fname + " " + a[i,j].lname break case "U" s = "undefinednull" break default s = "unknown type" + a[i,j] end ! "
" + i + "" + j + "" + s + "
" ! " " cnt = len( a ) for ( i=1; i<=cnt; i++ ) jcnt = len( a[i] ) for ( j=1; j<=jcnt; j++ ) switch ( type( a[i,j] ) ) case "N" s = "numeric" end end ! "" ]> ```

FGL makes it easy to put control structure wrappers around pure HTML and JavaScript code in Active Pages by using the FGL INLINE HTML tag, <% (opening) and %> (closing). For example:

 FGL INLINE HTML example:
 ```<[ IsFlag = seconds( ) % 2 ]> Control Structure Sample

This is text is always available and rendered in pure HMTL. The FGL INLINE HTML tag is used to create control structures of HTML and JavaScript without having to change the underlying code. <[ if ( IsFlag ) <%

This statement is only displayed when the IsFlag variable is set to TRUE, otherwise these lines are skipped and the alternate FALSE statement is displayed. %> else <%

This statement is only displayed when the IsFlag variable is set to FALSE, otherwise these lines are skipped and the alternate TRUE statement is displayed. %> end ]>

```

The above example could also have been written as follows:

 Alternate FGL INLINE HTML example:
 ```<[ IsFlag = seconds( ) % 2 ]> Control Structure Sample

This is text is always available and rendered in pure HMTL. The FGL INLINE HTML tag is used to create control structures of HTML and JavaScript without having to change the underlying code. <[ if ( IsFlag ) <%

This statement is only displayed when the IsFlag variable is set to TRUE, otherwise these lines are skipped and the alternate FALSE statement is displayed. %> else <%

This statement is only displayed when the IsFlag variable is set to FALSE, otherwise these lines are skipped and the alternate TRUE statement is displayed. %> end ]>

```

#####