MainframeSupports
tip week 10/2011:

In COBOL it is possible to write conditions in almost the same way as when you use written language. An example: "The color is green, yellow or red" can be expressed as COLOR = 'GREEN' OR 'YELLOW' OR 'RED' where other languages will force you to write COLOR = 'GREEN' OR COLOR = 'YELLOW' OR COLOR = 'RED' to acheive the same condition. It is possible to shorten other conditions than OR, but try it out to see that you get the expected result before you put it into production.

If I want to express the condition "the color must be green, yellow or red and must be made in Denmark", I will have to write (COLOR = 'GREEN' OR COLOR = 'YELLOW' OR COLOR = 'RED') AND COLORCOUNTRY = 'DENMARK' in other programming languages. In COBOL I will be tempted to express it like COLOR = 'GREEN' OR 'YELLOW' OR 'RED' AND COLORCOUNTRY = 'DENMARK', but unfortunately COBOL interprets this condition as COLOR = 'GREEN' OR COLOR = 'YELLOW' OR (COLOR = 'RED' AND COLORCOUNTRY = 'DENMARK') which only makes the condition work as expected when COLOR has the value 'RED'. Ugh, ugh, ugh, that hurt some of the programs on an installation I once worked on. The result were production errors. You must remember to use parenthesis in your conditions when you combine AND and OR with value lists. The correct condition for the previous example is acheived by writing (COLOR = 'GREEN' OR 'YELLOW' OR 'RED') AND COLORCOUNTRY = 'DENMARK' even though this is far from obvious.

Previous tip in english        Sidste danske tip        Tip list