A Series on Sample Syntax for 25 Different Computer Programming Languages - Article 1
I came up with the idea for this article series at the ‘witching hour’ which is when I tend to wake up with the most abstract thoughts and nagging bouts of curiosity. I don’t believe I’ve seen a comprehensive study on the sample syntax for so many programming languages in a single place. And as I thought through the challenge, my mind kept constantly reminding me that a post like this could get way out of hand. First, I had to come up with a good list of languages from the countless languages which have been released to those that have been abandoned. And when I settled on the list of languages I didn’t want to just do a basic ‘Hello World’ for each, but what was a suitable set of examples? So, here’s what I landed on for the list of languages and what the syntax (code) examples will be for each.
The List of Languages by Article
[You are here] Article 1: Fortran
, COBOL
, Lisp
, BASIC
, Algol
, C
, Pascal
, Ada
Article 2: Prolog
, Smalltalk
, C++
, Perl
, Python
, Ruby
, Java
, JavaScript
, PHP
Article 3: Swift
, Kotlin
, Go
, Rust
, Julia
, Dart
, TypeScript
, R
The Syntax Examples
“Hello World!”
If / Then / Else, or similar
Iteration over a collection of items, such as a list or an array
Defining and calling a function
I hope the work I’ve done here helps whoever may stumble upon it! And if I’ve made an error in the syntax I’ve shared, please leave a comment so that I can correct it!
Fortran
Fortran, which stands for “Formula Translation,” is a high-level programming language that was developed in the 1950s by IBM. It was designed for scientific and engineering computations and is widely used for numerical computations, data processing, and scientific applications.
Fortran is that it is one of the oldest programming languages still in use today, with the first version of the language dating back to 1957. Despite its age, Fortran remains a popular language for scientific computing due to its efficiency and strong support for numerical computations. It has gone through many revisions and updates, with the latest version being Fortran 2018, which was released in 2018.
Hello World!
PROGRAM HELLO
WRITE(*,*) 'Hello, World!'
END PROGRAM HELLO
If / Then / Else, or Similar
IF (x .GT. 0) THEN
write(*,*) "x is positive"
ELSEIF (x .EQ. 0) THEN
write(*,*) "x is zero"
ELSE
write(*,*) "x is negative"
END IF
Iteration Over a Collection of Items, Such as a List or an Array
This program initializes an array a with five values, and then iterates over the array using a do loop. The values of the array are printed to the console.
program main
implicit none
integer :: i, j
real :: a(5) = [1.0, 2.0, 3.0, 4.0, 5.0]
do i = 1, 5
write(*,*) a(i)
end do
end program main
Defining and Calling a Function
This program defines a function ‘square’ that takes a single argument x and returns its square. The main program initializes a variable x to 1.0, and then calls the square function to compute its square, which is assigned to the variable y. Finally, the value of y is printed to the console.
program main
implicit none
real :: x, y
x = 1.0
y = square(x)
write(*,*) y
contains
function square(x) result(y)
real, intent(in) :: x
real :: y
y = x * x
end function square
end program main
COBOL
COBOL (COmmon Business Oriented Language) is a high-level programming language designed for business applications. It was first developed in the late 1950s and quickly became popular for writing programs for large business and financial systems. COBOL is known for its verbosity and use of English-like phrases, which makes it easy to read and understand for non-programmers. It is still used today in many legacy systems, particularly in the finance and government sectors.
It is estimated that over 220 billion lines of COBOL code are still in use today, powering a significant portion of the world’s financial and administrative systems. Despite being over 60 years old, COBOL remains relevant due to the high cost of replacing legacy systems, which often require the expertise of experienced COBOL programmers to maintain and update.
Hello World!
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
PROCEDURE DIVISION.
DISPLAY 'Hello, World!'.
STOP RUN.
If / Then / Else, or Similar
IF x > 0
DISPLAY 'x is positive'
ELSE IF x = 0
DISPLAY 'x is zero'
ELSE
DISPLAY 'x is negative'
END-IF
Iteration Over a Collection of Items, Such as a List or an Array
In this example, we define an array MY-ARRAY with five elements of length 10. We then populate the array with five values. We use a PERFORM loop with a VARYING clause to iterate over the array, displaying the value of each element to the console.
Note that in COBOL, array indices start at 1, not 0.
IDENTIFICATION DIVISION.
PROGRAM-ID. ARRAY-ITERATION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MY-ARRAY.
05 MY-ELEMENTS OCCURS 5 TIMES PIC X(10).
PROCEDURE DIVISION.
MAIN-LOGIC.
MOVE "Alice" TO MY-ELEMENTS(1)
MOVE "Bob" TO MY-ELEMENTS(2)
MOVE "Charlie" TO MY-ELEMENTS(3)
MOVE "Dave" TO MY-ELEMENTS(4)
MOVE "Eve" TO MY-ELEMENTS(5)
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 5
DISPLAY MY-ELEMENTS(I)
END-PERFORM
STOP RUN.
Defining and Calling a Function
In COBOL, we can define a function using the FUNCTION keyword, and call it using its name and arguments. In this example, we define a function called MY-FUNCTION that takes a single argument X and returns a value of type COMP (a signed integer with 5 digits). The function computes the square of X, adds 1 to the result, and returns the final value.
We then call the MY-FUNCTION function from the MAIN-LOGIC section, passing in a value of 3 for the X argument. The function returns a value of 10, which we store in the RESULT variable. Finally, we use the DISPLAY statement to output the value of RESULT to the console.
IDENTIFICATION DIVISION.
PROGRAM-ID. FUNCTION-EXAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ARGUMENT PIC S9(5) COMP VALUE 3.
01 RESULT PIC S9(5) COMP.
PROCEDURE DIVISION.
MAIN-LOGIC.
COMPUTE RESULT = MY-FUNCTION(ARGUMENT)
DISPLAY "Result is " RESULT
STOP RUN.
MY-FUNCTION SECTION.
FUNCTION MY-FUNCTION (X PIC S9(5) COMP)
COMPUTE MY-FUNCTION = X ** 2 + 1
END-FUNCTION.
Lisp
Lisp is a family of programming languages with a long history, dating back to the 1950s. It is a functional programming language that is well-suited for processing symbolic data and for creating domain-specific languages. Lisp is also known for its use of parentheses as a primary means of grouping and organizing code, which can take some getting used to for programmers who are used to more traditional syntax.
Despite being one of the oldest programming languages, Lisp is still in use today. Many of the concepts that are now considered fundamental to programming, such as garbage collection and dynamic typing, were first developed in Lisp. Lisp is also known for its strong influence on the development of artificial intelligence and machine learning, and many popular programming languages today have borrowed ideas and concepts from Lisp.
Hello World!
(print "Hello, World!")
If / Then / Else, or Similar
(if (> x 0)
(print "x is positive")
(if (= x 0)
(print "x is zero")
(print "x is negative")))
Iteration Over a Collection of Items, Such as a List or an Array
In this example, my-list is defined as a list of integers. The dolist loop is then used to iterate over the list, binding each item in turn to the variable item. The body of the loop simply prints out each item in the list.
;; Define a list
(defvar my-list '(1 2 3 4 5))
;; Iterate over the list using the dolist loop
(dolist (item my-list)
(print item))
Defining and Calling a Function
In the below code, we define a function called add using the defun macro. The add function takes two arguments, x and y, and returns their sum. We then call the add function with the arguments 2 and 3, and the result is 5.
; define a function that adds two numbers
(defun add (x y)
(+ x y))
; call the add function with arguments 2 and 3
(add 2 3)
BASIC
BASIC is a high-level programming language designed to be simple to use and understand, making it a popular choice for beginners. It was originally created in the mid-1960s as a way to teach computer programming to students.
BASIC stands for “Beginner’s All-purpose Symbolic Instruction Code”. It was originally developed by John Kemeny and Thomas Kurtz at Dartmouth College in the 1960s. In addition to being a popular language for learning programming, BASIC has been used for a wide range of applications, including business, scientific, and educational purposes. One unique fact about BASIC is that it was the first programming language to be included as a standard feature on personal computers, including the original IBM PC released in 1981.
Hello World!
10 PRINT "Hello, World!"
If / Then / Else, or Similar
10 IF x > 0 THEN
20 PRINT "x is positive"
30 ELSEIF x = 0 THEN
40 PRINT "x is zero"
50 ELSE
60 PRINT "x is negative"
70 END IF
Iteration Over a Collection of Items, Such as a List or an Array
In the below code, we define an array called numbers that contains five integers. We then use a FOR loop to iterate over the array, printing each element to the console.
' define an array of numbers
DIM numbers(5)
numbers(1) = 1
numbers(2) = 2
numbers(3) = 3
numbers(4) = 4
numbers(5) = 5
' iterate over the array and print each element
FOR i = 1 TO 5
PRINT numbers(i)
NEXT i
Defining and Calling a Function
In this example, we define a function called add using the FUNCTION keyword. The add function takes two arguments, x and y, and returns their sum. We then call the add function with the arguments 2 and 3, and assign the result to a variable called result. Finally, we print the value of result to the console.
' define a function that adds two numbers
FUNCTION add(x, y)
add = x + y
END FUNCTION
' call the add function with arguments 2 and 3
result = add(2, 3)
PRINT result
Algol
Algol, short for Algorithmic Language, is a high-level programming language that was developed in the late 1950s and early 1960s as an international effort to create a standard language for scientific computing. Algol was designed to be a precise and unambiguous language that could express complex mathematical algorithms in a clear and concise way. It was also one of the first languages to introduce the concept of structured programming, which organized programs into modular blocks to improve readability and maintainability.
Unique fact: Algol was the first language to use the curly braces “{“ and “}” as a syntax for code blocks, which is now widely used in many programming languages.
Hello World!
BEGIN
OUTPUT("Hello, World!")
END.
If / Then / Else, or Similar
IF x > 0 THEN
OUT("x is positive")
ELSIF x = 0 THEN
OUT("x is zero")
ELSE
OUT("x is negative")
FI
Iteration Over a Collection of Items, Such as a List or an Array
In the below code, we define an array called numbers that contains five integers. We then use a FOR loop to iterate over the array, printing each element to the console using the WRITE statement.
BEGIN
ARRAY numbers[1:5] = [1, 2, 3, 4, 5];
FOR i := 1 UNTIL 5 DO
WRITE(numbers[i])
OD
END
Defining and Calling a Function
In this example, we define a function called add using the PROC keyword. The add function takes two arguments, x and y, and returns their sum as an integer. We then call the add function with the arguments 2 and 3, and assign the result to a variable called result. Finally, we print the value of result to the console using the WRITE statement.
BEGIN
PROC add(x, y) = (INT: x + y);
INT result := add(2, 3);
WRITE(result)
END
C
C is a general-purpose, high-level programming language that was developed in the early 1970s by Dennis Ritchie at Bell Labs. It was designed to be a simple and efficient language that could be used for a wide range of applications, including system programming, embedded systems, and application development. C has become one of the most widely used programming languages due to its high portability, efficient memory management, and ability to interface with low-level hardware.
Unique fact: C was originally developed as a system programming language for the Unix operating system, which was also created by Dennis Ritchie and his colleagues at Bell Labs. In fact, much of Unix was actually written in C, which helped to popularize the language and make it the go-to language for system programming.
Hello World!
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
If / Then / Else, or Similar
if (x > 0) {
printf("x is positive\n");
} else if (x == 0) {
printf("x is zero\n");
} else {
printf("x is negative\n");
}
Iteration Over a Collection of Items, Such as a List or an Array
In this example, we define an array called numbers that contains five integers. We then use a for loop to iterate over the array, printing each element to the console using the printf function.
#include <stdio.h>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Defining and Calling a Function
In this example, we define a function called add that takes two integer arguments x and y, and returns their sum. We then call the add function with the arguments 2 and 3, and assign the result to a variable called result. Finally, we print the value of result to the console using the printf function.
#include <stdio.h>
int add(int x, int y) {
return x + y;
}
int main() {
int result = add(2, 3);
printf("%d", result);
return 0;
}
Pascal
Pascal is a high-level, imperative programming language that was designed in the late 1960s and early 1970s by Niklaus Wirth. It was named after the French mathematician and philosopher Blaise Pascal and was designed to be a simple and efficient language for teaching programming and for developing software in a structured manner. Pascal features strong type checking, modular programming constructs, and built-in data types and structures.
Interestingly enough, Pascal was the first programming language to introduce the concept of a case-insensitive language, which means that upper-case and lower-case letters can be used interchangeably. This was a significant departure from the all-caps syntax of earlier programming languages, and it made Pascal more readable and easier to use for both novice and experienced programmers.
Hello World!
program Hello;
begin
writeln('Hello, World!');
end.
If / Then / Else, or Similar
if x > 0 then
writeln('x is positive')
else if x = 0 then
writeln('x is zero')
else
writeln('x is negative');
Iteration Over a Collection of Items, Such as a List or an Array
In this example, we define an array called numbers that contains five integers, and a variable i of type integer. We then use a for loop to iterate over the array, printing each element to the console using the writeln procedure.
program PascalLoop;
var
numbers: array[0..4] of integer = (1, 2, 3, 4, 5);
i: integer;
begin
for i := 0 to 4 do
writeln(numbers[i]);
end.
Defining and Calling a Function
In this example, we define a function called add that takes two integer arguments x and y, and returns their sum as an integer. We then declare a variable called result of type integer, and assign the result of calling the add function with the arguments 2 and 3 to result. Finally, we print the value of result to the console using the writeln procedure.
program PascalFunction;
function add(x, y: integer): integer;
begin
add := x + y;
end;
var
result: integer;
begin
result := add(2, 3);
writeln(result);
end.
Ada
Ada is a high-level, structured, object-oriented programming language that was designed in the late 1970s and early 1980s by a team led by Jean Ichbiah at Honeywell Bull. Ada was intended to be a robust, reliable, and efficient language for safety-critical and real-time systems, and it has been widely used in applications such as aviation, defense, and space exploration. Ada features strong typing, modular programming constructs, and built-in support for concurrency, exception handling, and low-level programming.
The development of Ada was sponsored by the United States Department of Defense, which was seeking to standardize the software development process for its various projects. As a result, Ada was designed to be highly structured and rigorous, with a strong emphasis on code safety and reliability. The language was named after Ada Lovelace, a 19th-century mathematician and writer who is widely considered to be the world’s first computer programmer.
Hello World!
with Ada.Text_IO;
procedure Hello is
begin
Ada.Text_IO.Put_Line("Hello, World!");
end Hello;
If / Then / Else, or Similar
if x > 0 then
Ada.Text_IO.Put_Line("x is positive");
elsif x = 0 then
Ada.Text_IO.Put_Line("x is zero");
else
Ada.Text_IO.Put_Line("x is negative");
end if;
Iteration Over a Collection of Items, Such as a List or an Array
In this example, we use a for loop to iterate over the indices of the array A and print each element using the ‘Image attribute, which converts the integer to a string for printing.
with Ada.Text_IO; use Ada.Text_IO;
procedure Iterate_Array is
-- Declare an array of integers
A : array (1..5) of Integer := (1, 2, 3, 4, 5);
begin
-- Loop over the array and print each element
for I in 1..A'Length loop
Put_Line(A(I)'Image);
end loop;
end Iterate_Array;
Defining and Calling a Function
In this example, we define a function Factorial that recursively calculates the factorial of an integer. We then call this function with argument 5 and print the result using the ‘Image attribute. Note that in Ada, function and procedure definitions must appear before any calls to them.
function Factorial(N : Integer) return Integer is
begin
if N = 0 then
return 1;
else
return N * Factorial(N-1);
end if;
end Factorial;
procedure Test_Factorial is
Result : Integer;
begin
-- Call the Factorial function with argument 5
Result := Factorial(5);
Put_Line("Factorial(5) = " & Result'Image);
end Test_Factorial;