- A literal is an exact value or a constant value. There are two types of literals, numeric and nonnumeric. Each of these types has it's own rules. Let's start with the numeric literals. A numeric literal can be up to 18 digits long, it can begin with a leading plus or minus sign and finally, a numeric literal may contain a decimal point but it may not end with a decimal point. A nonnumeric literal is enclosed in apostrophes or quotation marks as specified by the compiler.
An example might be single quotes or apostrophe hello space world end quote. Or in this sample program on line 28, I define in a variable called WS-DONE as a literal with a starting value of capital n. A nonnumeric literal may contain anything including spaces, numbers and even reserved words. The only thing that you don't want to include is another apostrophe. They should only be at the beginning and the end of your literal.
In this sample program, I also have an example of a numeric literal or a numeric constant. On line 22, I declare a working storage variable WS-STATE-TAX. I define it with a picture clause 9V99. Remember the V represents an implied decimal and I give it a value of 0.06 because Pennsylvania has a six percent sales tax and in this program I want to ask the user to enter an item, calculate the tax and provide the total cost.
Let's just look at the program to see how that value is used. I'm going to scroll down a little bit. On line 34 is my starting paragraph, 0100-PROCESS-RECORDS. The first thing I do is perform 0200 GET-NEXT-ITEM. That transfers control down to line 44. Line 45, it displays the words, Enter the cost of your item. I use the except verb to read the value into my WS-ITEM-COST then I add the cost to a total variable, I calculate the sales tax by multiply WS-STATE-TAX by WS-ITEM-COST giving me the WS-ITEM-TOTAL which is the total of the cost plus the tax.
I add the item total to my WS-TOTAL-COST variable and finally, at line 50, I display a message saying, Do you want to enter another item? Yes or no and I accept that value into WS-DONE. At this point, the flow of control goes back to my 0100-PROCESS-RECORDS to line 37 where it moves the TOTAL COST to DISPLAY TOTAL and then it displays the word TOTAL and the DISPLAY TOTAL value. Notice, that I had to declare an additional variable on line 27 that says, WS-DISPLAY-TOTAL.
Look at the pit clause. The pit clause has dollar signs in front of the 9.99, that allows for a floating dollar sign to be printed when the amount is displayed. Let's run the program to see how it works. Okay, I already have the program compiled so I need to run it. So I'm going to do ./ and then I specify the name, it's 02_03_constance. Remember, when you run the executable, you don't have to specify the extension.
I'm going to hit enter and it asks me for the cost of my item. I'll say $10 because it's easy to calculate the tax on $10. Do I want another item? No. And it prints out the total. You see the floating dollar sign? It says $10 and 60 cents. Again, because the state sales tax in Pennsylvania is currently six percent. The (mumbles) of other figurative constants that are helpful when programming in COBOL for example you might see high dash values, low dash values, zeros, spaces and quotes.
Let's go back to the sample program. I'm going to scroll up a little bit and if you notice on lines 24 and 25, I use the figurative constant for zeros to initialize my two ITEM COST and ITEM TOTAL variables. If you're defining an alphanumeric field, you might want to initialize the value to spaces. That's another figurative constant. Literals and constants are very helpful in any programming language because you can define a constant value, in this case, in working storage.
For example, my state tax of .06 and then I use that variable throughout the program. Now, if down the line, Pennsylvania changes their tax rate to 6.5 or maybe they lower it, that would be nice, to five percent, I only have to change it in one other place and all the other calculations will still continue to work but they'll use the new lower value. So, keep this in mind when you're writing your programs that if you have a value that is a constant, define a variable to hold that constant, that way, if it does have to change later on, you only have to change it in one place.
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