Freelance Writing Jobs | Today's Articles | Sign In

 

Testing Conditions in Programming

Determining the path through a program by selectively executing code based on the comparison of a value against an expression.

Apr 11, 2006 Guy Lecky-Thompson

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.

Introduction

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.

The 'if' statement

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.

Expressions

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:

  • = or ==, , != or

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.

Alternative 'if's

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:

  • Else - if validation fails do this
  • Else if - if validation fails test another condition

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.

The 'case' Statement

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.

Conclusion

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.

Navigation Links

Mailing List

Stay informed - sign up to the mailing list!

The copyright of the article Testing Conditions in Programming in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Testing Conditions in Programming in print or online must be granted by the author in writing.
Lost Bytes, Carsten Mueller Lost Bytes
   
What do you think about this article?

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
post your comment
What is 7+8?
;