



    ________________________________________________________________________
                                      Chapter 6: Control Structures   51
    ________________________________________________________________________


    CHAPTER SIX: CONTROL STRUCTURES

    HyperPAD usually executes commands in the sequence they appear in your
    script. Control structures change this linear sequence. You can create
    loops, conditional statements (commands that get executed only if
    certain conditions exist), and selection statements (which select a
    block of statements from a list).

    HyperPAD's control structures are similar to Pascal's:

    if...then...else       conditional
    case                   conditional
    while...do             looping
    repeat...until         looping
    for...do               looping

    -----------------------------------
    IF STATEMENT

    Syntax:
    if <expression> then
       <statement>
    [else
        <statement>];

    Purpose:  The if structure specifies that a statement should be executed
    only if a certain condition is true. If the condition is false, then
    either no statement is executed or the else statement is executed. The
    if statement responds directly to the user of your pad by executing
    commands based on the user's actions. For example, if step four is not
    completed, then do not allow the user to advance to the next page.

    <Expression> must return a Boolean value (either true or false).

    <Statement> is a single PADtalk statement. Multiple statements are
    enclosed in a begin...end block.

    Note:  The entire if statement ends with a semi-colon.

    Examples:

    A simple if statement:

    if it is "Ok" then quit;



    ________________________________________________________________________
                                      Chapter 6: Control Structures   52
    ________________________________________________________________________


    An if statement that executes more than one statement:

    if page field 1 contains "lawyer" then
      begin
        visual effect scroll up;
        go to the next page;
      end;

    A complex if..then..else statement:

    if field "Last Name" is empty then
      begin
        go to page 1;
        put "went to page 1";
      end
    else
      begin
        go to page 2;
        put "went to page 2";
      end;

    -----------------------------------
    CASE STATEMENT

    Syntax:
    case <expression> of
    <expression>  :  statement;
    <expression>  :  statement;
            :
            :
    [otherwise  :  statement;]
    end;

    Purpose: The case statement give scripts the power to choose from more
    than two alternatives without specifying numerous if statements.
    With a case statement, you set up a number of conditions, each with its
    own "action". When the case statement is executed, the action associated
    with the met condition is executed.

    A case statement consists of an expression (the selector) and a list of
    statements, each associated with an expression. The case statement
    selects for execution the statement that is equal to the current value
    of the selector. When the statement has completed execution, control
    goes to the end of the case statement.

    You are not limited to a single statement for each expression. If you
    want to execute more than one statement if a match is found, use the
    begin...end construct to enclose the statements.



    ________________________________________________________________________
                                      Chapter 6: Control Structures   53
    ________________________________________________________________________


    If there is no match for the selector, HyperPAD executes the statement
    following the word otherwise. Be sure to include an otherwise statement
    if there is a possibility that no matches will be found or HyperPAD will
    give a runtime error. Your otherwise statement does not have to generate
    an action, simply create a blank statement after that expression.

    Examples:

    The following is a typical case statement:

    case mycolor of
      "blue"   : go to page id 4;
      "red"    : begin
                   visual effect scroll up;
                   go to page id 4;
                 end;
      otherwise: answer "Don't know this color.";
    end;

    The following illustrates the versatility of the case statement. The
    selector and each of the possible matches are expressions. If no match
    is found, nothing happens.

    case trim(salary) of
      4 + 5/yearly_salary   : put salary*100 into msg;
      56*min(field1,field2) : put salary*200 into msg;
      otherwise             : ;
    end;

    Comments:

    A common mistake when scripting is to forget the end; that ends the case
    statement. You must end each begin...end construct as well as the entire
    case statement.

    If you do not want a statement to be executed when a match is found, use
    an empty statement (just place a semi-colon after the colon). For
    example:

    "blue" : ; -- do nothing



    ________________________________________________________________________
                                      Chapter 6: Control Structures   54
    ________________________________________________________________________


    The selector is evaluated for every comparison in the case statement.
    For speed, keep the most often matched cases near the top. If the
    selector involves a complex expression, put the result of the expression
    into a temporary variable first, then use the temporary variable as the
    selector, like in the following example:

    get page field 1 & page field 2;
    case it of
      "jim"     : put it after page field 1;
      "john"    : put it;
      otherwise : answer "Don't know you!";
    end;

    HyperPAD case statements follow a looser syntax than their Pascal
    counterparts. HyperPAD allows any valid expression to be used as a
    match, but in Pascal, only ordinals can be used as matches. This makes
    the case statement especially handy for evaluating text, as in the
    following example:

    case (word 2 of item 6 of line 4 of field "last name") of
      "smith"   : go to pad "smith";
      "jones"   : go to pad "jones";
      "fisher"  : go to pad "fisher";
      otherwise : go to page "help";
    end;

    -----------------------------------
    WHILE...DO STATEMENT

    Syntax:
    while <conditional expression> do
      statement;

    Purpose:  A while loop is used to execute a statement repeatedly while a
    condition remains true. As soon as the condition is false, the loop
    terminates execution. The while loop will skip the statement if the
    condition is initially false.

    To execute numerous statements inside the loop, enclose them in a
    begin..end block.



    ________________________________________________________________________
                                      Chapter 6: Control Structures   55
    ________________________________________________________________________


    Examples:

    This loop cycles through all the pages of a pad:

    put 1 into count;
    while count < the number of pages do
      begin
        visual effect fade;
        go to the next page;
        add 1 to count;
      end;

    This loop locates the last backslash in dirname:

    while (i>1) and (character i of dirname is not "\") do
      subtract 1 from i;

    -----------------------------------
    REPEAT...UNTIL STATEMENT

    Syntax:
    repeat
      <statement 1>
      <statement 2>
           :
           :
    until <condition>;

    Purpose:  The repeat...until control structure executes a block of
    statements until some condition is met. The sequence of statements is
    executed at least once. After each execution, the condition is
    evaluated. When the condition becomes true, execution falls out to the
    bottom of the loop.

    Examples:

    The following example prints out all the names in a pad:

    set the printer to on;
    put the number of pages into count;
    repeat
      print field "last name" & return;
      subtract 1 from count;
    until count < 1;
    set the printer to off;

    This example waits for the mouse button to be clicked:

    repeat until mouseClick();



    ________________________________________________________________________
                                      Chapter 6: Control Structures   56
    ________________________________________________________________________


    Comments:

    The differences between while and repeat are as follows:

    1.  Statements in a repeat loop are always executed once. A while loop
    executes only if  a specific condition is met, but be careful; the
    while statement will not execute its statement at all, if the condition
    is initially false.

    2.  The repeat control structure executes statements while a condition
    is false. The while control structure executes statements only while a
    condition is true. These two scripts point out the difference between
    repeat and while:

    repeat                   while (i = 10  do
     :                       begin
     :                        :
     :                       end;
    until (i = 10);

    3.  Repeat can hold multiple statements without enclosing them in a
    begin...end block.

    -----------------------------------
    FOR STATEMENT

    Syntax:
    for <variablename> = <start value> [down] to
      <stop value> [step<increment>] do statement;

    <variable name> is the index variable to be stepped through the range of
    values.

    <start value> is the initial value of the index.

    <stop value> is the ending value of the index.

    <increment> is the amount to be added to the index each time through the
    loop. By default, this is 1 (or -1, if "down to" is specified).

    Purpose:  A for loop executes a statement a fixed number of times. The
    index counts the number of executions as it loops through the script.
    When it reaches the specified number, it drops out of the loop. This is
    especially useful because the counting variable can also be used inside
    the loop. For example:

    For i = 1 to 10 do
      go to page i;

    The counting variable i is used inside the loop.



    ________________________________________________________________________
                                      Chapter 6: Control Structures   57
    ________________________________________________________________________


    By default, the index is incremented by 1 each time through the loop.
    You can change the increment by adding a step value, which is, a value
    (or amount) added to the index each time through the loop. For example:

    For i = 1 to 10 step 2 do go to page i;

    Specify a downward direction for the index by giving a start value that
    is less than the end value and using the keyword down. This causes the
    index to be decremented by 1 each time through the loop.

    For example:

    For i = 10 down to 1 do go to page i;

    To specify more than one statement to be executed each time through the
    loop, enclose the statements in a begin...end block.

    Examples:

    Cycle through 10 pages:

    for i = 1 to 10 do
      go to the next page;

    Count how many directories deep we are:

    put the directory into dirName;
    put 0 into count;
    for i = length(dirName) down to 1 do
    if character i of dirName is "\" then add 1 to count;

    Ends up with 11 in the Message Box:

    put 1;
    for i = 1 to 100 step 10 do
      add 1 to msg;

    Ends up with 21 in the message box:

    put 1;
    for i = 100 down to 1 step 5 do
      add 1 to msg;



    ________________________________________________________________________
                                      Chapter 6: Control Structures   58
    ________________________________________________________________________


    The following handler prints out a cosine graph taken from the "Pascal
    User Manual and Report":

    handler select;
    begin
      put 16 into xlines; --line spacing/abscissa unit
      put 32 into xlimit; --char widths/ordinate unit
      put 34 into zeroy; --length of graph in lines
      put 1/xlines into delta;
      put 8*atan(@procedurelist = 1.0) into twopi;
      put empty into it;
      for point = 0 to xlimit do
        begin
          put delta * point into x;
          put exp(-X)*sin(twopi*x)into y;
          put round(scale*y) + zeroy into ypos;
          repeat
            put space after it;
            subtract 1 from ypos;
          until ypos = 0;
          put "*" & return after it;
        end;
      put it into page field 1;
    end;