Part II: Control Flow and Program Logic
Chapters 7--10
A program that can only execute statements in a straight line from top to bottom is barely more useful than a calculator. The power of any programming language lies in its ability to make decisions, repeat operations, manipulate text, and organize data into structured collections. Part II introduces these capabilities in COBOL, transforming you from a programmer who can define data and perform calculations into one who can express complex business logic with clarity and precision.
The features covered in this part represent one of the most significant evolutions in COBOL's history. Before the COBOL-85 standard, COBOL programmers relied heavily on GO TO statements and paragraph-level PERFORM to control program flow, resulting in code that was notoriously difficult to follow -- the infamous "spaghetti code" that gave COBOL a bad reputation in academic circles. COBOL-85 changed everything. It introduced structured programming constructs -- END-IF, END-PERFORM, inline PERFORM, and the EVALUATE statement -- that brought COBOL in line with the structured programming principles advocated by Dijkstra, Wirth, and others. These constructs did not merely add syntactic sugar; they fundamentally changed how COBOL programs were designed, written, and maintained.
Today, every professional COBOL shop writes structured code using COBOL-85 constructs. The programs you write in Part II will follow these modern standards from the beginning, ensuring that your code is clean, maintainable, and consistent with the practices used in production environments worldwide. You will still encounter examples of older, unstructured patterns -- because you will inevitably encounter them in legacy codebases -- but you will learn them as historical artifacts to be recognized and refactored, not as techniques to be emulated.
What You Will Learn
Part II covers four interconnected topics that together form the core of COBOL program logic. Conditional logic lets your programs make decisions. Iteration lets them repeat operations efficiently. String handling lets them manipulate the textual data that dominates business processing. And tables (COBOL's term for arrays) let them organize related data into indexed collections that can be searched, sorted, and processed as units.
These four topics interact constantly in real-world COBOL programs. A payroll program might iterate through a table of employees, apply conditional logic to determine each employee's tax bracket, use string handling to format their name for a check, and repeat the entire process for every pay period. By the end of Part II, you will be able to write programs of this complexity.
Chapter Summaries
Chapter 7: Conditional Logic -- IF, EVALUATE, and Decision Structures
Every business rule is, at its core, a decision. If the account balance is below the minimum, charge a maintenance fee. If the customer's credit score exceeds the threshold, approve the loan. If the transaction amount is above the reporting limit, flag it for compliance review. Chapter 7 teaches you to express these decisions in COBOL using the IF/ELSE/END-IF construct, nested conditionals, compound conditions with AND and OR, the NOT operator, and the powerful EVALUATE statement that serves as COBOL's equivalent of the switch/case construct found in other languages. You will learn about condition names (level 88 items) and how they transform cryptic numeric tests into readable business logic -- testing IF ACCOUNT-IS-OVERDRAWN instead of IF WS-BALANCE < 0. The chapter covers the COBOL-85 scope terminator END-IF in depth, explaining how it eliminated the ambiguity of the period-delimited conditional statements used in earlier standards. Special attention is given to the EVALUATE statement, which can test multiple conditions simultaneously and is one of COBOL-85's most elegant additions to the language.
Chapter 8: Iteration and the PERFORM Statement
Batch COBOL programs process data in bulk -- millions of records in a single run. Iteration is the mechanism that makes this possible. Chapter 8 provides comprehensive coverage of the PERFORM statement in all its forms: out-of-line PERFORM (calling a separate paragraph), inline PERFORM (with the loop body between PERFORM and END-PERFORM), PERFORM TIMES (fixed iteration count), PERFORM UNTIL (condition-based loops), and PERFORM VARYING (counter-based loops equivalent to for-loops in other languages). You will learn the critical distinction between PERFORM WITH TEST BEFORE and PERFORM WITH TEST AFTER, which determines whether the loop condition is checked at the beginning or end of each iteration. The chapter covers nested PERFORM loops for processing multi-dimensional data, the PERFORM THRU construct for executing a range of paragraphs, and the EXIT PERFORM statement for breaking out of a loop early. Structured programming patterns are emphasized throughout, with explicit guidance on avoiding GO TO and writing loops that are easy to read, test, and maintain.
Chapter 9: String Handling -- INSPECT, STRING, UNSTRING, and Reference Modification
Business data is overwhelmingly textual: names, addresses, descriptions, codes, messages, and identifiers. Chapter 9 covers COBOL's string manipulation capabilities, which are more powerful than many programmers expect. The INSPECT statement counts and replaces characters within a field, with TALLYING, REPLACING, and CONVERTING options that can perform sophisticated text transformations in a single statement. The STRING statement concatenates multiple fields into one, with DELIMITED BY clauses that control how each source field contributes to the result. The UNSTRING statement parses a delimited string into individual fields -- essential for processing CSV data, parsing addresses, or splitting composite keys. Reference modification, introduced in COBOL-85, provides substring access using a position-and-length notation: WS-NAME(1:5) extracts the first five characters. The chapter also covers the COBOL 2002 intrinsic functions for string manipulation, including UPPER-CASE, LOWER-CASE, REVERSE, TRIM, and LENGTH, which simplify operations that were cumbersome in earlier standards.
Chapter 10: Tables and Arrays -- OCCURS, SEARCH, and Indexed Data
Business applications routinely work with collections of related data: a list of monthly sales figures, a table of tax brackets, a roster of employees, an array of transaction codes. COBOL handles these collections through tables, defined using the OCCURS clause. Chapter 10 covers table definition with fixed and variable-length OCCURS, multi-dimensional tables using nested OCCURS, table initialization with VALUE clauses and PERFORM loops, and the critical topic of subscripting versus indexing. You will learn the SEARCH statement for sequential table lookup and SEARCH ALL for binary search on sorted tables, including the WHEN clause syntax that specifies the search condition. The chapter covers the OCCURS DEPENDING ON clause for variable-length tables, the ASCENDING/DESCENDING KEY clause for ordered tables, and practical patterns for loading tables from files, searching for specific entries, and updating table contents. Performance considerations are addressed, including the efficiency advantages of indexed access (SET and SEARCH) over subscripted access in large tables.
Learning Objectives
Upon completing Part II, you will be able to:
- Implement conditional business logic using IF/ELSE/END-IF, nested conditions, compound conditions with AND/OR/NOT, and the EVALUATE statement for multi-branch decisions
- Use condition names (level 88 items) to create self-documenting boolean tests that make business rules readable and maintainable
- Write efficient iteration structures using all forms of PERFORM -- out-of-line, inline, TIMES, UNTIL, and VARYING -- with correct use of TEST BEFORE and TEST AFTER
- Manipulate string data using INSPECT (TALLYING, REPLACING, CONVERTING), STRING, UNSTRING, and reference modification for parsing, formatting, and transforming textual business data
- Define and process tables using OCCURS, subscripts, and indexes, including multi-dimensional tables, variable-length tables, and tables with sort keys
- Search tables efficiently using SEARCH for sequential lookup and SEARCH ALL for binary search, with appropriate WHEN conditions
- Write structured COBOL programs that avoid GO TO, use explicit scope terminators (END-IF, END-PERFORM, END-EVALUATE), and follow the structured programming principles introduced in COBOL-85
- Read and refactor legacy unstructured code by recognizing pre-COBOL-85 patterns (paragraph-level PERFORM, GO TO logic, period-delimited conditionals) and converting them to structured equivalents
Historical Context: COBOL-85 and the Structured Programming Revolution
The features in Part II are dominated by the COBOL-85 standard, which represents the single most important revision in the language's history. Understanding why COBOL-85 matters requires a brief look at what came before.
Before COBOL-85, program flow control was limited. The IF statement existed, but its scope was delimited by a period -- the same period that ends every COBOL sentence. This meant that nesting IF statements was treacherous: a misplaced period could silently terminate an outer IF, causing logic errors that were extraordinarily difficult to find. The PERFORM statement could call paragraphs, but loop bodies could not be written inline -- every loop required a separate paragraph, scattering related logic across the program.
The result was a programming culture that relied heavily on GO TO statements to route control flow. Programs became tangled webs of forward and backward jumps, earning COBOL the "spaghetti code" label that persists in popular imagination to this day. Academic computer scientists pointed to COBOL as an example of everything wrong with unstructured programming.
COBOL-85 addressed these problems directly:
- END-IF, END-PERFORM, END-EVALUATE, and other scope terminators replaced the period as the mechanism for delimiting conditional and iterative blocks. Nested structures became safe and clear.
- Inline PERFORM allowed loop bodies to be written at the point of use, keeping related logic together.
- EVALUATE provided a clean multi-branch conditional that replaced chains of nested IF statements and GO TO-based dispatch tables.
- CONTINUE provided a no-operation statement for empty branches, replacing awkward workarounds.
These additions did not merely improve COBOL's syntax. They transformed the culture of COBOL programming. New coding standards were adopted industry-wide, mandating structured code and restricting or prohibiting GO TO. Training programs were revised. Existing code was gradually refactored. Within a decade, structured COBOL-85 became the universal standard for new development.
The COBOL-74 standard also contributed to Part II's topics, particularly in string handling. The INSPECT statement was introduced in COBOL-74, providing the first standardized mechanism for character-level string operations. COBOL-85 extended these capabilities with reference modification, and COBOL 2002 added intrinsic functions like UPPER-CASE and LOWER-CASE that further simplified string processing.
Format Note
Part II continues the dual-format approach established in Part I. Most examples appear in fixed format, which remains the standard in production mainframe environments. Free-format alternatives are shown where they illuminate a concept or reduce visual clutter, and exercises often ask you to write solutions in both formats. By the end of Part II, the format distinction should feel natural rather than confusing -- you will read fixed-format code as fluently as free-format, and you will write in whichever format your environment requires.
Prerequisites
Part II assumes you have completed Part I (Chapters 1--6) and are comfortable with the following:
- The four-division COBOL program structure
- Data definition using PICTURE clauses, level numbers, and the WORKING-STORAGE SECTION
- Basic file I/O using OPEN, READ, WRITE, CLOSE, and FILE STATUS
- Arithmetic operations using ADD, SUBTRACT, MULTIPLY, DIVIDE, and COMPUTE
- Compiling and running COBOL programs using GnuCOBOL
If you can write a program that reads a sequential file, performs calculations on each record, and writes results to an output file, you are ready for Part II. If that description does not match your current skill level, return to Part I and complete the exercises before proceeding.
How the Chapters Build on Each Other
The four chapters of Part II form a natural progression:
- Chapter 7 (conditional logic) teaches your programs to make decisions -- the most fundamental control flow mechanism
- Chapter 8 (iteration) teaches your programs to repeat operations, combining with conditional logic to process data in bulk
- Chapter 9 (string handling) teaches your programs to manipulate the textual data that business applications are built on, using the control flow structures from Chapters 7 and 8
- Chapter 10 (tables) teaches your programs to work with collections of data, using iteration to traverse tables and conditional logic to search them
Each chapter assumes the concepts from the previous chapter. String handling exercises use conditional logic and loops. Table processing exercises use all three preceding topics. By Chapter 10, you are writing programs that load data into tables from files, search and sort those tables, apply conditional business rules to each entry, and produce formatted reports -- a pattern that appears in countless production COBOL systems.
Estimated Study Time
Plan for approximately 35 to 45 hours to work through Part II. The breakdown:
- Chapter 7 (conditional logic): 7--9 hours, including exercises
- Chapter 8 (iteration): 8--10 hours, including exercises
- Chapter 9 (string handling): 8--10 hours, including exercises
- Chapter 10 (tables and arrays): 10--14 hours, including exercises
Chapter 10 requires the most time because table processing involves the interaction of multiple concepts -- data definition, iteration, searching, and conditional logic -- and the exercises are correspondingly more complex. Do not rush this material. Table processing is one of the most frequently tested skills in COBOL job interviews, and deep fluency with OCCURS, SEARCH, and indexed access will serve you throughout your career.
What Mastery of Part II Enables
Completing Part II marks a significant milestone in your development as a COBOL programmer. You will have mastered the control flow constructs that appear in every non-trivial COBOL program, and you will be able to write programs that express complex business logic clearly and correctly.
More concretely, with Parts I and II complete, you have the skills to write programs that would be recognizable to a production COBOL team: programs that read files, apply business rules, manipulate data, manage collections, and produce output. You are not yet working with indexed files, databases, or online transactions -- those come in Parts III and V -- but the logical foundation is in place.
Part III (File Processing and Data Management) builds directly on your Part II skills. Sequential file processing patterns use the iteration and conditional logic from Chapters 7 and 8. Indexed file access uses the table and search concepts from Chapter 10. Sort and merge operations use the data manipulation skills from Chapters 9 and 10. Report Writer uses the string formatting techniques from Chapter 9. Without solid Part II skills, Part III will be an uphill struggle; with them, it will be a natural extension of what you already know.
Beyond this textbook, the skills from Part II are immediately applicable to reading and understanding production COBOL code. If you are joining a team that maintains existing COBOL systems, you will be able to read the PROCEDURE DIVISION of most batch programs and follow the logic, even if the data structures and file handling are more complex than what you have written yourself. That ability to read and comprehend existing code is the first skill that makes a new COBOL developer useful to a production team, and Part II is where you acquire it.
"Programming is the art of telling another human being what one wants the computer to do." -- Donald Knuth
Turn to Chapter 7 and teach your programs to think.