section7 - Business Oriented Programming Course 92-252-01...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Business Oriented Programming January 20, 2005 Course 92-252-01 – Spring 2005 SECTION 7 – Selection Constructs In most procedural languages, (If - End If) and Case/Switch are the only selection constructs supported. COBOL supports advanced versions of both of these constructs, but it also introduces the concept of Condition Names - a kind of abstract condition. When a program runs, the statements are executed one after another in sequence unless a statement is encountered that alters the order of execution. An IF statement is one of the statement types that can alter the order of execution in the program. An IF statement allows the programmer to specify that the block of code is to be executed only if the condition attached to the IF statement is satisfied. When an IF statement is encountered in a program, the Statement Block following the THEN is executed, if the condition is true, and the Statement Block following the ELSE (is used) is executed, if the condition is false. The Statement Block(s) can include any valid COBOL statement including further IF constructs, PERFORMs, etc. Although the scope of the IF statement may be delimited by a full-stop (the old way), or by the END-IF (the new way), the END-IF delimiter should always be used. The END-IF makes explicit the scope of the statement. Using a full stop to delimit the scope of the IF can lead to problems. For instance, the two IF statements below are supposed to perform the same task. But the scope of the one on the left is delimited by the END-IF, while that on the right is delimited by a full stop. Statement1 Statement2 IF VarA > VarD THEN Statement3 Statement4 END-IF Statement5 Statement6. Statement1 Statement2 IF VarA > VarD THEN Statement3 Statement4 Statement5 Statement6. Unfortunately, in the IF on the right, the programmer has forgotten to follow Statement4 by a delimiting full stop. This means that Statement5 and 6 will be included in the scope of the IF (i.e. will only be executed if
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
the condition is true) by mistake. If you use full stops to delimit the scope of an IF statement, this is an easy mistake to make and, once made, it is difficult to spot. A full stop is small and unobtrusive compared to an END-IF Condition Types o Simple Conditions : Relation, Class, Sign o Complex Conditions o Condition Name Simple and Complex conditions are examined in this section, but Condition Names are so important that they are covered separately in the next section. Relation Condition may be used to test whether a value is less than, equal to, or greater than, another value. In the comparison we can use the full words or the symbols shown. There is no symbol for NOT; you must use the word if you want to express this condition. When a condition is evaluated, it evaluates to either True or False. It does not evaluate to 1 or 0.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

section7 - Business Oriented Programming Course 92-252-01...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online