Contents | Index | < Browse | Browse >
2.1 Program Flow

  The program flow is from top down. The program starts with the first
  expression in the source code and keeps on running until it exits with
  "End".
  An "End" is forced if the execution runs into the void. When the
  debugger is used, this will cause a runtime error.
  Be careful that you program does not run into data sections.

2.1.1 Labels

  +-----------------------------------------------------------------------------
  |
  | ['][.]label[:]
  |
  +-----------------------------------------------------------------------------
  
  A label is specified by stating its name with an optional . (full stop) or '
  (apostrophe) in front. It is a good style to use a seperating ":" (colon)
  after the label, however, it is not necessary. 

  To avoid clashes of label names, a label can be made locally unique by using
  the ' (apostrophe) modifier. The label name will be concatenated with the
  previous label, thus being locally unique.
  
  The leading full stop is used to indicate that the label should be visible
  in the source browser of the IDE for quick access.

  Example:
  +-----------------------------------------------------------------------------
  |
  | John:         ; this is a normal label
  |
  | 'Smith:       ; actual label name becomes "JohnSmith"
  |
  | .Jack:        ; this is another label, visible in source browser
  |
  | 'Smith:       ; actual label name becomes "JackSmith" and does not clash with
  |               ; above 'Smith, thus being locally unique.
  |
  +-----------------------------------------------------------------------------

2.1.2 Goto

  +-----------------------------------------------------------------------------
  |
  | Goto label
  |
  +-----------------------------------------------------------------------------
  
  A simple branch is performed. Program execution will continue
  at the position after the label.

  Example:
  +-----------------------------------------------------------------------------
  | 
  | myLabel:
  | 
  | ...
  | 
  | Goto myLabel
  | 
  +-----------------------------------------------------------------------------
  Note: "Goto" is usually considered as a beginner's programming style and
        theoretically can be always avoided. However, there might be
        situation where a "Goto" is handy and makes life easier.

2.1.3 Gosub/Return

  +-----------------------------------------------------------------------------
  |
  | Gosub label
  |
  | Return
  |
  +-----------------------------------------------------------------------------
  
  A branch to a subroutine is performed. Program execution will continue
  at the position of the label, and the address of the calling "Gosub" is
  remembered. When the program execution reaches a "Return", it will jump back
  to the position after the calling Gosub. This is used for subroutines, that
  should be executed in the global context rather than a function that uses its
  own, local context.

  Example:
  +-----------------------------------------------------------------------------
  | 
  | ...
  |
  | Gosub mySubRoutine  ; jump to label "mySubRoutine"
  |
  | ...
  |
  | End                 ; end of program
  |
  |
  | mySubRoutine:
  | 
  | ...                 ; do something ...
  | 
  | Return              ; jump back to the calling Gosub
  | 
  +-----------------------------------------------------------------------------
  Note: "Gosub" is usually considered as a beginner's programming style.
        Gosub is sometimes handy but makes your code unmaintainable if the
        project grows. Usage is only recommended in very small, script-like
        applications. Ohterwise, use functions instead.
        
2.1.4 If/Then/Else 

  To make the execution of a block of statements dependent on a condition
  (expression that evaluates to type boolean), use "If":

  +-----------------------------------------------------------------------------
  |
  | If condition : ... : End If
  |
  | If condition Then ...
  |
  +-----------------------------------------------------------------------------

  The code in the "If" block is only executed, if the condition evaluates to
  true. If the condition is not the result of an comparator, e.g. a number,
  anything else than a 0 (zero) is interpreted as true, while the 0 is
  interpreted as false. In case of strings, all strings are true, except the
  empty string is interpreted as false.
  
  The "End If" is only needed, if the block spans over multiple lines.
  Otherwise they can be omitted and a "Then" is used. All expressions in the
  same line belong then to the conditional block.

  Example:
  +-----------------------------------------------------------------------------
  | 
  | If a=0 Then a=1 : Print "Hello!" ; single line If-block
  |  
  | If a=0                           ; multi line If-block
  |   a=1
  |   Print "Hello!"
  | End If
  | 
  +-----------------------------------------------------------------------------

  For every "If", you can define an alternative "Else" block:

  +-----------------------------------------------------------------------------
  |
  | If condition : ... : Else : ... : End If
  |
  | If condition Then ... : Else ... 
  |
  +-----------------------------------------------------------------------------

  Same rules for multilines apply for the "Else" block.

  Example:
  +-----------------------------------------------------------------------------
  | 
  | If a=0 Then a=1 : Print "Hello!" : Else a=0 : Print "Blabla."
  |  
  | If a=0 
  |   a=1
  |   Print "Hello!"
  | Else
  |   a=0
  |   Print "Blabla."
  | End If
  | 
  +-----------------------------------------------------------------------------

2.1.5 While/Wend

  +-----------------------------------------------------------------------------
  |
  | While condition : ... : Wend
  |
  +-----------------------------------------------------------------------------

  This executes a block of expressions, while the condition evaluates
  to true. Same rules as for the "If" condition apply for numeric or string
  values.

  Example:
  +-----------------------------------------------------------------------------
  | 
  | While a>0
  |   a=a-1 
  |   Print "Hello!"
  | Wend
  | 
  +-----------------------------------------------------------------------------

2.1.6 Repeat/Until

  +-----------------------------------------------------------------------------
  |
  | Repeat : ... : Until condition
  |
  +-----------------------------------------------------------------------------

  This is the same like a while loop, only that the condition is tested
  at the end of the block in contrast to the beginning of the block. Thus, the
  block is always executed at least once.

  Example:
  +-----------------------------------------------------------------------------
  | 
  | Repeat 
  |   a=a-1
  |   Print "Hello!"
  | Until a<=0
  | 
  +-----------------------------------------------------------------------------

2.1.7 For/Next/Step

  +-----------------------------------------------------------------------------
  |
  | For var=a To b [Step increment] : ... : Next 
  |
  +-----------------------------------------------------------------------------

  A "For" loop is a loop that sets a counter variable to an initial value a,
  executes a block of expressions and then de/increments the counter until it
  reaches the "To" value b.
  increment is the value added to the counter variable for each step,
  and is 1 if omitted. increment may also be negative for reverse loops.
  
  Examples:
  +-----------------------------------------------------------------------------
  |
  | For n=a To b : ... : Next        ; n is incremented by 1 for each cycle
  | For n=a To b Step c : ... : Next ; n is incremented by c for each cycle
  |
  +-----------------------------------------------------------------------------
  
  A real example (output the first 100 positive integer numbers):
  +-----------------------------------------------------------------------------
  |
  | For n.l=1 To 100 
  |   NPrint n
  | Next
  |
  +-----------------------------------------------------------------------------

2.1.8 Select/Case

  "Select" is selecting a "Case" block for execution that matches a previously
  given value. If none of the "Case" blocks matches the value, the optional
  "Default" block is exectued.
  
  +-----------------------------------------------------------------------------
  | 
  | Select value
  |   Case alternative1
  |     ...
  |   Case alternative2
  |     ...
  |   ...
  |   Default 
  |     ...
  | End Select
  | 
  +-----------------------------------------------------------------------------

  The Select/Case construct should be prefered over a cascade of "If" block, if
  a value is checked against more than one alternative.
  
  Example:
  +-----------------------------------------------------------------------------
  | 
  | Select a.l
  |   Case 1   : NPrint "Value is 1"
  |   Case 2   : NPrint "Value is 2"
  |   Default  : NPrint "Value is something else..."
  | End Select
  | 
  +-----------------------------------------------------------------------------
  Note: Unlike C "switch", no "break" is needed after each block.

2.1.9 End/initbasic

  +-----------------------------------------------------------------------------
  | 
  | End
  | 
  +-----------------------------------------------------------------------------

  "End" frees all resources (that AmiBlitz³ is aware of...) and terminates
  the program. All programs, including libraries, must have this function as the
  last instruction.

  +-----------------------------------------------------------------------------
  | 
  | initbasic
  | 
  +-----------------------------------------------------------------------------

  "initbasic" will move the initialization code of AmiBlitz³ to this line.
  It is not allowed to execute any AmiBlitz³ code before this function is called,
  *if* it is called.
  If there is no initbasic, this will be done before the first line of source code.
  "initbasic" should be called only once or never! This is for advanced
  programming only.


Contents | Index | < Browse | Browse >