Difficulty: Medium
Correct Answer: MOVE A TO B simply copies the value of A into B, while COMPUTE B = A evaluates an arithmetic expression and assigns the numeric result to B, applying numeric conversion rules
Explanation:
Introduction / Context:
Certain procedural languages such as COBOL provide both MOVE and COMPUTE statements for assigning values to variables. Although these statements can sometimes appear to do the same thing, they have different semantics and are intended for different purposes. Understanding this distinction is important for writing correct numeric code and avoiding subtle conversion or rounding issues.
Given Data / Assumptions:
Concept / Approach:
The MOVE statement performs a straight copy from source to destination according to COBOL move semantics, which may include some data conversion and truncation, but does not evaluate any arithmetic expression. The COMPUTE statement is designed for arithmetic. It can evaluate expressions involving operators such as plus, minus, multiply, and divide. Even when the expression is as simple as B = A, the COMPUTE mechanism still treats the operation as a numeric assignment, applying numeric conversion rules and possibly using intermediate precision. This difference becomes important when data types differ or when editing and rounding rules apply.
Step-by-Step Solution:
Step 1: Recall that MOVE A TO B is used primarily for copying data values from one field to another.Step 2: Understand that MOVE does not perform arithmetic; it copies and may convert based on picture clauses.Step 3: Recall that COMPUTE B = expression is used for arithmetic evaluation, and the expression can involve multiple operands.Step 4: Even when the expression is just A, COMPUTE still treats the operation as a numeric expression evaluation and enforces numeric rules.Step 5: Conclude that the main logical difference is that MOVE copies data, while COMPUTE evaluates an arithmetic expression and then assigns the result.
Verification / Alternative check:
You can verify by considering a case where A is an alphanumeric field and B is a numeric field. Moving A to B may not even be allowed or will follow non arithmetic move rules. Using COMPUTE B = A requires A to be compatible as a numeric value and may trigger run time conversion or errors. In more complex arithmetic expressions, such as COMPUTE B = A + 1, the difference is obvious because MOVE has no way to perform that computation.
Why Other Options Are Wrong:
Option B is incorrect because MOVE does not perform addition or any arithmetic calculation; it is primarily a data transfer. Option C is wrong because both MOVE and COMPUTE can involve numeric data, and there is no strict rule that MOVE is only for numeric fields and COMPUTE only for strings. Option D is incorrect because compilers often generate different code paths for MOVE and COMPUTE, especially when expressions and data types differ, so they are not logically identical.
Common Pitfalls:
A common pitfall is to use COMPUTE in situations where a simple MOVE would be clearer and slightly more efficient. Another mistake is to ignore the impact of data types and picture clauses on assignments, which can lead to unexpected truncation or rounding. Programmers should choose MOVE when they want a straightforward copy and COMPUTE when they need arithmetic evaluation, even if the expression seems trivial.
Final Answer:
The main logical difference is that MOVE A TO B simply copies the value of A into B, while COMPUTE B = A evaluates an arithmetic expression and assigns the numeric result to B, applying numeric conversion rules, which is option A.
Discussion & Comments