Condition Testing BASIC ProgramsUsing the IF THEN ELSE ENDIF decision making statements in BBC BASICFeb 22, 2007 Guy Lecky-Thompson
Part of the Learn to Program with BBC BASIC tutorial, this concentrates on decision making and condition testing using the IF THEN ELSE and ENDIF keywords.
IntroductionThis article is part of the Learn to Program with BBC BASIC series, and covers ways in which we can implement decision making in BASIC programming. For a primer, we recommend the Testing Conditions in Programming article, especially for beginning programmers. Condition TestingDecision making in programming requires that a given condition is tested against:
In other words, we take a variable value (see Using Variables in Programming) and test it against something we know to be true. We can also test this against something we know might have a value which matches the variable, to yield a TRUE or FALSE result. Furthermore, we could also test the variable value against another condition which may, in turn, yield a variable or TRUE/FALSE result. In essence, we are testing to see whether the condition or conditions, when taken together, resolve to TRUE or FALSE. Conditions are not only used in the IF THEN construct; we can also use them in repetition programming, for example. The remainder of the article covers the basic IF THEN usage. Single Line IF THENThe simplest form of the IF statement can be written as a single line: IF nMyAge 100 THEN PRINT "Wow!" We can break this down into pieces: IF <condition> THEN <action> The condition must resolve to a TRUE or FALSE value, and the action can be any statement or sequence of statements (compound statement) supported by BBC BASIC. The THEN keyword is mandatory with one notable exception - when used in conjunction with the GOT keyword, where it is not allowed, as in: IF nMyAge 100 GOTO 100 We can omit the GOTO, and retain the THEN, but this will inevitably result in less readable code: IF nMyAge 100 THEN 100 Besides the IF THEN mechanism, we also have the possibility to tell the program to do something else should the condition test fail (evaluate to FALSE). The ELSE keyword is used for this, and the same rules apply as regards the action part of the statement, of which the generic form is: IF <condition> THEN <action> ELSE <action> An example of this statement in a program might be as follows: IF nTotalCost 100 THEN PROC_ApplyDiscount ELSE PROC_SevereMarkup The above is readable enough, but there is bound to come a time, especially when using multiple compound statements, that the line becomes unmanageably long. In such cases, we can use the IF .. THEN .. ENDIF construct, which allows us to break the code up over multiple lines. Multi-Line IF THEN ... ENDIFThe following is a very simple generic skeleton of this in action: IF <condition> THEN <action> <action> <...> <action> ELSE <action> <...> <action> ENDIF Note that there may be one or more action statements at each decision level. The multi-line version of the IF THEN ELSE construct simply breaks the line up over several lines. Nesting IF THEN ELSE ENDIF StatementsSince the action parts can contain any supported BASIC statements, we might be tempted to write code that tests multiple conditions in a single statement. The alternative is to use nesting. The only limit to nesting is the size of the stack. In other words, each time a new level is introduced the program pushes its' current state onto the stack, contained in available local memory. If the nesting is too deep, and the stack becomes full, a 'Stack Overflow' error will result. An example of nesting might be: IF nOrderAmount 100 THEN IF nOrderAmount 200 THEN PROC_ApplySmallDiscount ELSE PROC_ApplyLargeDiscount ENDIF ENDIF There is an alternative, covered in a future article - use the CASE statement. Links to Flow Control & Related Programming Articles
The copyright of the article Condition Testing BASIC Programs in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Condition Testing BASIC Programs in print or online must be granted by the author in writing.
Related Topics
Reference
More in Technology
|