;;; sec3.00.mhtml: -*- Meta-HTML -*- 

;;;  Copyright (c) 1996 Universal Access Inc.
;;;  Author: Henry Minsky (hqm@ua.com) Sun Sep 15 11:05:06 1996.

<set-var title="Meta-HTML Control Structure">
<include header.mhtml>
<use-pagelist tutorial>

<plain-text>
  <pagelist-control-panel>

  <h2><Meta-HTML> Control Structure</h2>

  All programming languages provide some means for controlling the order
  in which program statements are executed. Together, the functions
  providing this ability are called a language's <em>control
  structure</em>. In general, a language's control structure consists of
  functions which execute only if certain conditions are true, functions
  which repeat some action while a condition is true, and functions
  which cause a program's execution to stop and begin at some other part
  of the program.

  These functions together are often referred to as
  <em>conditional</em> functions because they do something <em>if</em>
  some condition is true (and often something else if the condition is
  not true).

  Closely related to conditional functions are the relational functions.
  These are a set of functions which test to see if some given
  condition is true or false, and return the <em>logical values</em>
  <code>true</code> or <code>false</code>. These returned values are used
  by the conditional functions to determine what operations to perform next.

  The set of <meta-html> control functions includes:
  <ul>
    <li><funref if>
    <li><funref while>
    <li><funref when>
  </ul>

  And the relational functions include:
  <ul>
    <li><funref and>
    <li><funref or>
    <li><funref not>
    <li><funref lt>
    <li><funref gt>
    <li><funref eq>
  </ul>

  We'll use some of these to demonstrate a very simple way of getting
  repetitious output onto an HTML page.

  As we saw in the first section of this tutorial, if we wanted to use
  plain old HTML to get some text to repeat five times on a page,
  we'd have to write that text over and over, five times. But with <meta-html>
  we can handle the task with a program.
  For example, let's say we want the string <code>"Hello,
  world!"</code> to appear five times in a  row on a page. The HTML
  way of doing this looks something like this:

  <example-table>
  <strong>Hello, <em>world</em>!</strong>
  <strong>Hello, <em>world</em>!</strong>
  <strong>Hello, <em>world</em>!</strong>
  <strong>Hello, <em>world</em>!</strong>
  <strong>Hello, <em>world</em>!</strong>
  </example-table>

  What if you wanted to change the word <code>"world"</code> to
  <code>"mom"</code>? You'd have make five changes. Let's see how we
  could do this a little better using <meta-html>.

  First, we'll want to store the string that's going to be repeated
  into some <em>variable</em>. So we choose an appropriate name
  for the variable, and use the <funref set-var> function to
  create it and set its value:

  <example-table>
  <set-var repeating-string="<strong>Hello, <em>world</em>!</strong>">
  </example-table>

  Once we've done this, the string is safely stored in the variable
  repeating-string, and is waiting for us to use it.

  Next, we'll need to keep some sort of count, so we know how many
  times to repeat. This can be stored in another variable:

  <example-table>
  <set-var count=5>
  </example-table>

  Now we've got something to repeat, and a number of times to
  repeat it, all we need is a test, a little math, and a control function.
  For the test, we'll use the relational function <funref gt>. This 
  function takes two arguments, and returns a logical value of 
  <code>true</code> when the first argument is <em>greater then</em>
  the second, and <code>false</code> otherwise. It's used line this:

  <example-table>
  <gt 4 2>
  <gt 2 4>
  </example-table>

  The first returns <code>true</code> and the second returns an
  <em>empty string</em> (as you remember from the section on return
  values, the empty string is equivalent to a logical value of
  <code>false</code>).

  <divider>

  For the math function, we'll use the <funref sub> function. This 
  function <em>subtracts</em> tits second argument from it's first.
  A simple example might look like this:

  <example-table>
  <sub 5 3>
  </example-table>

  This returns a value of:

  <sub 5 3>

  And finally, to tie all this together, we need a control function.

  We will use the <funref while> iteration function to create
  a loop, and use our <i>count</i> variable and a relational
  operator to control how many times we go around the loop.

  <example-table result>
  <set-var repeating-string="<strong>Hello, <em>world</em>!</strong>"
    count=5>
  <while <gt count 0>>
    <get-var repeating-string>
    <br>
    <set-var count=<sub count 1>>
  </while>
  </example-table>

  <pagelist-control-panel>
</plain-text>

<include footer.mhtml>
