Branching with IF and ELSE in COBOL is straightforward.
To run the program, compile the COBOL code and execute the resulting program:
Note that in COBOL, you need to use END-IF to close each IF statement. Also, COBOL doesn’t have a built-in modulus operator, so we use the FUNCTION MOD to achieve the same result.
COBOL uses AND and OR for logical operations, which is more verbose but arguably more readable than symbols like && and ||.
In COBOL, variable declarations are typically done in the WORKING-STORAGE SECTION, and we can’t declare variables within control structures. Instead, we set the variable’s value before using it in the IF statement.
COBOL doesn’t have a ternary operator, so you’ll need to use a full IF statement even for basic conditions.