COBOL Tutorials
Learn COBOL from basics to advanced level with practical examples. This complete tutorial will help you master COBOL programming step by step.
COBOL Course Content
0% complete1. Introduction to COBOL
Start with the basics and understand where COBOL is used.
1.1 What is COBOL?
COBOL (Common Business-Oriented Language) is a high-level programming language designed for business, finance, and administrative systems. It’s still widely used in large-scale enterprise applications.
Example
01 HELLO-MESSAGE.
05 WS-MSG PIC X(20) VALUE "Hello COBOL".
PROCEDURE DIVISION.
DISPLAY WS-MSG
STOP RUN.
1.2 History of COBOL
COBOL was introduced in 1959 by the CODASYL committee to create a common, business-friendly programming language that was easy to read and maintain. Through the 1960s–1980s, COBOL evolved with multiple revisions and formal ANSI/ISO standardization, helping it run consistently across different systems.
It became famous for powering high-volume batch and transaction workloads—especially in banking, insurance, retail, and government. With its English-like syntax and focus on clarity, COBOL code is often easier to review and maintain over decades. COBOL’s importance was highlighted during Y2K, and it remains widely used today, often modernized to work with databases, APIs, and enterprise platforms while keeping proven core systems stable. On mainframes, it’s trusted for reliability, security, and performance at massive scale.
1.3 COBOL Applications
COBOL Applications are found wherever organizations process large volumes of structured business data. COBOL is widely used for critical systems that must be accurate, reliable, and available for long periods.
You’ll see where COBOL fits in the real world—such as banking and payments, insurance, government, retail, payroll, and billing—and how modern teams often integrate COBOL programs with databases, batch jobs, and APIs.
1.4 COBOL Program Structure
COBOL Program Structure is designed to keep programs organized and easy to navigate. A COBOL program is typically divided into clear sections, separating environment setup, data definitions, and processing logic.
In this topic, you’ll learn the purpose of the IDENTIFICATION, ENVIRONMENT, DATA, and PROCEDURE divisions, along with common elements like WORKING-STORAGE, paragraphs, and basic control flow—so you can read and write COBOL confidently.
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE.
ENVIRONMENT DIVISION.
...
DATA DIVISION.
...
PROCEDURE DIVISION.
...1.5 Your First COBOL Program
Your First COBOL Program is a hands-on start to writing and running COBOL code. You’ll build a simple program that shows the overall structure and how statements are written in COBOL.
We’ll cover essentials like program layout, DISPLAY output, basic variables, and a simple execution flow. By the end, you’ll have a working example you can expand into input handling, calculations, and file processing in later lessons.
2.1 Install a Compiler
In this step, you’ll install a COBOL compiler so you can turn source code into a runnable program. A working compiler is the foundation for everything you’ll do next—writing code, compiling it, and seeing real output.
You’ll also learn how to confirm the install is correct and where your compiler looks for files, so you can avoid common setup issues later.
- Choose a compiler: Pick an option suitable for learning and local practice.
- Install & verify: Check the version and compile a tiny “Hello COBOL” example.
- Understand outputs: Know where the executable is created and how errors are reported.
2.2 Configure Editor / IDE
Here you’ll set up an editor or IDE so COBOL is easier to write, read, and maintain. A good setup helps you catch mistakes faster and keeps your code neatly formatted.
You’ll create a simple workflow so you can edit, compile, and run your program without switching tools constantly.
- COBOL language support: Enable syntax highlighting and basic linting (where available).
- Project folders: Organize source files, copybooks, and outputs in a clean structure.
- Run tasks: Set up a one-click compile/run command (or a simple build task) for speed.
2.3 Compile & Run
This topic shows how to compile your COBOL source code and run it to see real output. You’ll understand the basic steps from source file → compile → executable → run.
You’ll also learn how to read compiler messages, fix typical first-time errors, and confirm your output is correct before moving to bigger programs.
- Compile step: What the compiler produces and where to find the results.
- Run step: How to execute the program and verify output.
- Troubleshooting: Quick fixes for path issues, missing files, and syntax errors.
3.1 COBOL Line Format
This topic explains the traditional COBOL line format and why it matters when writing programs—especially in mainframe-style environments where column rules can affect compilation. Understanding line structure helps you avoid confusing errors and keeps your code consistent across teams.
You’ll learn where to place statements, how indentation and continuation work, and how to write comments that make programs easy to maintain. We’ll also highlight practical differences you may see between classic fixed format and modern/free-format compilers.
- Columns & layout: How classic COBOL organizes each line and how the compiler reads it.
- Sequence area awareness: What it is and when it matters (especially in legacy codebases).
- Commenting: Writing clear comments and safely disabling lines during testing.
- Continuation: Splitting long literals and statements across lines without breaking meaning.
- Common mistakes: Misplaced characters/indentation that lead to “unexpected keyword” or syntax errors.
Outcome: You’ll be able to format COBOL code correctly so the compiler interprets it exactly as intended.
3.2 Keywords & Identifiers
Here you’ll learn the difference between COBOL keywords (reserved words used by the language) and identifiers (the names you create for data items, records, paragraphs, and files). This is essential for writing code that reads clearly and compiles without surprises.
You’ll also build strong naming habits—so your programs stay self-explanatory, your data items are easy to locate, and your team can maintain the code years later.
- Reserved words: What they are, why they can’t be used as names, and how to avoid conflicts.
- Naming rules: Valid characters, hyphen usage, and practical length/clarity guidelines.
- Meaningful names: Patterns for variables (e.g., totals, flags), records, and paragraphs.
- Consistency: Using prefixes/suffixes to keep large programs readable (without overcomplicating).
- Common pitfalls: Confusing similar names and creating identifiers that hide their purpose.
Outcome: You’ll confidently choose identifiers that are valid, readable, and maintainable.
3.3 Basic Statements
This topic introduces the basic COBOL statements you’ll use in almost every program. You’ll start building real logic by moving data, doing calculations, and printing results—then expand into simple decision-making and repetition.
We’ll focus on writing clean, predictable code that matches how COBOL is used in real business programs (totals, validations, and report-style output).
- Data movement: Using MOVE to assign values and populate records safely.
- Arithmetic: ADD, SUBTRACT, MULTIPLY, DIVIDE, and storing results correctly.
- Conditions: Getting started with IF / ELSE for validations and business rules.
- Repetition: Intro to PERFORM for repeating steps and structuring logic.
- Output: Using DISPLAY for quick checks and debugging while learning.
Outcome: You’ll be able to write small COBOL programs that calculate values, make decisions, and show correct output.
4.1 Data Division Overview
The Data Division is where a COBOL program defines all its variables, records, and data layouts. It acts as the program’s blueprint for how information is stored—so the Procedure Division can process data accurately and consistently.
In this topic, you’ll understand the main sections of the Data Division (such as working storage and file descriptions) and how COBOL’s structured data design supports real business processing like customer records, transactions, and reports.
- Why it matters: Correct data definitions prevent logic errors and formatting issues.
- Key areas: Common storage areas for temporary fields and record layouts.
- Business focus: Modeling real-world fields like dates, amounts, IDs, and names.
Outcome: You’ll be able to read and define COBOL data items confidently before writing program logic.
4.2 Level Numbers
Level numbers define the hierarchy of data items in COBOL—showing which fields belong inside a record and how groups are broken down into smaller fields. They are the key to building structured records that match real business documents.
You’ll learn how group items and elementary items work together, how nesting affects data layout, and how to choose the right levels for clean, maintainable record definitions.
- Group vs. elementary: Organize a record into logical sections and individual fields.
- Common levels: Typical levels used for records and sub-fields in practice.
- Readable layouts: Designing records that are easy to understand and update later.
Outcome: You’ll be able to design structured records using level numbers without confusion.
4.3 PIC Clause
The PIC (Picture) clause defines the type, size, and format of a COBOL data item. It’s how COBOL accurately models business fields such as amounts, dates, account numbers, and names.
In this topic, you’ll learn the most commonly used PIC patterns, how numeric and alphanumeric fields differ, and how PIC affects storage, validation, and display formatting—especially for money and totals.
- Alphanumeric vs numeric: Choosing the right data type for each business field.
- Length & layout: Defining exact field sizes to match record formats.
- Practical formatting: Working with decimals, signs, and display-friendly output.
Outcome: You’ll be able to write correct PIC clauses for common business data fields.
5.1 Sequential Files
Sequential files are one of the most common file types in COBOL, especially for batch processing. Records are read in order from the beginning to the end—perfect for reports, daily processing jobs, and large volumes of business data.
In this topic, you’ll learn how sequential files are organized, how records are defined in the Data Division, and when to use sequential processing instead of random access.
- Record-based storage: Data is processed as structured records, not as raw text lines.
- Typical use cases: Payroll, billing, statements, audit logs, and end-of-day batch runs.
- File definitions: How to connect a file name to its record layout in your program.
Outcome: You’ll understand how sequential files work and how COBOL programs process them reliably.
5.2 READ / WRITE
This topic covers the core file I/O operations in COBOL: READ and WRITE. These statements let you pull one record at a time from an input file and create output records in a new file—exactly how most batch programs work.
You’ll learn the typical program flow for reading records, processing them, and writing results—along with how to handle success and error conditions cleanly.
- READ: Retrieve the next record and check the status of the operation.
- WRITE: Create an output record with the right formatting and field values.
- Processing pattern: Read → validate/compute → write → repeat.
Outcome: You’ll be able to build a simple file-processing program that reads input records and writes output records correctly.
5.3 EOF Handling
EOF (End of File) handling is how a COBOL program knows when to stop reading records. Without proper EOF logic, programs can loop incorrectly, miss records, or throw errors during file processing.
In this topic, you’ll learn the standard ways to detect EOF and control your processing loop using status flags and clean termination logic.
- EOF condition: Recognize when the last record has been read.
- Flags & control: Use a switch/flag to stop the READ loop safely.
- Reliable endings: Close files properly and finalize totals/reports.
Outcome: You’ll be able to process a full file from start to finish without missing records or running past the end.
6.1 Arithmetic Operators
This topic covers how COBOL performs calculations using arithmetic operators and statements. You’ll learn how to work with business numbers safely—especially totals, balances, quantities, and currency values.
You’ll practice common patterns like adding totals, calculating discounts/taxes, and storing results correctly using proper numeric data definitions.
- Core operations: Add, subtract, multiply, divide, and compute derived values.
- Accuracy for money: Using fixed-point decimals to avoid rounding surprises.
- Practical examples: Totals, running sums, averages, and validations.
Outcome: You’ll be able to write reliable business calculations in COBOL.
6.2 Relational Conditions
Relational conditions let your program compare values and make decisions—such as checking whether an amount is greater than a limit or whether a code matches an expected value.
In this topic, you’ll learn the most common comparison types and how to write conditions that are clear, correct, and easy to maintain in real business rules.
- Comparisons: Equal, not equal, greater/less than, and range checks.
- Data type awareness: Comparing numeric vs alphanumeric fields correctly.
- Business rules: Validations like minimum balance, status checks, and date/ID matching.
Outcome: You’ll be able to write accurate conditions for validations and branching.
6.3 Boolean Logic
This topic explains how to combine multiple conditions using Boolean logic. It’s essential when business rules depend on more than one check—like verifying a customer is active and the balance is within limits.
You’ll learn how to group conditions correctly so your program behaves exactly as expected, and how to keep complex rules readable.
- AND / OR / NOT: Combine and negate conditions safely.
- Grouping: Control evaluation using parentheses-style grouping (where applicable) and clear structure.
- Readable rules: Break long logic into smaller checks using flags or helper conditions.
Outcome: You’ll be able to build clean multi-condition business validations.
7.1 IF / EVALUATE
This topic introduces COBOL’s main decision-making tools: IF for simple branching and EVALUATE for clean multi-way decisions (similar to switch/case).
You’ll learn when to use each one, how to keep conditions readable, and how to avoid deeply nested logic in real business programs.
- IF / ELSE: Handle validations and two-path logic clearly.
- EVALUATE: Manage multiple cases like status codes, menu options, and categories.
- Best practices: Write maintainable branching for long-term enterprise code.
Outcome: You’ll confidently implement business rules using IF and EVALUATE.
7.2 PERFORM Loops
PERFORM is one of the most important statements in COBOL for structuring programs. It lets you reuse logic, build clear paragraphs/sections, and repeat processing steps—especially in file and record processing.
In this topic, you’ll learn common PERFORM patterns used in real COBOL code, including simple loops and controlled repetition.
- PERFORM a paragraph: Reuse logic without copying code.
- PERFORM UNTIL: Loop until a condition is met (e.g., EOF reached).
- Clean structure: Separate reading, processing, and writing steps for clarity.
Outcome: You’ll be able to write structured COBOL programs using PERFORM effectively.
7.3 GO TO (When to avoid)
This topic explains the GO TO statement, why it exists in older COBOL code, and why it’s usually avoided in modern, maintainable programs. Overuse of GO TO can make logic hard to follow and debug.
You’ll learn safe alternatives and recognize situations where GO TO appears in legacy systems—so you can understand existing code without writing new “spaghetti logic.”
- Legacy usage: Common patterns you may see in older programs.
- Why to avoid: Harder testing, unclear flow, and maintenance risks.
- Better options: Prefer PERFORM, structured IF/EVALUATE, and clear paragraph flow.
Outcome: You’ll be able to read GO TO-based code and refactor toward cleaner structures.
8.1 Sections & Paragraphs
This topic explains how COBOL programs are organized into sections and paragraphs to keep logic readable and maintainable. Clear structure is especially important in enterprise programs that grow over time.
You’ll learn how paragraphs are named and executed, how sections group related logic, and how good structure makes debugging and enhancement much easier.
- Paragraph flow: Write steps in a clear sequence and reuse them safely.
- Section grouping: Organize related tasks (read, process, write) into logical blocks.
- Maintainability: Keep programs easy to navigate for long-term support.
Outcome: You’ll be able to structure the Procedure Division using clean sections and paragraphs.
8.2 PERFORM THRU
PERFORM THRU lets you execute a range of paragraphs in sequence. You’ll see it often in legacy COBOL programs, where logic is arranged as a chain of steps that run together.
In this topic, you’ll learn how PERFORM THRU works, how it affects program flow, and how to use it carefully to avoid unexpected fall-through behavior.
- Range execution: Run multiple paragraphs from a start point to an end point.
- Legacy patterns: Understand older code that relies heavily on THRU.
- Safer alternatives: Prefer targeted PERFORM calls and clearer structure when possible.
Outcome: You’ll be able to read and manage PERFORM THRU logic without breaking control flow.
8.3 Modular Programs
This topic shows how to design COBOL programs in a modular way—breaking large logic into smaller, reusable parts. Modular design improves readability, testing, and long-term maintenance.
You’ll learn how to separate responsibilities (input, validation, processing, output), reduce duplication, and prepare your code for subprograms and shared utilities.
- Separation of concerns: Keep each module focused on one job.
- Reuse: Share logic safely across different parts of the program.
- Scalability: Grow programs without turning them into hard-to-maintain blocks.
Outcome: You’ll be able to structure COBOL logic into clean, reusable modules.
9.1 Batch Programs
Batch programs process large volumes of data in scheduled runs—often overnight or end-of-day. They are a core part of COBOL’s enterprise use, powering jobs like billing, settlements, statements, and data reconciliation.
You’ll learn the typical batch flow (read → process → write), how files and reports fit into the process, and what makes batch jobs reliable and restart-friendly.
- High volume processing: Handle thousands/millions of records efficiently.
- Job flow mindset: Steps, checkpoints, totals, and control reports.
- Reliability: Clean starts/ends and predictable outputs.
Outcome: You’ll understand how COBOL batch programs are designed and why they’re critical in production systems.
9.2 Online Programs (CICS)
This topic introduces online COBOL programs that run in interactive environments like CICS. These programs respond to user requests in real time—such as account lookups, updates, and transaction processing.
You’ll understand how online programs differ from batch programs, what “transaction-driven” execution means, and how reliability and response time matter in online workloads.
- Real-time processing: Handle one request/transaction at a time.
- Screen & input flow: Typical request/response and validation patterns.
- Production concerns: Performance, security, and consistency.
Outcome: You’ll understand the basics of COBOL in online transaction systems like CICS.
9.3 Subprograms & CALL
This topic covers how COBOL programs can be split into subprograms and invoked using CALL. This is a common enterprise approach for sharing logic like validations, rate calculations, and formatting routines.
You’ll learn parameter passing basics, how data is shared safely, and how to design call interfaces that stay stable over time.
- Why subprograms: Reuse logic and keep main programs smaller.
- Passing data: Send inputs/outputs through a clear, defined interface.
- Good design: Keep CALL interfaces consistent to avoid breaking dependent programs.
Outcome: You’ll be able to understand and design basic CALL-based modular COBOL programs.
10.1 Error Handling
This topic focuses on building COBOL programs that fail safely and are easy to support in production. Good error handling helps you detect issues early, log meaningful messages, and prevent bad data from spreading.
You’ll learn how to handle file status checks, validation errors, and unexpected conditions in a controlled way—especially in batch and online environments.
- Validate inputs: Catch bad data before it affects processing.
- Check results: Use status codes for file and operation outcomes.
- Controlled exits: End programs cleanly with clear messages and totals.
Outcome: You’ll be able to add practical error handling to real COBOL programs.
10.2 Testing Strategy
This topic explains how to test COBOL programs with a clear strategy—so you can trust your results before moving to production. Testing is especially important for business systems where accuracy is critical.
You’ll learn how to create test data, cover edge cases, verify totals, and compare outputs so defects are found early and fixes are easier.
- Test cases: Normal, boundary, and negative scenarios.
- Test data: Small controlled files to validate logic step-by-step.
- Result checks: Totals, counts, and output comparisons for confidence.
Outcome: You’ll be able to plan and run effective tests for COBOL batch or online logic.
10.3 Performance Tips
This topic shares practical ways to improve performance in COBOL programs—especially those processing large files or high transaction volumes. Small design choices can make a big difference at enterprise scale.
You’ll learn common performance-sensitive areas like file I/O frequency, unnecessary computations, and data movement, along with simple habits for writing efficient code.
- Reduce I/O: Avoid extra reads/writes and keep processing streamlined.
- Efficient logic: Minimize repeated work inside loops.
- Measure & tune: Identify bottlenecks using timings and counts.
Outcome: You’ll be able to apply practical tuning techniques to make COBOL programs faster and more stable.
11.1 Embedded SQL Basics
This topic introduces Embedded SQL in COBOL—how COBOL programs interact with relational databases to read and update business data. It’s a key skill for modern mainframe development.
You’ll learn the basic idea of writing SQL inside COBOL, how variables connect to SQL statements, and how programs handle the results safely.
- SQL inside COBOL: Use SELECT/INSERT/UPDATE/DELETE in structured program flow.
- Host variables: Pass data between COBOL fields and SQL statements.
- Result handling: Check success/“no rows” outcomes and avoid surprises.
Outcome: You’ll understand the fundamentals needed to start using SQL with COBOL programs.
11.2 CURSOR Operations
Cursors are used when a query returns multiple rows and your program needs to process them one at a time. This is a common pattern in batch jobs and reporting programs.
In this topic, you’ll learn the cursor lifecycle—declare, open, fetch, and close—and how to loop through results safely while handling end-of-data conditions properly.
- Cursor flow: DECLARE → OPEN → FETCH (loop) → CLOSE.
- End-of-data: Detect when there are no more rows to process.
- Performance mindset: Fetch rows efficiently and avoid unnecessary calls.
Outcome: You’ll be able to understand and implement basic cursor-based row processing.
11.3 Commit / Rollback
This topic explains how database changes are controlled using COMMIT and ROLLBACK. Transaction control ensures data stays consistent—especially when programs process many updates or handle failures mid-run.
You’ll learn when to commit work, when to roll back, and how to design safe checkpoint-style processing so large jobs can recover cleanly.
- Commit points: Save work in controlled steps to reduce risk.
- Rollback safety: Undo changes when errors occur or validations fail.
- Consistency: Keep data accurate across multiple related updates.
Outcome: You’ll understand how to control database transactions from COBOL safely and predictably.
12.1 File Read Program
In this hands-on exercise, you’ll build a simple file read program that processes records one by one. This is a core pattern in COBOL—used in batch jobs for statements, billing, reconciliation, and data validation.
You’ll define the file and record layout, read records in a loop, display key fields for verification, and apply basic checks so the program behaves correctly from the first record to EOF.
- Setup: File definition + record structure in the Data Division.
- Processing loop: READ → validate → track counts/totals → repeat.
- EOF handling: Stop cleanly, close the file, and print final counts.
Outcome: You’ll be able to create a working sequential file reader with correct loop and EOF logic.
12.2 Report Generation
This topic shows how COBOL programs generate business reports from file or database data. You’ll learn how to format readable output, produce totals, and create report-style layouts that match real enterprise needs.
You’ll build a basic report flow—read records, group or summarize data, print headings/details, and finish with totals—so your output is consistent and audit-friendly.
- Report layout: Headings, detail lines, and trailer totals.
- Formatting: Alignment, numeric formatting, and clean display output.
- Control totals: Counts and sums to verify accuracy.
Outcome: You’ll be able to produce a simple, well-formatted COBOL report with totals and validations.
12.3 DB2 Fetch Program
In this practical program, you’ll fetch data from DB2 using Embedded SQL and process the results in COBOL. This is a common enterprise pattern for reading customer, account, or transaction data safely and efficiently.
You’ll work with host variables, handle “no rows found” situations, and understand the basic flow of preparing, executing, and reading query results—either as a single row or through a cursor-based loop.
- Host variables: Map DB2 columns to COBOL fields.
- Result handling: Success vs no rows vs error conditions.
- Row processing: Single-row fetch or cursor loop (as applicable).
Outcome: You’ll be able to build a basic COBOL-DB2 fetch routine with safe result checks.
13.1 Freshers Level
Use these beginner-friendly questions to build confidence on COBOL fundamentals—program structure, data definitions, and basic file processing.
13.2 2–5 Years
These questions focus on real project patterns—file status handling, modular design, testing, and production-minded coding.
13.3 Advanced / Scenario
Scenario questions test how you think in production—restart/recovery, DB2 strategy, performance tuning, and safe modernization of legacy COBOL.