- What are conditional expressions in COBOL? We have to make decisions everyday. If it's raining when I leave for work, I will bring an umbrella. This is an example of a conditional expression, if something is true then perform some task. Programming languages work in a similar fashion, if some condition is true then you perform a certain set of code. In COBOL, we don't use curly brackets to denote the start and stop of a block of code. Instead, we have to explicitly designate the end of the condition using the keyword then and also to indicate the end of an if statement we use the keyword and if.
If statements can be nest within COBOL just like other languages. In COBOL, there are some special relational conditions that we can easily test. For example, we can check to see if a data field is numeric or if it is not numeric. We can also test for alphabetic, even uppercase using alphabetic-upper or lowercase alphabetic-lower. These tests are helpful when we read from the file we want to check the data integrity of the values in the file.
I have my car salesperson program open, let's add a check to the program to make sure the field with a number of cars sold is numeric. If not, we'll print out an error message. So in line 149, I'm going to add an if statement, I'm going to say if car total is numeric then I will compute the working storage total car sales the way it was before.
Now what I don't want to do here is if it's not numeric, so I have to use the keyword else. If not, I'm just going to display an error message that says invalid car sales and I'm actually going to print out whatever the value was in the car total field so I can see what was wrong. Now I need to say that is the end of my if statement, so I need to add the and if. I'm going to save the program and I've already made a copy of my car sales data file and I call it carsales2.dat and notice in the first line I've changed the number of cars sold for Matt Thomas from 10 to abc.
This way, when my program runs it'll check to see if that value is numeric we should get a message saying invalid car sales and it'll tell us what the value was in that field, in our case abc. Okay let's make sure we compile and run this, so let's go over to our terminal window and I'm going to start by compiling it. So I'm going to use the c-o-b-c, the dash x and the name, this is 0303carsales, hit enter.
I didn't get any errors so now I can run the program by doing dot slash and the name of the program 03_03_car whoops lowercase it is case sensitive, underscore sales and no extension. Okay and if I hit enter, it read the new carsales2.dat file and as you can see right below Matt Thomas I have my error message invalid car sales and I displayed the value in that field which was abc.
So now someone can go back and figure out why that field was wrong and make sure that they update the file correctly. We can also use conditional tests for testing to see if one data field is greater than the other, if it's less than, if it's equal to, greater than or equal to, if it's positive or negative. We can also negate the condition just by using the keyword not. And finally, we can combine multiple conditions using and or the keyword or.
This course is designed to help new and experienced programmers alike add COBOL (or add COBOL back) to their skill set. Peggy Fisher shows how to get a COBOL development environment up and running and how to start programming. She reviews COBOL's data types and constants, control structures, file storage and processing methods, tables, and strings. Challenges issued along the way will help you practice what you've learned.
- Downloading and installing Cygwin and GNU COBOL
- Editing, compiling, linking, and running COBOL programs
- Describing data in COBOL
- Working with verbs and expressions
- Using branching
- Reading and writing sequential files
- Updating and deleting records
- Working with relative and indexed files
- Creating and searching tables
- Handling strings