Common Statements

Assignment statements

You have already used an assignment statement, and it is probably the most commonly used part of any programming language. The Equal Symbol (=) is used to assign a value to a variable or array. Take the following examples:

a=42
a#=99.9
a$="HELLO"
lottery[1,1]=49

Branch statements

Normally, a program executes statements in sequence starting at the top. A branch statement allows you to jump to another part of the program to continue execution. A GOSUB command will jump to a label and continue from its new location. When the program encounters a RETURN command, the program will jump back to the GOSUB from where it originally came. Take the following example:

PRINT ( "Hello" )
GOSUB MySubroutine
END
 
MySubroutine:
  PRINT ( "World" )
RETURN

The program will print the "Hello" text to the screen, then jump to the MySubroutine line of the program and continue execution. The next command it finds will print "World" to the screen. The RETURN command then returns the program to the point it left, where it then proceeds onto the next command after the GOSUB command which in this case is the END command.

A GOTO command however, does not remember from where it jumped and will continue running from its new location permanently. It is not recommended you use GOTO commands often, as there are better ways to control the flow of your programs. Here is an example, however, of a simple GOTO command:

MyLabel:
  PRINT ( "Hello World " )
  SYNC()
GOTO MyLabel

Or alternatively, a better way would be:

DO
  PRINT ( "Hello World " )
  SYNC()
LOOP

You will agree the last example is a much better, cleaner and friendly way of doing the above and demonstrates how the use of GOTO can be eliminated. GOTO is retained in the AGK language for compatibility with older BASIC languages.

For next statements

You may recall the use of the FOR NEXT statement in earlier examples. The FOR NEXT commands are used to create a finite loop in which a variable is incremented or decremented from a value to a value. A simple example would be:

FOR T=1 TO 5
  PRINTC ( T )
  PRINTC ( " " )
NEXT T
PRINT ( "Done" )

The output to the screen would read:

1 2 3 4 5 Done

The program would set T to a value of 1 and then go to the next two lines to print the value of T followed by a space. After printing, the NEXT command would return the program to the FOR command and increment the value of T to make it 2. When the PRINT commands are encountered again, the value of T has changed and a new value is printed. This continues until T has gone from 1 through to 5, then the loop ends and the program is permitted to continue. The next command after the NEXT statement prints "Done" to the screen showing the program has left the loop.

You can also nest loops to create a loop within a loop, as the following example shows:

FOR A=1 TO 5
  PRINTC ( "MAIN A=" )
  PRINT ( A )
  FOR B=1 TO 10
    PRINTC ( "LITTLE B=" )
    PRINT ( B )
  NEXT B
NEXT A

The FOR NEXT statement loops the main A variable from 1 to 5, but for every loop of A the FOR NEXT statement inside the first loop must also loop its variable B from 1 to 10. This is known as a nested loop as the loop in the middle is nested inside an outer loop.

Such loops are especially useful for working on array data by using the variables that increment as position indexes for the arrays. As an example, we could list all our lottery numbers using the following example:

FOR week=1 TO 52 STEP 4
  PRINTC ( "LOTTERY NUMBER FOR WEEK " )
  PRINTC ( week )
  PRINTC ( " ARE " )
  FOR index=1 to 6
    PRINTC ( lottery[week,index] )
    PRINT ( " " )
  NEXT index
NEXT week

Notice the new STEP command added to the end of the FOR NEXT statement. The STEP command is used to change the default increment value from 1 to another value. In this case, the program will only print the lottery numbers for every fourth week.

If then statements

The IF statement allows your program to make decisions that controls the flow of your program. The IF statement requires an expression to evaluate results as either true or false. If the expression is true, the commands following the THEN command will be executed. If the expression is false, the program will move onto the next statement and ignore the rest of the IF THEN statement. Take the following example:

age=20
IF age>=16 THEN PRINT ( "You can buy a lottery ticket" )

This program demonstrates a simple IF THEN Statement. To understand how this works we must look at the IF command in a little more detail. First, we must take the expression and evaluate it:

age>=16

We can determine from our earlier coverage of operators, that this relational operator will result in either a zero or a one depending on whether age is greater or equal to 16. The IF command considers a value of zero to be false and all other values as true. So we can determine that if age is indeed greater or equal to 16 then the result will be 1, and the expression according to the IF command will be true.

The expression can be any combination of values, variables, arrays and operators providing the expression makes sense. These expressions will make sense:

IF A THEN PRINT ( "ok" )
IF A = B THEN PRINT ( "ok" )
IF A > (B - 5) THEN PRINT ( "ok" )
IF A = (B + (A * 2)) THEN PRINT ( "ok" )
IF A=1 AND B=2 THEN PRINT ( "ok" )
IF A#=1.5 OR LOTTERY[10,2]=20 THEN PRINT ( "ok" )

These expressions will not make sense:

IF A = B = THEN PRINT ( "not ok" )
IF > A = B THEN PRINT ( "not ok" )
IF A B THEN PRINT ( "not ok" )
IF AND A THEN PRINT ( "not ok" )
IF B OR THEN PRINT ( "not ok" )

On occasions where one line is not enough after the THEN command, you can use the IF ENDIF statement. Using the same IF logic as above, instead of a THEN Command, simply provide your commands to be executed on the lines following the IF command. You must then mark the end of the commands to be executed with an ENDIF command, as the following example shows:

IF A = B
  PRINT ( "Hello A and B!" )
ENDIF

This is the same as:

IF A = B THEN PRINT ( "Hello A and B!" )

But the main advantage is that the first piece of code can be adapted to do this:

IF A = B
  PRINT ( "Hello A!" )
  PRINT ( "Hello B!" )
  PRINT ( "Hello A and B!" )
  PRINT ( "Hello B and A!" )
  PRINT ( "Hello Everything!" )
ENDIF

You can also respond to an IF command if the expression turns out to be false. In cases where you wish to execute a different piece of code if the condition is false, the ELSE command should be used as follows:

IF A = B
  PRINT ( "The values are the same!" )
ELSE
  PRINT ( "The values are different!" )
ENDIF

It is important to make sure that you always use an ENDIF when THEN is not in use. You will note ENDIF is used whether or not the ELSE command is utilized.

You can also make use of the ELSEIF statement, which only executes further statements based on a condition being true, such as:

IF A = 1
  PRINT ( "The value of A is 1!" )
ELSEIF A=2
  PRINT ( "The value of A is 2!"  )
ENDIF

Print Statements

The PRINT command is capable of printing out more than a single value. The command allows you to specify a list of data items that can be printed one after the other on the same line. The data items can be of any type. Although the use of PRINT has been frequent in the above examples, there are some unique features you may not be aware of. To print out more than one value on a line you can do this:

a = 1
b = 2
name$ = "richard"
PRINT ( name$ + STR ( a ) + STR ( b ) )

Note that the STR command is used to convert numbers into a string that is compatible with the PRINT command.

When the PRINT command is used to print data to the screen, the print cursor that is used to paste the individual letters to the screen resets to the left of the screen and one line down when the print is complete. A string of PRINT commands will print to the screen one line at a time. You can change this by leaving the cursor at the end of the printed line after a PRINT command. You achieve this by using the alternative command PRINTC, for example:

PRINTC ( "Hello " )
PRINT ( "World" )

There are much more sophisticated text commands in AGK that handle bitmap fonts, colors, sizes and styles but you will discover these as you explore the rest of the help system.

End and Exit statements

The END command will terminate the execution of a program and end the application immediately.

The EXIT command can be used to escape a conditional loop early, often as the result of a particular objective being achieved. For example:

FOR T=1 TO 100 STEP 10
  PRINT ( T )
  IF T=51
    PRINT ( "got to 51!" )
    EXIT
  ENDIF
NEXT T