MainframeMaster

COBOL Tutorial

COBOL SIGNED Clause - Quick Reference

Progress0 of 0 lessons

Overview

The SIGNED clause is used to define signed integer data types in COBOL. It specifies that a numeric data item can hold both positive and negative values, enabling the representation of the full range of signed numbers for mathematical calculations and data processing.

Purpose and Usage

  • Signed integer support - Enable negative value representation
  • Mathematical operations - Support full range of arithmetic
  • Data range expansion - Extend data range to include negatives
  • Financial calculations - Handle negative amounts and balances
  • Scientific applications - Support temperature, elevation, etc.

Syntax

The SIGNED clause is used in the DATA DIVISION for signed integer data definition.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
* Basic SIGNED syntax 01 data-name PIC S9(n) [SIGNED]. * Complete example with different signed integer types IDENTIFICATION DIVISION. PROGRAM-ID. SIGNED-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. * Small signed integer (5 digits, can be negative) 01 SMALL-SIGNED-INT PIC S9(5) SIGNED. * Medium signed integer (9 digits, can be negative) 01 MEDIUM-SIGNED-INT PIC S9(9) SIGNED. * Large signed integer (18 digits, can be negative) 01 LARGE-SIGNED-INT PIC S9(18) SIGNED. PROCEDURE DIVISION. MAIN-LOGIC. * Assign positive and negative values MOVE 12345 TO SMALL-SIGNED-INT MOVE -67890 TO MEDIUM-SIGNED-INT MOVE 999999999 TO LARGE-SIGNED-INT DISPLAY "Small signed: " SMALL-SIGNED-INT DISPLAY "Medium signed: " MEDIUM-SIGNED-INT DISPLAY "Large signed: " LARGE-SIGNED-INT STOP RUN.

SIGNED enables negative value representation in integer data types.

Practical Examples

Examples of using the SIGNED clause in different scenarios with detailed explanations.

Financial Application - Account Balances

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
* Financial application using signed integers for account balances DATA DIVISION. WORKING-STORAGE SECTION. * Account balance can be positive (credit) or negative (debit) 01 ACCOUNT-BALANCE PIC S9(9)V99 SIGNED. * Transaction amount can be positive (deposit) or negative (withdrawal) 01 TRANSACTION-AMOUNT PIC S9(7)V99 SIGNED. * Running total that can go negative 01 RUNNING-TOTAL PIC S9(10)V99 SIGNED. PROCEDURE DIVISION. PROCESS-FINANCIAL-TRANSACTIONS. * Initialize account with positive balance MOVE 1000.00 TO ACCOUNT-BALANCE DISPLAY "Initial balance: " ACCOUNT-BALANCE * Process a withdrawal (negative transaction) MOVE -250.50 TO TRANSACTION-AMOUNT ADD TRANSACTION-AMOUNT TO ACCOUNT-BALANCE DISPLAY "After withdrawal: " ACCOUNT-BALANCE * Process a deposit (positive transaction) MOVE 500.00 TO TRANSACTION-AMOUNT ADD TRANSACTION-AMOUNT TO ACCOUNT-BALANCE DISPLAY "After deposit: " ACCOUNT-BALANCE * Allow account to go negative (overdraft) MOVE -2000.00 TO TRANSACTION-AMOUNT ADD TRANSACTION-AMOUNT TO ACCOUNT-BALANCE DISPLAY "After large withdrawal: " ACCOUNT-BALANCE.

This example demonstrates how SIGNED integers handle financial data where accounts can have both positive (credit) and negative (debit) balances. The account balance can go negative to represent overdrafts or outstanding debts.

Temperature Monitoring System

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
* Temperature monitoring system using signed integers DATA DIVISION. WORKING-STORAGE SECTION. * Temperature readings can be positive or negative (Celsius) 01 CURRENT-TEMPERATURE PIC S9(3) SIGNED. * Temperature change can be positive (warming) or negative (cooling) 01 TEMP-CHANGE PIC S9(3) SIGNED. * Average temperature that can be negative 01 AVERAGE-TEMP PIC S9(3)V9 SIGNED. PROCEDURE DIVISION. MONITOR-TEMPERATURE. * Record current temperature (can be negative) MOVE -5 TO CURRENT-TEMPERATURE DISPLAY "Current temperature: " CURRENT-TEMPERATURE "°C" * Record temperature change (negative means cooling) MOVE -2 TO TEMP-CHANGE ADD TEMP-CHANGE TO CURRENT-TEMPERATURE DISPLAY "Temperature after change: " CURRENT-TEMPERATURE "°C" * Calculate average temperature (can be negative) MOVE -1.5 TO AVERAGE-TEMP DISPLAY "Average temperature: " AVERAGE-TEMP "°C" * Check for extreme cold conditions IF CURRENT-TEMPERATURE < -20 DISPLAY "WARNING: Extreme cold conditions detected!" END-IF.

This example shows how SIGNED integers are essential for temperature monitoring where readings can be below zero. The system can track temperature changes, calculate averages, and detect extreme conditions using negative values.

Mathematical Calculations

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
* Mathematical calculations using signed integers DATA DIVISION. WORKING-STORAGE SECTION. * Variables for mathematical operations 01 OPERAND-A PIC S9(5) SIGNED. 01 OPERAND-B PIC S9(5) SIGNED. 01 RESULT PIC S9(6) SIGNED. * Counter that can go negative 01 LOOP-COUNTER PIC S9(3) SIGNED. PROCEDURE DIVISION. PERFORM-MATH-OPERATIONS. * Initialize operands with positive and negative values MOVE 100 TO OPERAND-A MOVE -50 TO OPERAND-B * Addition with negative numbers ADD OPERAND-B TO OPERAND-A GIVING RESULT DISPLAY "100 + (-50) = " RESULT * Subtraction resulting in negative MOVE 25 TO OPERAND-A MOVE 100 TO OPERAND-B SUBTRACT OPERAND-B FROM OPERAND-A GIVING RESULT DISPLAY "25 - 100 = " RESULT * Multiplication with negative numbers MOVE -6 TO OPERAND-A MOVE 7 TO OPERAND-B MULTIPLY OPERAND-A BY OPERAND-B GIVING RESULT DISPLAY "(-6) × 7 = " RESULT * Division with negative result MOVE -100 TO OPERAND-A MOVE 4 TO OPERAND-B DIVIDE OPERAND-A BY OPERAND-B GIVING RESULT DISPLAY "(-100) ÷ 4 = " RESULT * Loop counter going negative MOVE 5 TO LOOP-COUNTER PERFORM UNTIL LOOP-COUNTER < -5 DISPLAY "Counter: " LOOP-COUNTER SUBTRACT 1 FROM LOOP-COUNTER END-PERFORM.

This example demonstrates comprehensive mathematical operations using SIGNED integers. It shows addition, subtraction, multiplication, and division with negative numbers, plus a loop counter that can go negative to control program flow.

Best Practices

Understanding best practices ensures effective use of the SIGNED clause.

Best Practices

  • Choose appropriate size - Select field size based on expected value range
  • Consider overflow - Ensure field size can handle maximum expected values
  • Validate input - Check that input values are within expected range
  • Handle negative logic - Include logic to handle negative values appropriately
  • Document assumptions - Document when negative values are expected or allowed

Test Your Knowledge

1. What is the primary purpose of the SIGNED clause in COBOL?

  • To sign documents
  • To define signed integer data types
  • To sign programs
  • To sign files

2. In which COBOL division is the SIGNED clause typically used?

  • IDENTIFICATION DIVISION
  • ENVIRONMENT DIVISION
  • DATA DIVISION
  • PROCEDURE DIVISION

3. What is the difference between SIGNED and UNSIGNED data?

  • They are the same thing
  • SIGNED can hold negative values, UNSIGNED cannot
  • SIGNED is faster than UNSIGNED
  • SIGNED is obsolete, UNSIGNED is modern

4. Can SIGNED be used with all numeric data types?

  • Yes, with all numeric types
  • No, only with integer types
  • No, only with decimal types
  • Only with specific data types

5. What is the relationship between SIGNED and SIGN clauses?

  • They are the same thing
  • SIGNED defines the data type, SIGN controls sign position
  • SIGNED is obsolete, SIGN is modern
  • They cannot be used together

Frequently Asked Questions