[SparForte][Banner]
[Top Main Menu] Intro | Tutorials | Reference | Packages | Examples | Contributors   [Back Page]      [Next Page]  

Flow of Control

The If Statement

if statements are used for conditional branching.

if x > y then
   put_line( "x is greater than y" );
elsif x = y then
   put_line( x is equal to y" );
else
   put_line( x is less than y" );
end if;
 

Example: The If Statement

The Case Statement

The case statement can test a variable for several different values.

type country is ( australia, u_k, brazil );
c : country;
...
case c is
when austraila =>
   put_line( "Australia" );
when u_k =>
   put_line( "United Kingdom" );
when brazil =>
   put_line( "Brazil" );
when others =>
   put_line( "Unexpected country" );
end case;


 

Example: The Case Statement

Multiple cases can be strung together using a vertical bar (|).

when brazil | u_k => -- brazil or U.K.

The "when" cases must not be variables (although constants are OK). The "when others" case is always required. You can create a do nothing case by using the null statement.

Ada: The when others clause is optional in Ada. It's required in SparForte.

The While Loop Statement

The while loop is a pre-test loop.  The commands in the loop are repeat while the condition is true and the condition is tested each time the first line of the loop is executed.

while x < y loop
  x := @ + 1;
end loop;
 

Example: The While Statement

The For Loop Statement

The for loop increments its index variable by 1 until it iterates through the specified range. The range can either be numeric or enumerated.

The index identifier is automatically declared for you and only exists for the scope of the the loop.  The index identifier is declared as a constant: you cannot assign a new value to it inside of the loop. (However, it can be changed at a breakout prompt for debugging purposes.)

for i in 1..10 loop
  put( "i is " );
  put_line( i );
end loop;
 

Example: The For Statement

To loop through values in the reverse order, use "in reverse" instead of "in".

The Exit Statement

Any loop can be exited by using either an exit statement or an "exit when" shorthand.

if x > 100 then
  exit;
end if;

 
exit when x > 100;
 

Example: Examples of the Exit Statement

The Loop Loop Statement

A "loop" loop is a general purpose loop. It can only be exited with "exit".

loop
  reply := get_line;
  exit when reply = "exit";
end loop;
 

Example: The Loop Statement

 

[Right Submenu]

 AdaScript versus GCC

 Case Sensitivity

 Reserved Words

 Comments

 Literals

 Bourne Shell Word Expansions

 Fundamental Types

 User-defined Types

 Enumerated Types

 Arrays

 Records

 Basic Assignment

 The @ and % Operands

 Command Argument Shortcuts

 Redirection and Pipelines

 Command Line Interaction

 Built-in Shell Commands

 The Current Directory

 Database Commands

 Flow of Control

 Other Statements/ Subprograms

 External Commands

 Block Statements and Subprograms

 TCP/IP Sockets

 Numeric Formatting with Put

 Interpreter Directives

 Command Line Options

 Command Reference

 ASCII and Latin_1 Character Sets

 Common Error Messages

 Common PHP Functions and the SparForte Equivalent

[Back to Top] Back To Top [Small Forte Symbol]