In this article, we shall be discussing condition testing – facilities offered by a programming language to allow the programmer to change the flow of control.
Condition testing allows the programmer to determine the path through a program by selectively executing code based on the comparison of a value against an expression.
The value can be a constant, a variable, another expression, or result of a function. The veracity of the test (is it true or false, do the values match?) determines whether the code that follows is executed or not.
Each condition statement will contain a statement to test, possibly a start of code block identifier, an end of code block identifier, and some code to execute. The code block must be contained in some way so that the computer knows where the conditionally executed code ends, and the rest of the program carries on.
All programming languages have a type of condition testing statement known as the 'if' statement. In the BASIC language they look like:
IF A = 1 THEN ...
In the C language (and C styled variants):
if (a == 1) ...
It is also worth pointing out that where C (and variants) encloses the code in curly braces - { and } - most procedural languages like Modula, Pascal, BASIC and so on use an IF ... THEN ... END IF construction.
There are also some language specific differences in the various operators used in comparison testing. By and large, the equivalence operators for mathematical types are common:
Logical (Boolean) operators vary slightly, ie && (AND), || (OR), ! (NOT), as do the behaviours of comparisons involving functions and return values.
The left hand side and right hand side of the expression must evaluate to the same data type otherwise the computer will not be able to perform the comparison correctly.
Most programming languages also allow for the programmer to define a block of code to be executed should the validation fail. Known as an 'else' statement, there are usually two flavours:
The 'else if' construction allows the programmer to test an alternative condition and then execute a code block according to the outcome. Usually, programming languages allow these to be mixed, permitting the programmer to construct a complex set of conditions and outcomes.
In order to prevent overuse of multiple if statements, many programming languages provide a 'case' statement to validate various scalar values (numbers, characters, etc.) rather then creating an 'if' for every condition.
This is also known as a 'select' or 'switch' statement in some programming languages, and has a condition part, and a series of value labels. Each value label also has a code block associated with it, allowing a single test to be validated against multiple constant values and selectively execute code accordingly.
There are other aspects to flow control which allow conditional execution of code (see the Loops tutorial) and the possibility for the programmer to define specific blocks of code for a specific purpose, and call the block by name (see the Functions, Procedures etc. tutorial) and mastering all three types is usually a pre-requisite for being able to use a programming language effectively.
Stay informed - sign up to the mailing list!