|
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 |
|
|
<html>
<head>
<title>Control Structure Sample</title>
</head>
<body text=black bgcolor=white>
<[
! "<p>FOR/NEXT example:<br>"
a = { "One", "Two", "Three", "Four", "Five", "Six" }
cnt = len( a )
for ( i=1; i<=cnt; i++ )
! "" + i + ". " + a[i] + "<br>"
end
! "<p>WHILE/END example:<br>"
i = cnt
while ( type( a[i] ) != "U" )
! "" + i + ". " + a[i] + "<br>"
i--
end
! "<p>REPEAT/UNTIL example:<br>"
i=2, s = ""
repeat
s += "" + i + ", "
i *= 2
until ( i > 8194 )
! left( s, len( s ) - 2 )
! "<p>IF/ELSE/ELSEIF/END example:<br>"
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 + "'"
! "<p>SWITCH/CASE/END example:<br>"
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
! "<p>BREAK/CONTINUE example:<br>"
i=1, s=""
while ( 1 )
i++
if ( i < 50 )
continue
end
if ( i > 65 )
break
end
s += "" + i + ", "
end
! left( s, len( s ) - 2 )
! "<p>GOTO/LABEL example:<br>"
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."
! "<p>TRY/CATCH example:<br>"
x = ( seconds( ) % 2 )
try
y = 1 / x
s = "No divide by zero"
catch( e )
s = "Divide by zero error caught."
end
! s
! "<p>RETURN example:<br>"
ExitFlag = 0
if ( ExitFlag )
! "returned"
return( 0 )
end
! "not returned"
! "<p>INLINE OPERATOR example:<br>"
x = seconds( )
IsOddNumber = ( x % 2 ) // using the modulus (remainder) operator
! "" + x + " is an " + ( IsOddNumber ? "odd number" : "even number" )
]>
</body>
</html>
|
<[
// note: embedded libarary references, classes, and functions
// must be defined first, before any other FGL statements
CLASS objContact
local fname, lname, phone
END
]>
<html>
<head>
<title>Control Structure Sample</title>
</head>
<body text=black bgcolor=white>
<[
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" }
}
! "<table border=1 width=400 cellspacing=0 cellpadding=2>"
! "<tr bgcolor=#F0F0F0><td>i-value</td><td>j-value</td>"
! " <td>data type</td><td>value</td></tr>"
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</td><td>" + a[i,j]
break
case "C"
s = "character</td><td>" + a[i,j]
break
case "O"
s = "object</td><td>" + a[i,j].fname + " " + a[i,j].lname
break
case "U"
s = "undefined</td><td>null"
break
default
s = "unknown type</td><td>" + a[i,j]
end
! "<tr><td>" + i + "</td><td>" + j + "</td><td>" + s + "</td></tr>"
end
end
! "</table>"
]>
</body>
</html>
|
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:
<[
IsFlag = seconds( ) % 2
]>
<html>
<head>
<title>Control Structure Sample</title>
</head>
<body text=black bgcolor=white>
<p>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 )
<%
<p>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
<%
<p>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
]>
<p><input type=button value="Reload" onClick="document.location.reload( );">
</body>
</html>
|
The above example could also have been written as follows:
<[ IsFlag = seconds( ) % 2 ]>
<html>
<head>
<title>Control Structure Sample</title>
</head>
<body text=black bgcolor=white>
<p>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 ) <%
<p>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 <%
<p>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 ]>
<p><input type=button value="Reload" onClick="document.location.reload( );">
</body>
</html>
|
#####
|