Computers: Universe: Basic Conditions

Condition handling: IF

To execute a statement only on a particular condition, use an IF statement like this:

IF VALUE = 0 THEN PRINT 'Value is 0'

To execute a different statement if the condition is not true, add an ELSE clause:

IF VALUE = 0 THEN PRINT 'Value is 0' ELSE PRINT "Value isn't 0"

More than 1 line can be made conditional by beginning a new line immediately after then THEN, and closing the clause with an END:

IF VALUE = 0 THEN
   PRINT 'The value is 0.'
   PRINT 'No invoice will be printed.'
END

If you wish to add an else clause, use END ELSE to terminate the THEN clause, and END to close then ELSE clause.

IF VALUE = 0 THEN
   PRINT 'The value is 0.'
   PRINT 'No invoice will be printed.'
END ELSE
   PRINT 'This invoice has a value of ' : VALUE
   PRINT 'Invoice to be prnted...'
END

Finally, IF statements may be nested:

IF VALUE = 0 THEN
   IF QUANTITY = 0 THEN
      PRINT 'The invoice has no quantity'
	  PRINT 'and no value.'
   END ELSE
      PRINT 'There has been a mistake.'
	  PRINT 'The quantity is ' : QUANTITY
	  PRINT 'The value should not be zero.'
   END
END ELSE
   PRINT 'This invoice has a value of ' : VALUE
   PRINT 'Invoice to be prnted...'
END

Note the identation of the lines above. This is done automatically by the editor's FORMAT command, and is extremely useful. If an END statement does not line up with the IF statement which set the condition, then somewhere along the way you have omitted an END, or a THEN, or in some other way made a mistake in your code: so this forms a useful check.

Multiple condition handing: CASE

A commonly asked question is 'does UniVerse Basic support ELSEIF?'. The answer is no, but it does provide the same facility in the form of the CASE block.

A case block begins with BEGIN CASE and ends with END CASE. Between these lie one or more CASE statements, each followed by a particular condition. When UniVerse reaches BEGIN CASE, it then examines each CASE condition in the block until it finds one which is true. It then starts executing the statements under that CASE statement until it encounters either the next CASE or the block's END CASE. Finally, it transfers control to the statement following the END CASE.

As ever, a written description can make something sound more complicated than it is. Consider instead the following example:

BEGIN CASE
   CASE TYPE = 'I'
      PRINT 'Processing INVOICE document...'
      * Process INVOICES here.
   CASE TYPE = 'C'
      PRINT 'Processing CREDIT NOTE document...'
      * Process CREDIT NOTES here.
   CASE 1
      PRINT 'Invalid document type: not processed.'
END CASE

Each time the code above is run, only one of the three messages will be printed: as soon as UniVerse Basic has found a true CASE, the others are ignored. Note the CASE 1: this is a common and useful trick. As the value 1 is logically true (see Basic Types), the statements between the CASE 1 and the END CASE will always be executed if none of the other CASEs are true: a handy safety net. Of course, CASE 1

will always be the last CASE in the list: any cases below it would never be reached, as CASE 1 is always true.

Such constructs are more efficient if the cases more likely to be true are put at the top (providing, of course, that the arrangement doesn't alter the logic away from that desired, as moving CASE 1 above would). To explain this, imagine that the code above is used to process 9000 invoices, 900 credit notes, and 100 invalid types. To discover an invoice takes only one comparison: TYPE = 'I' must be true. To find a credit note takes two: TYPE = 'I' must be false, and TYPE = 'C' true. Invalid types take three. Thus, the total number of comparisons made is 9000 * 1 + 900 * 2 + 100 * 3 or 11,100. However, reverse the order of the first two tests, and suddenly invoices will require two tests, and credit notes only one. The number of comparisons made would become 900 * 1 + 9000 * 2 + 100 * 3 or 19,200: getting on for twice as many.