When starting with a new programming language—whether through tutorials or courses—one of the first tasks we do is usually performing a basic calculation. It’s a simple but effective way to get familiar with the syntax and language rules.
I got curious and decided to see how different languages handle basic calculation. Here, I’ll perform the simple addition operation “5 + 3” in 10 different programming languages, and the goal here is to explore how each language syntax differs in readability. I'll also note their compile and/or runtime and provide a rating out of 10 based on the readability of their syntax as well as list them in descending order.
For consistency, I used the online coding platform JDoodle, so you can easily try out the examples yourself. Please note that the runtime is based on the JDoodle environment and doesn’t necessarily reflect how fast each language would run in a native or optimized environment. It just serves as a way to compare the languages in a controlled setup.
Now that’s all cleared up, let's begin!
1. Python
x=5
y=3
z=x+y
print ("sum of x+y =", z)
Python makes it so easy: assign the values in variables x
and y
, store the added values in variable z
and use the print()
function to display the result. It doesn’t get easier than this. Clean, simple, and easy to read.
Runtime:
0.902 sec(s)
Rating:
9.5
To the surprise of no one, Python is at the top of the list. It’s simplicity and readability make it an ideal choice for beginners; it allows them to focus on learning without being overwhelmed by complex syntax.
Note: Some runtimes show "N/A" because of limitations in JDoodle's runtime environment.
2. JavaScript
let x = 5
let y = 3
let z = x + y
console.log(z);
JavaScript console.log
does the job here. It uses the let
keyword in assigning variables, stores the added value in a variable, and simply prints it out in the browser’s console. It’s simple, easy to read, versatile, and very useful for debugging.
Runtime:
N/A
Rating:
9.0
It's syntax is clean and straightforward, making it a popular choice for both beginners and experienced developers.
3. C
#include<stdio.h>
int main() {
int x= 5;
int y= 3;
int z=x+y;
printf("Sum of x+y = %i", z);
}
C requires a bit more setup compared to Python and JavaScript, but that’s what makes learning low-level languages important. It helps you understand the mechanics of memory management and how operations work at a deeper level.
Runtime:
1.132 sec(s)
Rating:
8.0/10
C provides more control over system resources but requires more boilerplate code, making it less beginner-friendly for simple tasks like addition.
4. PHP
<?php
$x=5;
$y=3;
$z=$x+$y;
$msg = 'Sum of x+y = ';
print($msg.$z);
?>
PHP is a widely used scripting language designed for web development. It’s simple, user-friendly, and readable, especially when dealing with basic operations. While PHP may not be as fast or strict as some compiled languages, it's great for web scripting and server-side applications.
Runtime:
N/A
Rating:
7.5/10
The syntax is simple and straightforward but could be more structured and robust for larger, more complex applications
5. Go
package main
import "fmt"
func main() {
x:= 5
y:= 3
z:= x + y
fmt.Printf("Sum of x + y = %d", z)
}
Go’s syntax is very straightforward and is designed for efficiency and scalability, especially in concurrent programming.
Runtime:
N/A
Rating:
7.0/10
Although the syntax is simple and effective for concurrent and scalable programming, the extra imports and verbosity for output reduce its overall simplicity compared to the previous languages.
6. C++
#include <iostream>
using namespace std;
int main() {
int x=5;
int y=3;
int z=x+y;
cout<<"Sum of x+y = " << z;
}
C++ syntax is relatively clear, but it requires more boilerplate than languages like Python or JavaScript. The need for header files and the use of cout
for output might make the code feel a bit more complex, though it allows for greater control over performance and memory management.
Runtime:
1.052 sec(s)
Rating:
6.8/10
The syntax is straightforward but adds verbosity with its use of header files and manual output handling. While powerful, it may feel complex for beginners looking for quick and simple calculations.
7. C
using System;
class Program
{
static void Main() {
int x = 5;
int y = 3;
int z = x + y;
Console.Write("Sum of x + y = "+ z);
}
}
C# follows a structured approach, but the need for defining a class and using specific methods such as Console.Write
makes it more verbose compared to higher-level languages. The syntax is clear, but it’s less beginner-friendly and involves more boilerplate code for a simple task.
Runtime:
12.075 sec(s)
Rating:
6.5/10
Although the code is easy to read, it introduces more complexity due to the necessity of class structures and method calls. The added boilerplate reduces the efficiency for a simple calculation.
8. Java
public class MyClass {
public static void main(String args[]) {
int x= 5;
int y= 3;
int z=x+y;
System.out.println(z);
}
}
Java requires defining a full class structure, which feels excessive for a simple calculation. Its strict syntax ensures reliability and performance but adds complexity, making it less intuitive for beginners.
Runtime:
1.402 sec(s)
Rating:
6.0/10
The need for a class structure and the strict syntax makes Java feel cumbersome for a simple task like addition, although it’s powerful and efficient once you get familiar.
9. Objective-C
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int x = 5;
int y = 3;
NSLog(@"Sum of x + y = %i", (x+y));
[pool drain];
return 0;
}
Objective-C adds object-oriented features to C, but the syntax is much more verbose, especially for simple tasks like addition. It’s powerful but requires more boilerplate code that isn’t easily readable, making it less beginner-friendly compared to other languages like C++ or even Java.
Runtime:
7.846 sec(s)
Rating:
5.5/10
The verbose syntax and additional boilerplate make it harder to write and understand for simple tasks, though it is still powerful and flexible for complex applications.
10. Assembly
section .text
global _start
_start:
mov eax, [x]
sub eax, '0'
mov ebx, [y]
sub ebx, '0'
add eax, ebx
add eax, '0'
mov [sum], eax
mov ecx, msg
mov edx, len
mov ebx, 1
mov eax, 4
int 0x80
mov ecx, sum
mov edx, 1
mov ebx, 1
mov eax, 4
int 0x80
mov eax, 1
int 0x80
section .data
x db '5'
y db '3'
msg db "sum of x and y is "
len equ $ - msg
segment .bss
sum resb 1
Assembly language is extremely low-level and requires extensive code even for simple operations like addition. Although it offers maximum control over system hardware and performance, the complexity and verbosity make it unsuitable for simple tasks and less beginner-friendly than higher-level languages.
Runtime:
0.876 sec(s)
Rating:
4.0/10
It’s no surprise that this gets last place. It’s too verbose, prone to errors, and definitely not practical for simple tasks.
Reflection
I find it interesting how something as simple as adding two numbers can be expressed in so many different ways. But one thing that’s certain is that although each language has its own unique syntax and rules, they all ultimately achieve the same result. Some languages may have simpler syntax, while others are more complex, but at the end of the day, each of them has their strengths and weaknesses, and they’re all just tools for solving problems.
So which of these languages do you prefer for simple tasks? Feel free to share your thoughts!