Hello again!
Wow, I have to say… I feel good! Do you feel good? I really hope you do.
Today I have planned to write my own very first piece of COBOL code. That means I’ll write my own JCL to compile the COBOL and execute it. Let’s keep it simple today. To be honest, that’s an easy way to hide the fact that I don’t fully know what I’m doing, haha. The next step will be writing COBOL code that reads a data set? But before I do that, I want to make some blogposts on Zowe first, as Master the Mainframe 2019 is approaching!
Is this blog a tutorial? Hmm, it might be. I just write about what I’m doing in a tutorial-like way. Why? Because I like to document stuff, weird, I know. I want to explain the COBOL and the JCL I am writing. I hope to do a good job as I don’t want to blunder, after all, you never know who reads this. Hi FBI!
Today’s assignment
Hello world! KIDDING! That would be too much of a Kevin thing to do. Let’s make a FizzBuzz. This will learn me how to code a fizz buzz and it might help you during a coding interview. There’s tons of FizzBuzzes out there, probably also in COBOL, but this one will be explained, coded inside a mainframe, executed by JCL and made with tons of love.
So what is a FizzBuzz?
A FizzBuzz is a very easy programming task that’s used in most software developer interviews. It was invented by Imran Ghory.
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
So let’s do this in COBOL, compile it using JCL and see if I can get it to work. For this blog’s purpose, I’ll code the simple version of FizzBuzz.
Pre-Workout: the JCL
We’ll copy the JCL from TESTCOB that we’ve reviewed last time. Don’t worry, copying code is not a crime as long as you know what the code actually does.
So I went to RFE (1), UTILITIES (3), DSLIST (4), entered data set name prefix SYS2.JCLLIB, edited SYS2.JCLLIB and I made a new member using the primary command s.
After this the new empty member was created.
Into the new member FIZZBUZZ I want to COPY TESTCOB. This can be done with ease using, you’ve guessed it, COPY TESTCOB.
Done, all copied! So what’s next? First we’ll take note of Marco’s comment and rename the job to our user profile name. In my case it’s HERC01.
I have changed TESTCOB to HERC01. I also have made some other small changes. There might be some issues running the job as we’re using our username as the jobname, we already have a job running with our name the moment we login. So that’s the reason it will ask for a jobname.
Workout: the COBOL
Defining variables in COBOL
So there was already a constant defined called HELLO-CONST. This variable was defined in the WORKING-STORAGE section. I have done the same, I have defined three new variables called WS-FIZZ, WS-BUZZ and WS-FIZZBUZZ. I prefix them with WS to let others know that this is a variable in the WORKING-STORAGE section. This is a common practice.
Every variable except for indexes are defined with a level number. In this case the level number for WS-FIZZ is 77, not cool, but ok. Most companies don’t like using level number 77 as it wastes space, some even have a policy against using it. For more details about this visit mainframestechhelp.com.
Looping in COBOL
In addition to the above mentioned text working storage variables I have defined 3 numeric variables that can each be up to 3 (numeric) characters long. I have initialized WS-I variable with 1, as we don’t want to check 0 for fizzbuzz. The other variables I have initialized with 0. WS-I is short for working storage indice and WS-R is short for working storage remainder.
PERFORM CALCUL UNTIL WS-I > 99. This line of code performs the “function” named CALCUL until the WS-I variable is larger than 99. After that it will stop the execution. I know, CALCUL is not an ideal name, but say it out loud. Sounds cool right?
Biggest COBOL amateur at work
Believe me, this is so embarassing. I had so much troubles with the COBOL section, it is unreal. The END-IF should begin A-MARGIN. But I couldn’t start my IF in the A-margin? Also COBOL expected a condition for the else? But when I provided a condition it didn’t work?
Well then, I guess I’ll have to work around the fact that I can’t use ELSE.
Lo and behold the production of me learning COBOL for 2 hours on a friday night. Because I can’t use the ELSE for some reason, I just checked if WS-R was bigger than 0 and then called the next “function” in line.
DIVIDE 3 INTO WS-I GIVING WS-QUOTIENT REMAINDER WS-R
The line of code above equals to WS-QUOTIENT = WS-I / 3 and the remainder value will be stored in WS-R. The result of this statement will be stored in WS-QUOTIENT. If you’re not familiar with this, you’re probably thinking: “But 45/3 is not 0? So it can’t be FizzBuzz with this code?”.
It works like this:
- 45 divided by 15 equals 3. The remainder is 0, so it’s FizzBuzz.
- 48 divided by 15 equals 3.2. So the remainder is 0.2, not FizzBuzz.
- 48 divided by 3 equals 16. So the remainder is 0, so it’s Fizz.
I really don’t like putting this ugly code out here, but I just kind of felt like it. If any of you would like to help me out, or yell at me, feel free! 🙂 I can do better sir, just not right now, heheh. (sweating)
Does it FizzBuzz?
Let me check! SUBMIT, check the output via RFE > 3.8 > select the job and see.
I really did it! I have to say, I never expected to get this far with the little COBOL learning I did. To conclude, I really should study and practice before I just start doing things.
Up next: Zowe?
Not too long ago Zowe has announced that it’s now available on IBM Z Trial! It’s only a 3 day trial though. So this means I’ll have to request the trial on a friday so I can solve the introduction and blog about it in one weekend.
Zowe will also be used in the new Master the Mainframe 2019 challenges, so this will be a very nice warm up to prepare us for that big moment in september!
Hi kevin,
I’m using MVS 3.8j, tk4 in my windows 10 system. I created a cobol program to read a sequential file, however i’m not able to use if condition in my program and I get the following error message:
IKF4086I-C
IF CONDITION USED WHERE ONLY IMPERATIVE STATEMENTS ARE LEGAL MAY CAUSE ERRORS IN PROCESSING.
Could you please help me resolve this issue ?
Hi Kevin.
Have fun, also with KICKS.
I started a blog about using KICKS and VSAM in TK4.
idiotmainframe.blogspot.com
Also, for operation for input of cards and very nice output see the free MVS OPS.
https://mvs.gadsby.me.uk
And head over to the Moshox discord channel, lots of patience and polite people.
Hi Kevin. Try this free software to print jobs in TK4. http://timpinkawa.net/hercules/prtspool.html
Hi Frank
Thanks for sharing that resource 🙂 I have visited Tim’s Hercules Page in the past and this tool is excellent.
I believe your recommendation is a good free alternative to the tool “hercprt” that a commenter below, Gerard, has shared.
Stay safe,
Kind regards,
Kevin
Ok, I decided to take up this challenge myself.
Strangely enough, I used the distribution of TK4 that comes with Hercules and I was able to use ELSE and even a nested IF statement. My PROCEDURE DIVISION looks like this:
00051 MAIN-PARAGRAPH.
00052 OPEN INPUT INFILE.
00053 READ INFILE AT END DISPLAY ‘READY FIZZBUZZ’.
00054 MOVE UP-TO-NUMBER (1) TO N.
00055 CLOSE INFILE.
00056 *
00057 IF N > 0 AND N < 10000
00058 PERFORM PRINT-FIZZBUZZ UNTIL I GREATER N
00059 ELSE
00060 DISPLAY 'INPUT IS TOO LARGE OR TOO SMALL'.
00061 *
00062 DISPLAY 'FIN'.
00063 STOP RUN.
00064 **
00065 **
00066 PRINT-FIZZBUZZ.
00067 DIVIDE I BY 3 GIVING DIV REMAINDER REMAIN-T.
00068 DIVIDE I BY 5 GIVING DIV REMAINDER REMAIN-F.
00069 *
00070 IF REMAIN-T EQUAL ZERO AND REMAIN-F EQUAL ZERO
00071 DISPLAY 'FIZZBUZZ'
00072 ELSE
00073 IF REMAIN-F EQUAL ZERO
00074 DISPLAY 'BUZZ'
00075 ELSE
00076 IF REMAIN-T EQUAL ZERO
00077 DISPLAY 'FIZZ'
00078 ELSE
00079 DISPLAY I.
00080 *
00081 ADD 1 TO I.
(I had sent it with proper indentation, I don’t know why it disappeared when I sent it)
Hi Anonymous
Thanks for sharing! Sorry about the indentation, I believe it’s the way WordPress works without using real code plugins.
Kind regards
– Kevin
I would suggest for clarity that in the paragraph ‘Looping in Cobol’ you would start with something like: ‘In addition to the above mentioned text working storage variables I have defined 3 numeric variables….. etc’
Otherwise nice to see someone who likes the hercules / mvs environment.
Hi Gerard!
You’re right! I didn’t really talk about my other defined variables, I should have mentioned them too. I blame the editor for this mistake 😉
My interest in MVS/Hercules and blogging about it is definitely here to stay! I am glad there’s still people interested in reading such beginner posts about MVS and Hercules!
Are you working on your own mainframe too?
Kind regards,
– Kevin
Yes I have TK4- on an ubuntu linux 16.04 LTS old (2005 !!) IBM desktop.
I do TSO from my windows desktop
I have purchased hercprt (hercules remote print spooler) for looking at output in PDF format on Windows. You can use hercprt also for free.
kind
Groetjes, (.be ??)
Gerard, Johannesburg.
Hi Gerard!
Sounds like a very cool setup! I’m thinking of getting a refurbished computer and run TK4- on it too.
Hercprt looks really cool! I’ll take a look! I never thought about exploring the net for third-party software, thanks for the tip 🙂
Inderdaad, groetjes!
– Kevin, Ieper.
Very interesting post and glad you are back! (Also, Calcul does sound very cool!)