- [Voiceover] In COBOL we have computational verbs in addition to verbs that allow us to initialize, display, accept and move data, we also have verbs to perform calculations or computations. I've already created a program that reads a file with information about a local car dealership. Specifically, the file has the salesperson's name, quarterly sales, and the total number of cars sold. Let's look at the file. It's called carsales.dat.
As you can see I start with the last name, then the first name, next is the quarterly sales, and finally the last three numbers of the total car sold for each salesperson. So for example, Matt Thomas sold 10 cars, while Eric Henderson sold five cars. The goal of the program is to print out a report that shows each salesperson's name, their quarterly sales, and their total for the year. Let's go back over to the program. In this program we're going to use one of the compute verbs which is the add verb.
It is used to add two or more numbers and store the result in the destination variable. When we read each record in our file, we need to add the quarterly sales to the total for the quarter and also add it to the total for the year. Let's scroll down to line 135. As you can see, on line 136 I have the very first occurrence of my add verb. I say add Q1 sales to the working storage Q1 sales variable and also to the WS salesperson yearly.
And you can see that I do this four times, one for each quarter. There is also a subtract verb. This is used for subtraction operations. The difference is that you use the words subtract X from Y giving Z. Let's add an example to our program. I'm going to scroll down to around line 154 actually line 155 and I'm going to add a subtract statement. So I'm going to type in subtract, and I'm just going to take away 10 from the total car sales.
So I say subtract 10 from WS total car sales and then I have to specify where to put the result. So I say, giving WS cars minus 10. This is an example of how we can take away 10 from the variable total car sales and place that value into a variable WS cars minus 10. Similar to the add verb, it is also possible to subtract multiple values from another value and store it in a third value using the giving keyword.
Multiply and divide are similar to the subtract verb. The syntax for the multiply verb looks like this. Multiply A by B giving C. And divide would be, divide A into B which puts the results into the variable B or divide A by B giving C. Remainder R. That would put the integer portion of A divided by B into the variable C and the remainder will be put into the letter R.
Ok. If this is all a little confusing, have no fear. The next verb is the compute verb. And it is used to write arithmetic expressions in a form using the plus sign, the minus sign, the asterisk for multiplication and the slash for divide. The format of the compute statement is just as important. You start with the resulting variable and you set it equal to an equation. For example, A equals B times C. Remember your order of operations, and use paretheses as needed to dictate an alternate order.
This syntax aligns better with Java, C Sharp and Python. Let's scroll down to line 167 where I set up some statements to compute the average car price for all salespeople at the dealership. You can see right now I have it commented out so I'm going to take out my comments. On line 168 I have a compute statement. It says compute, the working storage car price average is equal to the working storage yearly sales which has the total for all salespeople at the dealership, and I divide that by using a slash WS total car sales.
Next, I move the resulting value to a display field. I display a blank line and then I display the total car sales and the average car price. There are more verbs that you can use in COBOL but this is the short list of verbs that you will probably encounter or need the most. Now, let's run the program so you can see how these verbs work. I'm going to open my terminal editor window and I'm going to compile my program using the COB C command.
I'm going to create an executable, which is why I used the dash x, and finally I put the name of my program with the .cbl extension. I hit enter, there's no errors. So now I can run the program. So I type dot, slash, and the name of the program. zero, three, underscore, zero, two, underscore, compute, underscore, verbs with no extension, and when I hit enter let's take a look at what we have.
It starts with the salesperson name and then it has the quarter one sales, quarter two sales, quarter three, quarter four, the yearly sales. I also have the totals for each salesperson and finally that 207 is the value of the total car sales which is 217 minus 10, which I put in there just to show how you can use the subtract verb. We'd want to take that out if we were doing this report for production. And finally the average car price, which turns out the average car price for our dealership is $37,288.
As you saw, COBOL provides verbs such as add, subract, multiply and divide. But for me, I find it a lot easier to use the compute verb and then use these symbols, plus, minus, asterisk and slash for addition, subtraction, multiplication and division respectively.
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