I tried 10 programming languages and I liked it
Tags: programming, fizzbuzz
Published: 2019-07-05 15:24:39

---
title: I tried 10 programming languages and I liked it
published: true
description: Compare programming languages with FizzBuzz example
tags: programming, fizzbuzz
---

For the last several years I've been using JavaScript in my day-to-day work. But what about other programming languages? Most of them grow and evolve as well as JS. I thought it'd be interesting to take a look at the structure and syntax of 10 different programming languages.

I deciced to write [FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz) program with every language. It's a simple algorithm, but requires to know how to write loops, conditionals and declare variables. That's should be enough for introduction.

### 1. JavaScript
I'll start with the most familiar one.
[REPL](https://repl.it/@karataev/JS-FizzBuzz)
```js
for (let i = 1; i <= 100; i++) {
  let result = '';
  if (i % 3 === 0 && i % 5 === 0) result = 'FizzBuzz';
  else if (i % 3 === 0) result = 'Fizz';
  else if (i % 5 === 0) result = 'Buzz';
  else result = i;
  console.log(result);
}
```
Short and easy. Feels like a breeze.

### 2. TypeScript
Just add types and you're ready to go.
[REPL](https://repl.it/@karataev/TypeScript-FizzBuzz)
```typescript
let result: string;
for (let i:number = 1; i <= 100; i++) {
  if (i % 3 === 0 && i % 5 === 0) result = 'FizzBuzz';
  else if (i % 3 === 0) result = 'Fizz';
  else if (i % 5 === 0) result = 'Buzz';
  else result = String(i);
  console.log(result);
}
```

### 3. Python
I tried Python couple of times in the past, but I can say that I spent no more that 10 hours programming Python in my whole life. I just remember it's necessary to use indentation instead of curly braces 😂
Here come the first difficulties. How do I create a loop? What is the syntax for logical operators? If/elif. Google, google, google.
[REPL](https://repl.it/@karataev/Python-FizzBuzz)
```python
result = ''
for i in range(1, 101):
  if i % 3 == 0 and i % 5 == 0:
    result = 'FizzBuzz';
  elif i % 3 == 0:
    result = 'Fizz'
  elif i % 5 == 0:
    result = 'Buzz'
  else:
    result = i;
  print(result)
```

### 4. Basic
Time for the nostalgia. QBasic is the language of my childhood. I remember drawing complex pictures with LINE and CIRCLE commands 🤓
And 20 years ago GOTO instructions were used everywhere even [Go To statement considered harmful](https://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf) was written in 1968.
Writing FizzBuzz, I noticed that string variables MUST end with dollar sign, otherwise the compiler will scream at you, wow.
Also it's neccessary to explicitly coerce types. No dynamic type coercion like in JS!
[REPL](https://repl.it/@karataev/QBasic-FizzBuzz)
```
result$ = ""
FOR i = 1 TO 100
IF i MOD 3 = 0 AND i MOD 5 = 0 THEN
  result$ = "FizzBuzz"
ELSE IF i MOD 3 = 0 THEN 
  result$ = "Fizz"
ELSE IF i MOD 5 = 0 THEN 
  result$ = "Buzz"
ELSE 
  result$ = STR$(i)
END IF
PRINT result$
NEXT i
```

### 5. C++
I remember C++ as a language for "real programmers" with pointers, manual memory allocation and all that low-level stuff.
Again explicit coercion from number to string. Also it's necessary to include standard libraries for useful functions.
[REPL](https://repl.it/@karataev/C-FizzBuzz)
```c
#include <iostream>
#include <string>

int main() {
  std::string result = "";
  for (int i = 1; i <= 100; i++) {
    if (i % 3 == 0 && i % 5 == 0) result = "FizzBuzz";
    else if (i % 3 == 0) result = "Fizz";
    else if (i % 5 == 0) result = "Buzz";
    else result = std::to_string(i);
    std::cout << result << std::endl;
  }
  
}
```

### 6. Java
The language of bloody enterprise. It was a surprise for me that Java looks like TypeScript. Learn TypeScript to know two languages for the price of one! 😄
[REPL](https://repl.it/@karataev/Java-FizzBuzz)
```java
class Main {
  public static void main(String[] args) {
    String result = "";
    for (int i = 1; i <= 100; i++) {
      if (i % 3 == 0 && i % 5 == 0) result = "FizzBuzz";
      else if (i % 3 == 0) result = "Fizz";
      else if (i % 5 == 0) result = "Buzz";
      else result = String.valueOf(i);
      System.out.println(result);
    }
  }
}
```

### 7. Rust
I know almost nothing about Rust. It's like C++, used by Mozilla and is handy to write a core of an operating system. Not really handy for frontend development.
[REPL](https://repl.it/@karataev/Rust-FizzBuzz)
```rust
fn main() {
  let mut res = "";
  for i in 1..101 {
    if i % 3 == 0 && i % 5 == 0 {
      res = "FizzBuzz";
    } else if i % 3 == 0 {
      res = "Fizz";
    } else if i % 5 == 0 {
      res = "Buzz"
    } else {
      println!("{}", i);
      continue;
    }
    println!("{}", res);
  }
}
```

### 8. PHP
It looks funny that every variable have to start with $.
[REPL](https://repl.it/@karataev/PHP-FizzBuzz)
```
<?php

for ($i = 1; $i <= 100; $i++) {
  if ($i % 3 == 0 && $i % 5 == 0) $res = "FizzBuzz";
  else if ($i % 3 == 0) $res = "Fizz";
  else if ($i % 5 == 0) $res = "Buzz";
  else $res = $i;
  echo "$res \n";
}
```

### 9. Ruby
Ruby has definitely different syntax for loops than other languages.
[REPL](https://repl.it/@karataev/Ruby-FizzBuzz)
```ruby
res = ""
1.upto 100 do |i|
  res = ""
  if i % 3 == 0 && i % 5 == 0
    res = "FizzBuzz"
  elsif i % 3 == 0
    res = "Fizz"
  elsif i % 5 == 0
    res = "Buzz"
  else
    res = i
  end

  puts res
end
```

### 10. (((Clojure)))
Clojure it's an interesting functional language with parenthesis flying everywhere. Functional programming is another world with different mindset to adopt. Who writes arithmetic expressions like `(+ 1 2)`, right? 
Actually FizzBuzz in Clojure inspired me to learn the language and ecosystem deeper.
[REPL](https://repl.it/@karataev/Clojure-FizzBuzz)
```closure
(defn FizzBuzz [] (
  (loop [x 1]
  (when (<= x 100)
    (if (= 0 (rem x 15)) 
      (println "FizzBuzz")
      (if (= 0 (rem x 3)) 
        (println "Fizz")
        (if (= 0 (rem x 5))
          (println "Buzz")
          (println x)
        )
      )
    )
    (recur (+ x 1))))
))

(FizzBuzz)

```

## Conclusion

That's all! It was an interesting and sometimes frustrating experience to try ten different programming languages. They might look different, but I think all of them have a lot in common - they are just tools to help translate thoughts humans can understand to the instructions computers can understand.