Day 5: Using Conditional Statements and Decision-Making Logic in COBOL Programs for Real Business Applications

 

1. Introduction: Adding Decision-Making to COBOL Programs

So far, our COBOL programs have performed sequential tasks — displaying information, accepting input, or performing arithmetic. But real-world programs often need decision-making to respond differently based on certain conditions. COBOL provides the IF and EVALUATE statements to implement such logic.

Conditional statements allow programs to branch, choose paths, and handle multiple scenarios automatically. For example, a payroll system might apply different tax rates depending on salary, or an inventory program might reorder items when stock is low. Using these statements makes programs smarter, flexible, and more aligned with business rules.


2. The IF Statement in COBOL

The IF statement evaluates a condition and executes a block of code if the condition is true. Optionally, you can use ELSE for alternative actions.

Example:

IF AGE >= 18 DISPLAY "ELIGIBLE TO VOTE" ELSE DISPLAY "NOT ELIGIBLE TO VOTE".
  • COBOL allows nested IF statements, enabling complex decision logic.

  • Conditions can compare numeric, alphabetic, or alphanumeric variables using operators like =, >, <, >=, <=, and NOT =.


3. The EVALUATE Statement for Multi-Branch Logic

The EVALUATE statement is COBOL’s version of a switch-case. It allows evaluating a variable or condition and branching into multiple outcomes neatly:

Example:

EVALUATE GRADE WHEN "A" DISPLAY "EXCELLENT" WHEN "B" DISPLAY "GOOD" WHEN "C" DISPLAY "FAIR" WHEN OTHER DISPLAY "NEEDS IMPROVEMENT" END-EVALUATE.
  • EVALUATE is more readable than multiple nested IFs.

  • Ideal for grading, status checks, or handling multiple business categories.


🧠 Key Takeaways

  • Conditional statements make COBOL programs dynamic and responsive.

  • IF/ELSE handles simple true/false decisions.

  • EVALUATE simplifies multi-branch decision-making for readability and maintainability.


💻 Practice Task

  1. Create a COBOL program called GradeEvaluator.cbl.

  2. Accept a student’s marks as input.

  3. Use IF to check pass/fail status (pass if marks ≥ 35).

  4. Use EVALUATE to display the grade: A, B, C, or F.

Example structure:

IDENTIFICATION DIVISION. PROGRAM-ID. GRADE-EVALUATOR. DATA DIVISION. WORKING-STORAGE SECTION. 01 STUDENT-MARKS PIC 99. 01 GRADE PIC X. PROCEDURE DIVISION. DISPLAY "ENTER MARKS: ". ACCEPT STUDENT-MARKS. IF STUDENT-MARKS >= 35 DISPLAY "PASS" ELSE DISPLAY "FAIL". EVALUATE STUDENT-MARKS WHEN 90 THRU 100 MOVE "A" TO GRADE WHEN 75 THRU 89 MOVE "B" TO GRADE WHEN 50 THRU 74 MOVE "C" TO GRADE WHEN OTHER MOVE "F" TO GRADE END-EVALUATE. DISPLAY "GRADE: " GRADE. STOP RUN.

"This Content Sponsored by SBO Digital Marketing.

Mobile-Based Part-Time Job Opportunity by SBO!

Earn money online by doing simple content publishing and sharing tasks. Here's how:

  • Job Type: Mobile-based part-time work
  • Work Involves:
    • Content publishing
    • Content sharing on social media
  • Time Required: As little as 1 hour a day
  • Earnings: ₹300 or more daily
  • Requirements:
    • Active Facebook and Instagram account
    • Basic knowledge of using mobile and social media

For more details:

WhatsApp your Name and Qualification to 9994104160

a.Online Part Time Jobs from Home

b.Work from Home Jobs Without Investment

c.Freelance Jobs Online for Students

d.Mobile Based Online Jobs

e.Daily Payment Online Jobs

Keyword & Tag: #OnlinePartTimeJob #WorkFromHome #EarnMoneyOnline #PartTimeJob #jobs #jobalerts #withoutinvestmentjob"



Comments