A Series on Sample Syntax for 25 Different Computer Programming Languages - Article 3
This is article 3 of 3 in my series of studying the syntax in 25 computer programming languages I’ve chosen. If you missed Article 1, please visit it here. Otherwise, here’s a brief summary of the languages I chose and what the syntax (code) examples are for each.
The List of Languages by Article
Article 1: Fortran
, COBOL
, Lisp
, BASIC
, Algol
, C
, Pascal
, Ada
Article 2: Prolog
, Smalltalk
, C++
, Perl
, Python
, Ruby
, Java
, JavaScript
, PHP
[You are here] 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!
Swift
Swift is a general-purpose programming language developed by Apple for building apps on iOS, macOS, watchOS, and tvOS. It is designed to be fast and efficient, and provides modern language features such as optionals, closures, and generics. Swift is strongly typed and features type inference, making it easier for developers to write clean and expressive code.
Swift was developed by Chris Lattner, who also created LLVM, the compiler infrastructure that is used by many programming languages including Swift itself. The development of Swift was started in 2010 by a small team at Apple, led by Lattner, and the first version of the language was released to the public in 2014. Since then, Swift has gained popularity as a modern and efficient programming language for building apps across Apple’s platforms.
Hello World!
print("Hello, World!")
If / Then / Else, or Similar
let x = 10
if x > 5 {
print("x is greater than 5")
} else {
print("x is less than or equal to 5")
}
Iteration Over a Collection of Items, Such as a List or an Array
The below example shows how we can iterate over an array of integers in Swift.
let numbers = [1, 2, 3, 4, 5]
for number in numbers {
print(number)
}
Defining and Calling a Function
The code shown below will output “Hello, Ira!” to the console. The greet function takes a single argument name of type String, and returns a string that is a greeting for the given name. The function is called with the argument “Ira”, and the returned greeting is assigned to the greeting constant, which is then printed to the console.
func greet(name: String) -> String {
return "Hello, \(name)!"
}
let greeting = greet(name: "Ira")
print(greeting)
Kotlin
Kotlin is a statically typed programming language that runs on the Java virtual machine and can also be compiled to JavaScript. It was designed to improve upon and modernize the Java language, with a focus on safety, interoperability, and expressiveness. Kotlin is used for developing a variety of applications, including web, mobile, desktop, and server-side applications.
Kotlin was developed by JetBrains, the company behind the popular IntelliJ IDEA integrated development environment (IDE) for Java. In fact, Kotlin was originally created as a language that could be used with IntelliJ IDEA and was later open-sourced.
Hello World!
fun main() {
println("Hello, World!")
}
If / Then / Else, or Similar
val x = 10
if (x > 5) {
println("x is greater than 5")
} else {
println("x is less than or equal to 5")
}
Iteration Over a Collection of Items, Such as a List or an Array
The below code displays how we can iterate over a collection of items in Kotlin.
fun main() {
val fruits = arrayOf("apple", "banana", "orange", "grape")
for (fruit in fruits) {
println(fruit)
}
}
Defining and Calling a Function
Here, we define a function called addNumbers that takes two parameters num1 and num2, both of which are of type Int. The function returns the sum of num1 and num2. In the main function, we call the addNumbers function with the arguments 5 and 10, and store the result in a variable called result.
fun main() {
val result = addNumbers(5, 10)
println("The sum of the numbers is $result")
}
fun addNumbers(num1: Int, num2: Int): Int {
return num1 + num2
}
Go
Go, also known as Golang, is a statically typed, compiled programming language designed for building efficient, scalable, and concurrent software. It was developed by Google in 2007 and has since gained popularity due to its simplicity, performance, and robust standard library.
Go was developed to address the challenges of building large-scale networked software at Google. It was designed to be easy to learn and use, to have fast compile times, and to support concurrency and communication between processes. Additionally, the syntax of Go is intentionally minimal, which helps to reduce the complexity of the language and make it easier to read and write.
Hello World!
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
If / Then / Else, or Similar
x := 10
if x > 5 {
fmt.Println("x is greater than 5")
} else {
fmt.Println("x is less than or equal to 5")
}
Iteration Over a Collection of Items, Such as a List or an Array
The below Go code defines a slice of integers, then iterates over the slice using a for loop and the range keyword. The loop prints out the index and value of each element in the slice
package main
import "fmt"
func main() {
nums := []int{1, 2, 3, 4, 5}
for i, num := range nums {
fmt.Printf("Index: %d, Value: %d\n", i, num)
}
}
Defining and Calling a Function
The below code defines a function called add that takes two integer arguments and returns their sum. The main function then calls the add function with the arguments 5 and 3, assigns the result to a variable called sum, and then prints the value of sum to the console.
package main
import "fmt"
func add(x int, y int) int {
return x + y
}
func main() {
sum := add(5, 3)
fmt.Println(sum)
}
Rust
Rust is a systems programming language that was initially developed by Mozilla. It aims to provide the low-level control of C or C++ while also providing strong memory safety guarantees. Rust achieves this through a unique ownership model and a modern type system, which allows developers to write efficient and safe code without relying on a garbage collector.
Rust was originally a personal project of its creator, Graydon Hoare, who worked on it during his free time for several years before joining Mozilla, where he continued to work on it full-time. The language is named after a fungus, which is a reference to the Rust community’s focus on building resilient and sustainable systems.
Hello World!
fn main() {
println!("Hello, World!");
}
If / Then / Else, or Similar
let x = 10;
if x > 5 {
println!("x is greater than 5");
} else {
println!("x is less than or equal to 5");
}
Iteration Over a Collection of Items, Such as a List or an Array
In Rust, a vector is a collection type that represents a dynamic array. A vector is similar to an array, but it can grow or shrink dynamically as items are added or removed from it.
We can create a vector by calling the Vec::new() method, which creates an empty vector, or the vec! macro, which creates a vector with initial values as shown below.
let mut my_vector = Vec::new(); // creates an empty vector
let my_vector = vec![1, 2, 3]; // creates a vector with initial values 1, 2, and 3
We can add elements to a vector using the push() method, and remove elements using the pop() method. We can also access individual elements of a vector using indexing, like with an array.
let mut my_vector = Vec::new();
my_vector.push(1);
my_vector.push(2);
my_vector.push(3);
println!("The first element of my_vector is {}", my_vector[0]);
println!("The length of my_vector is {}", my_vector.len());
let removed_element = my_vector.pop();
In the below code, we define a vector of integers called numbers and then iterate over it using a for loop. The loop variable number takes on the value of each element in the vector in turn, and the code prints out each value.
fn main() {
let numbers = vec![1, 2, 3, 4, 5];
for number in &numbers {
println!("{}", number);
}
}
Defining and Calling a Function
Here, we define a function called add_five that takes an integer argument and returns that argument plus 5. The main function then calls add_five with the value 5 and prints out the result, which should be 10. The syntax for defining functions in Rust is similar to that of other C-like languages, with the fn keyword followed by the function name, argument list, return type, and code block
fn main() {
let x = 5;
let y = add_five(x);
println!("{}", y);
}
fn add_five(x: i32) -> i32 {
x + 5
}
Julia
Julia is a high-level, high-performance dynamic programming language designed for numerical and scientific computing, data analysis and visualization. It was developed in 2012 and is increasingly being used in various scientific and technical fields.
Julia was developed with the goal of combining the ease of use of high-level languages like Python and R with the performance of lower-level languages like C and Fortran. Julia’s performance has been found to be on par with C in many cases, making it a popular choice for computationally intensive applications in scientific and technical fields. Additionally, the language features a just-in-time (JIT) compiler that can dynamically generate optimized machine code at runtime, which further enhances its performance.
Hello World!
println("Hello, World!")
If / Then / Else, or Similar
x = 10
if x > 5
println("x is greater than 5")
else
println("x is less than or equal to 5")
end
Iteration Over a Collection of Items, Such as a List or an Array
Nothing too complicated here. We use a for loop to iterate over an array.
arr = [1, 2, 3, 4, 5]
for i in arr
println(i)
end
Defining and Calling a Function
In this example, we define a function called add_numbers that takes two arguments, x and y, and returns their sum. The function is called with arguments 3 and 4, and the result is stored in the variable result, which is then printed.
function add_numbers(x, y)
return x + y
end
result = add_numbers(3, 4)
println(result)
Dart
Dart is a programming language developed by Google, which is designed to build web, mobile, and desktop applications. It is an object-oriented, class-based, garbage-collected language, which is strongly typed but optionally type-inferred. Dart was designed to be familiar, intuitive, and easy to learn for those coming from other programming languages, such as Java or JavaScript. It includes features like async/await, streams, and isolate-based concurrency.
Dart is has a Just-in-Time (JIT) compiler, which provides fast development cycles, allowing developers to make changes to the code and immediately see the results. Dart also has an Ahead-of-Time (AOT) compiler, which produces fast, predictable, and compact code suitable for deployment to mobile devices or the web.
Hello World!
void main() {
print('Hello, World!');
}
If / Then / Else, or Similar
var x = 10;
if (x > 5) {
print("x is greater than 5");
} else {
print("x is less than or equal to 5");
}
Iteration Over a Collection of Items, Such as a List or an Array
The below code is similar to many other programming languages with the exception of the List
void main() {
List<String> fruits = ['apple', 'banana', 'orange'];
for (var fruit in fruits) {
print(fruit);
}
}
Defining and Calling a Function
Nothing terribly complicated here either. This code is quite readable, as we create a function called multiply which takes two integer parameters ‘x’ and ‘y’. The main function of the program is executed when the code is run, which assigns the result of calling the multiply function with arguments 3 and 4 to a variable called result. Then, the program prints the value of the result variable to the console.
void main() {
var result = multiply(3, 4);
print(result);
}
int multiply(int x, int y) {
return x * y;
}
As with many other languages, we can call this function by using its name and passing in our desired arguments.
result = max(10, 20)
puts result
TypeScript
TypeScript is an open-source programming language developed and maintained by Microsoft. It is a superset of JavaScript and adds optional static typing, classes, and interfaces to the language, making it easier to build and maintain large-scale applications.
TypeScript is was created by a Microsoft engineer named Anders Hejlsberg, who also created the popular C# programming language. Hejlsberg saw the potential for static typing in JavaScript and created TypeScript as a way to make it easier to build large-scale web applications.
Hello World!
console.log("Hello, World!");
If / Then / Else, or Similar
let x = 10;
if (x > 5) {
console.log("x is greater than 5");
} else {
console.log("x is less than or equal to 5");
}
Iteration Over a Collection of Items, Such as a List or an Array
This example of iteration is not as easily readable as some of the other examples provided in this series. But, its process is similar in that it uses a ‘for’ loop to iterate through each of the items in the array and then outputs each item to the console.
let fruits: string[] = ["apple", "banana", "orange"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Defining and Calling a Function
This example defines a function called addNumbers that takes two numbers as arguments and returns their sum. It then calls the function with the arguments 5 and 10, and stores the result in a variable called sum. Finally, it logs the value of sum to the console.
function addNumbers(x: number, y: number): number {
return x + y;
}
let sum: number = addNumbers(5, 10);
console.log(sum);
R
R is a programming language and software environment for statistical computing and graphics. It is widely used among statisticians, data scientists, and researchers to develop statistical software and data analysis.
R provides a wide variety of statistical and graphical techniques, including linear and nonlinear modeling, classical statistical tests, time-series analysis, classification, clustering, and others. It has a wide variety of user-created packages for specific functions, such as bioinformatics, machine learning, and more.
R was originally developed by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, in the mid-1990s as an open-source alternative to commercial statistical software. The name “R” was chosen partly as a play on the name of the programming language S, which was an early version of R, and also as a nod to the first names of the two developers.
Hello World!
cat("Hello, World!\n")
If / Then / Else, or Similar
x <- 10
if (x > 5) {
print("x is greater than 5")
} else {
print("x is less than or equal to 5")
}
Iteration Over a Collection of Items, Such as a List or an Array
Similar to Rust, we create a vector in R called my_vector with five elements and then iterate over it using a for loop. The loop runs once for each element in the vector, with the variable item taking on the value of each element in turn. The loop body just prints the value of item.
my_vector <- c(1, 2, 3, 4, 5)
for (item in my_vector) {
print(item)
}
Defining and Calling a Function
Here, we define a function my_function in R that takes two arguments x and y, adds them together, and returns the result. We then call the function with the arguments 3 and 4, and store the result in a variable result. Finally, we print the value of result.
# Define the function
my_function <- function(x, y) {
z <- x + y
return(z)
}
# Call the function
result <- my_function(3, 4)
print(result)
This wraps up this series. If you would like to see an article like this on any programming languages I didn’t cover, please let me know in the comments and I will consider them for a future post.