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 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
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
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
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 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
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
#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
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
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
<?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
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
(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.
Top comments (11)
You missed the language of the dragons - assembly language. ;)
Hehe, I think that in this case the post'd take a couple of months longer to complete π
This is really very common fallacy. Actually assembly language programming is much easier for programming than is commonly considered.
I am working on several big projects in asm and it takes only a little bit more time than in conventional languages. But the results are really much better. With less bugs, less memory usage and higher performance.
Are you serious?
Programs written with Assembler are long as hell. And AFAIK there are goto (jump) instructions used everywhere, which makes a program hard to follow.
I can understand that's it's sometimes necessary to write assembler code to fix performance bottlenecks or with a very limited resources (RAM).
But it's hard for me to believe that nowdays regular projects are written with assembler when there are so many high-level programming languages available with handy abstractions.
Can you please share what projects are you working on?
Well, the contemporary assembly language is a little bit different. With good IDE and decent libraries, the programming in assembly language is almost as productive as with the high level languages, but still keeping all the advantages of the low level programming.
Yes, it has less abstraction levels, but IMO it is actually an advantage that helps to solve the problems in the most simple way.
Anyway, here are several of my free assembly language projects. Some of them are huge and actually contain several sub-projects:
Fresh IDE - advanced assembly language IDE. (the source ) It contains as a sub-project the library FreshLib that is for writing portable applications. It contains OOP macro library as well, used mainly in the GUI widget toolkit of the library.
BTW, the web site from the above links is served by assembly written web server (not my project), the ultra-fast RWASA and is managed by my CMS, MiniMagAsm written in assembly language.
Another my web project is the web forum engine AsmBB. The source code is here.
You can browse the sources from the links above and see, that they are not so unreadable and hard to understand and maintain as you think.
Thanks, this is really insightful. My perception of assembler shifted from "used for close-to-hardware tasks only" to "might be useful in different domains, even for the web".
Also thanks for your one hour video of assembly programming. It gives the picture of current development process with asm: IDE, code highlighting, syntax, structure, e.t.c.
This post loads slow as hell!
Yep, maybe it was not a good idea to inject repl.it sandboxes inside the post, but provide links for those who interested instead.
I just wanted to make it possible to edit and run the code right in the post, without leaving DEV.
Thanks for your feedback!
Edit: I replaced repl.it sandboxes with code snippets because of loading performance issue.
well when a person knows a lot of programming languages. I'm still learning python.
I know JavaScript only. Most of other languages in this post are new to me. I googled every syntax construction in every language in order to complete this post π
Go next time π