MainframeMaster

COBOL Tutorial

COBOL SIGNED-SHORT Clause - Quick Reference

Progress0 of 0 lessons

Overview

The SIGNED-SHORT clause is used to define small signed integer data types in COBOL. It provides memory-efficient storage for applications requiring limited positive and negative values, making it ideal for embedded systems, small counters, and memory-constrained environments.

Purpose and Usage

  • Memory efficiency - Optimize memory usage for small values
  • Limited range - Handle small signed integer values
  • Embedded systems - Support memory-constrained environments
  • Small counters - Efficient storage for counters and flags
  • Status codes - Handle small status and error numbers

Syntax

The SIGNED-SHORT clause is used in the DATA DIVISION for small 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
28
29
30
31
* Basic SIGNED-SHORT syntax 01 data-name PIC S9(n) SIGNED-SHORT. * Complete example with small signed integer types IDENTIFICATION DIVISION. PROGRAM-ID. SIGNED-SHORT-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. * Small signed integer for temperature readings 01 TEMPERATURE-VALUE PIC S9(3) SIGNED-SHORT. * Small signed integer for status codes 01 STATUS-CODE PIC S9(3) SIGNED-SHORT. * Small signed integer for small counters 01 SMALL-COUNTER PIC S9(4) SIGNED-SHORT. * Small signed integer for error numbers 01 ERROR-NUMBER PIC S9(3) SIGNED-SHORT. PROCEDURE DIVISION. MAIN-LOGIC. * Assign small values within SIGNED-SHORT range MOVE 25 TO TEMPERATURE-VALUE MOVE -5 TO STATUS-CODE MOVE 1000 TO SMALL-COUNTER MOVE -127 TO ERROR-NUMBER DISPLAY "Temperature: " TEMPERATURE-VALUE "°C" DISPLAY "Status code: " STATUS-CODE DISPLAY "Small counter: " SMALL-COUNTER DISPLAY "Error number: " ERROR-NUMBER STOP RUN.

SIGNED-SHORT provides memory-efficient storage for small signed integer values with limited range.

Practical Examples

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

Embedded System - Temperature Monitoring

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
45
46
* Embedded system using SIGNED-SHORT for temperature monitoring DATA DIVISION. WORKING-STORAGE SECTION. * Temperature readings (range: -128 to +127 degrees Celsius) 01 CURRENT-TEMPERATURE PIC S9(3) SIGNED-SHORT. * Temperature change (range: -128 to +127 degrees) 01 TEMP-CHANGE PIC S9(3) SIGNED-SHORT. * Status indicator (range: -128 to +127) 01 SYSTEM-STATUS PIC S9(3) SIGNED-SHORT. * Error code (range: -128 to +127) 01 ERROR-CODE PIC S9(3) SIGNED-SHORT. * Retry counter (range: -128 to +127) 01 RETRY-COUNTER PIC S9(3) SIGNED-SHORT. PROCEDURE DIVISION. MONITOR-TEMPERATURE. * Initialize temperature reading MOVE 22 TO CURRENT-TEMPERATURE DISPLAY "Current temperature: " CURRENT-TEMPERATURE "°C" * Record temperature change (negative for cooling) MOVE -3 TO TEMP-CHANGE ADD TEMP-CHANGE TO CURRENT-TEMPERATURE DISPLAY "Temperature after change: " CURRENT-TEMPERATURE "°C" * Set system status (positive for normal, negative for issues) MOVE 1 TO SYSTEM-STATUS DISPLAY "System status: " SYSTEM-STATUS * Handle error condition (negative error codes) MOVE -5 TO ERROR-CODE DISPLAY "Error code: " ERROR-CODE * Initialize retry counter MOVE 3 TO RETRY-COUNTER DISPLAY "Retry counter: " RETRY-COUNTER * Decrement retry counter SUBTRACT 1 FROM RETRY-COUNTER DISPLAY "Remaining retries: " RETRY-COUNTER * Check for extreme temperatures IF CURRENT-TEMPERATURE > 100 OR CURRENT-TEMPERATURE < -50 MOVE -1 TO SYSTEM-STATUS DISPLAY "WARNING: Extreme temperature detected!" END-IF.

This example demonstrates how SIGNED-SHORT optimizes memory usage in embedded systems for temperature monitoring. Temperature readings, changes, status codes, error codes, and retry counters all use small signed integers within the range of -128 to +127. This provides memory efficiency while handling both positive and negative values for various system states.

Game Development - Score and Status Tracking

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
45
46
47
48
49
50
51
52
53
54
55
56
57
* Game development using SIGNED-SHORT for score and status tracking DATA DIVISION. WORKING-STORAGE SECTION. * Player score (range: -128 to +127 points) 01 PLAYER-SCORE PIC S9(3) SIGNED-SHORT. * Health points (range: -128 to +127) 01 HEALTH-POINTS PIC S9(3) SIGNED-SHORT. * Level number (range: -128 to +127) 01 LEVEL-NUMBER PIC S9(3) SIGNED-SHORT. * Lives remaining (range: -128 to +127) 01 LIVES-REMAINING PIC S9(3) SIGNED-SHORT. * Power-up status (range: -128 to +127) 01 POWER-UP-STATUS PIC S9(3) SIGNED-SHORT. PROCEDURE DIVISION. TRACK-GAME-STATUS. * Initialize player score MOVE 0 TO PLAYER-SCORE DISPLAY "Initial score: " PLAYER-SCORE * Add points for successful action MOVE 10 TO POWER-UP-STATUS ADD POWER-UP-STATUS TO PLAYER-SCORE DISPLAY "Score after bonus: " PLAYER-SCORE * Set health points (can be negative for damage) MOVE 100 TO HEALTH-POINTS DISPLAY "Health points: " HEALTH-POINTS * Apply damage (negative value) MOVE -20 TO POWER-UP-STATUS ADD POWER-UP-STATUS TO HEALTH-POINTS DISPLAY "Health after damage: " HEALTH-POINTS * Set level number MOVE 5 TO LEVEL-NUMBER DISPLAY "Current level: " LEVEL-NUMBER * Set lives remaining MOVE 3 TO LIVES-REMAINING DISPLAY "Lives remaining: " LIVES-REMAINING * Lose a life SUBTRACT 1 FROM LIVES-REMAINING DISPLAY "Lives after loss: " LIVES-REMAINING * Check for game over condition IF LIVES-REMAINING <= 0 MOVE -1 TO POWER-UP-STATUS DISPLAY "GAME OVER!" END-IF * Check for health warning IF HEALTH-POINTS < 20 MOVE -2 TO POWER-UP-STATUS DISPLAY "WARNING: Low health!" END-IF.

This example shows how SIGNED-SHORT efficiently handles game state variables in a memory-constrained environment. Player scores, health points, level numbers, lives remaining, and power-up status all use small signed integers. Negative values represent damage, penalties, or special conditions, while positive values represent bonuses, health, or normal states.

Industrial Control - Sensor Status Monitoring

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
45
46
47
48
49
50
51
52
53
54
55
56
57
* Industrial control system using SIGNED-SHORT for sensor monitoring DATA DIVISION. WORKING-STORAGE SECTION. * Pressure reading (range: -128 to +127 PSI) 01 PRESSURE-READING PIC S9(3) SIGNED-SHORT. * Flow rate (range: -128 to +127 GPM) 01 FLOW-RATE PIC S9(3) SIGNED-SHORT. * Valve position (range: -128 to +127 degrees) 01 VALVE-POSITION PIC S9(3) SIGNED-SHORT. * Alarm status (range: -128 to +127) 01 ALARM-STATUS PIC S9(3) SIGNED-SHORT. * Maintenance counter (range: -128 to +127) 01 MAINTENANCE-COUNTER PIC S9(3) SIGNED-SHORT. PROCEDURE DIVISION. MONITOR-INDUSTRIAL-SYSTEM. * Read pressure sensor MOVE 75 TO PRESSURE-READING DISPLAY "Pressure: " PRESSURE-READING " PSI" * Read flow rate (negative for reverse flow) MOVE 25 TO FLOW-RATE DISPLAY "Flow rate: " FLOW-RATE " GPM" * Set valve position (negative for closed, positive for open) MOVE 90 TO VALVE-POSITION DISPLAY "Valve position: " VALVE-POSITION " degrees" * Initialize alarm status (0 = normal, negative = warnings, positive = errors) MOVE 0 TO ALARM-STATUS DISPLAY "Alarm status: " ALARM-STATUS * Set maintenance counter MOVE 30 TO MAINTENANCE-COUNTER DISPLAY "Days until maintenance: " MAINTENANCE-COUNTER * Decrement maintenance counter SUBTRACT 1 FROM MAINTENANCE-COUNTER DISPLAY "Days until maintenance: " MAINTENANCE-COUNTER * Check for high pressure alarm IF PRESSURE-READING > 100 MOVE -1 TO ALARM-STATUS DISPLAY "WARNING: High pressure detected!" END-IF * Check for reverse flow IF FLOW-RATE < 0 MOVE -2 TO ALARM-STATUS DISPLAY "WARNING: Reverse flow detected!" END-IF * Check for maintenance due IF MAINTENANCE-COUNTER <= 0 MOVE 1 TO ALARM-STATUS DISPLAY "ALERT: Maintenance required!" END-IF.

This example demonstrates SIGNED-SHORT usage in industrial control systems for monitoring various sensors and equipment status. Pressure readings, flow rates, valve positions, alarm status, and maintenance counters all use small signed integers. Negative values represent warnings, reverse flow, or closed positions, while positive values represent normal operation, forward flow, or open positions.

Best Practices

Understanding best practices ensures effective use of the SIGNED-SHORT clause.

Best Practices

  • Use for small values - Use SIGNED-SHORT only for values within its range
  • Consider memory constraints - Choose SIGNED-SHORT for memory-constrained environments
  • Validate value ranges - Check that values fit within SIGNED-SHORT limits
  • Monitor overflow - Watch for potential overflow conditions
  • Document limitations - Document the limited range of SIGNED-SHORT

Test Your Knowledge

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

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

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

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

3. What is the difference between SIGNED-SHORT and SIGNED-INT?

  • They are the same thing
  • SIGNED-SHORT handles smaller values than SIGNED-INT
  • SIGNED-SHORT is faster than SIGNED-INT
  • SIGNED-SHORT is obsolete, SIGNED-INT is modern

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

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

5. What is the relationship between SIGNED-SHORT and memory usage?

  • They are unrelated
  • SIGNED-SHORT uses less memory than larger integer types
  • SIGNED-SHORT uses more memory than larger integer types
  • SIGNED-SHORT has no impact on memory

Frequently Asked Questions