Hi, this is Yuichi, an N high school student.
This time I wrote COBOL with interest in mind. What kind of technology is the reason why I decided to touch COBOL, which I am not familiar with?
I found out while researching various things, so I will explain for beginners.
When creating a COBOL program, create the program after building the environment of Ubuntu or GnuCOBOL (formerly openCOBOL).
I'm Windows 10 and I don't even have Ubuntu, let alone GnuCOBOL. So, it is a form to prepare from scratch.
--Use Windows 10 --Do it on Ubuntu. --Use the vi editor.
WSL is required to use GnuCOBOL on Windows, so install WSL and Ubuntu.
Search for Turn Windows features on or off
.
When the Windows Features screen appears, check Windows Subsystem for Linux
.
The language may change depending on the environment, so if you do not have the above check item, look for the item Windows Subsystem for Linux
and check it.
Search for "Ubuntu 20.04 LTS" from the MicroSoft Store → Get → Start
After installation, you will only be prompted for your username and password for the first boot. The password also includes entering the confirmation password, so enter the password as well.
Enter new UNIX username: <username>
New password: <password>
This completes the setup for the time being.
It's okay to start Ubuntu terminal, but I'll change it to a terminal that has a Mac feel.
Search for "Windows Terminal Preview" in the MicroSoft Store → Get → Launch.
When you start it, a mac-like terminal will be displayed.
Click the down arrow (drop-down menu) in the title bar to display the menu. The terminals available on that PC are reflected here, and you can start any terminal. (PowerShell, command prompt, etc.) All you have to do is select Ubuntu 20.04 and you're done.
This completes the new terminal.
Execute the following command on the terminal.
--For Windows 10
Use WSL to use GnuCOBOL on Windows. Since I prepared Ubuntu for installation etc. earlier, execute the following command.
sudo apt update
sudo apt install open-cobol
--For macOS
Home Brew is used on macOS. You can install it from here.
brew install gnu-cobol
It will take more than 10 minutes to complete, so make good use of your time by reading a book during that time.
Finally the main subject. Let's actually write a program using COBOL. The procedure is as follows.
Execute the pwd command.
$ pwd
/mnt/c/Users/user
dev
directory, hello.cbl
.
As expected, execute the vi command and write the program with the vi editor.$ mkdir dev
$ cd dev
$ vi hello.cbl //Create COBOL source file
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY "Hello, World!".
STOP RUN.
-x
option is an option for creating an executable file.$ cobc -x ./hello.cbl
-x
option I mentioned earlier created a file named hello as an executable file.$ ls
hello hello.cbl
$ ./hello
Hello, World!
IDENTIFICATION DIVISION.
PROGRAM-ID.loop.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 I PIC 9(2).
01 CNT PIC 9(2).
PROCEDURE DIVISION.
MAIN.
MOVE 0 TO I
MOVE 0 TO CNT
PERFORM UNTIL I >= 10
ADD 1 TO I
ADD I TO CNT
DISPLAY "I=" I ", CNT=" CNT
END-PERFORM.
DISPLAY "CNT=" CNT
STOP RUN.
END PROGRAM loop.
Declare variables under DATA DIVISION.
and WORKING-STORAGE SECTION.
.
The range from PERFORM
to END-PERFORM
repeats the process,
Write as PERFORM UNTIL condition
.
After that, when you execute it, it will be as follows.
I=01, CNT=01
I=02, CNT=03
I=03, CNT=06
I=04, CNT=10
I=05, CNT=15
I=06, CNT=21
I=07, CNT=28
I=08, CNT=36
I=09, CNT=45
I=10, CNT=55
CNT=55
IDENTIFICATION DIVISION.
PROGRAM-ID. FizzBuzz.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N PIC 9(3) VALUE 1.
01 FIZZ PIC 9(1).
01 BUZZ PIC 9(1).
PROCEDURE DIVISION.
PERFORM 100 TIMES
COMPUTE FIZZ = FUNCTION MOD(N 3)
COMPUTE BUZZ = FUNCTION MOD(N 5)
IF FIZZ = 0 AND BUZZ = 0 THEN
DISPLAY "FizzBuzz"
ELSE IF FIZZ = 0 THEN
DISPLAY "Fizz"
ELSE IF BUZZ = 0 THEN
DISPLAY "Buzz"
ELSE
DISPLAY N
END-IF
END-IF
END-IF
ADD 1 TO N
END-PERFORM.
STOP RUN.
With PERFORM 100 TIMES
, it repeats 100 times.
COMPUTE FIZZ = FUNCTION MOD (N 3)
assigns the remainder of N divided by 3 to the variable FIZZ.
↓ Execution
001
002
Fizz
004
Buzz
Fizz
007
008
Fizz
Buzz
011
Fizz
013
014
FizzBuzz
...
That's all from building the COBOL environment to writing the program. By design, COBOL is characterized by redundant syntax, and I think it's a language with a strong habit. The code of the FizzBuzz problem is not so different from other programming languages such as the if else part.
For those who want to write in VScode, you can use syntax highlighting and input completion by using an extension called "COBOL", so please use it. (Is there anyone who wants to write COBOL?)
-COBOL is a hot topic in corona ?! Languages over 60 years ago are a hot topic recently -Install Ubuntu 20.04 LTS on Windows 10 -[COBOL] Until even a first-time user can easily build a development environment (OpenCOBOL) and create and execute Hello World programs
Recommended Posts