- In COBOL, we don't have a For-Loop to perform a block of code or a paragraph a set number of times like we do in Java or C++. Instead, we will use the perform statement again. But this time, we'll use PERFORM and TIMES. In this example, we will perform the print block of code ten times. We will print out a countdown from ten to one and then print the word "blastoff" when the counter gets to zero. As you can see here, I have a shell program that has two variables defined in the working storage.
The first variable, WS-COUNTER, is used to start at ten and then in the code, I will subtract one each time. But when I print the value for the counter I don't want to see leading zeros so I've added a second variable that has ZZ99 for the picture clause. This will allow me to not print the leading zeros but allows up to three digits. Although I'm only starting with ten but if I wanted to change the program to start at a hundred, I allowed for three digits.
Let's start by adding a paragraph to print the countdown variables. So on line 23, in the print paragraph I'm going to add my code to first move WS-COUNTER to WS-DISPLAY-COUNTER then I'm going to display the WS-DISPLAY-COUNTER variable. Notice I'm placing a period at the end of each sentence because I'm inside the 0200-PRINT paragraph. Next I want to subtract one from my WS-COUNTER variable and I want to check and see if I'm down to zero.
Because if I am, I want to print "blastoff." So I'm going to use an "if" statement. If WS-COUNTER equals zero. This is another difference in syntax that I'd like to point out. You might be used to, if you're a programmer, using two equal signs. In COBOL, we go back to just using one. So if WS-COUNTER equals zero, I use the keyword "then," if that happens, I want to display the word "blastoff." And here I don't use a period because I'm inside of my "if" statement and I might want to do more than one thing.
In this case, I don't, so on the next line I'm just going to end my "if" statement. "END-IF." Okay, now we want to add the loop portion. So let's go back up to line 19 to the start paragraph and we're going to add "perform the paragraph 0200-PRINT 10 times." After I'm done printing my "ten, nine, eight seven, six, five, four, three, two, one, blastoff," I want to stop the program.
So below that, I'll tell it to perform "0300-STOP-RUN." Okay, this is an example of using Perform and Times. In this case, I'm going to perform the paragraph "0200-PRINT" ten times. Let's save our program, switch over to our terminal window, and we can test it out. So the first thing I want to do is compile it. So I'm going to use my command "COBC-X space, the name of the program, in my case, 03_06_countdown.CBL Okay that looks good, now I want to run it.
So I'm going to use "./03_06_countdown." Oops, I have to have lowercase, because it has to match exactly. Countdown with no extension. And if I hit enter ... And there we go, we have a successful program that counts down from ten to one and then prints the word "blastoff." This was a really short example but I wanted you to see how you can use the PERFORM and TIMES.
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