Syntax compare

The same task in seven languages. Pick two and see how they are alike and how they differ.

Two languages side by side

Hello world

The simplest program: print one line.

Python
print("Hello, World!")
JavaScript
console.log("Hello, World!");
Go
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
Java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
C
#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}
C++
#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
PHP
<?php
echo "Hello, World!\n";

Output: Hello, World!

Variables and types

Python and JS infer types at runtime; Go, Java, C and C++ are statically typed (written explicitly or inferred by the compiler).

Python
n = 42
name = "Ola"
print(n)
print(name)
JavaScript
const n = 42;
const name = "Ola";
console.log(n);
console.log(name);
Go
package main

import "fmt"

func main() {
    n := 42
    name := "Ola"
    fmt.Println(n)
    fmt.Println(name)
}
Java
public class Main {
    public static void main(String[] args) {
        int n = 42;
        String name = "Ola";
        System.out.println(n);
        System.out.println(name);
    }
}
C
#include <stdio.h>

int main(void) {
    int n = 42;
    const char *name = "Ola";
    printf("%d\n", n);
    printf("%s\n", name);
    return 0;
}
C++
#include <iostream>
#include <string>

int main() {
    int n = 42;
    std::string name = "Ola";
    std::cout << n << "\n";
    std::cout << name << "\n";
    return 0;
}
PHP
<?php
$n = 42;
$name = "Ola";
echo $n . "\n";
echo $name . "\n";

Output: 42 ⏎ Ola

if / else

Python marks blocks with indentation, the others use braces; PHP variables start with $.

Python
x = 7
if x % 2 == 0:
    print("even")
else:
    print("odd")
JavaScript
const x = 7;
if (x % 2 === 0) {
  console.log("even");
} else {
  console.log("odd");
}
Go
package main

import "fmt"

func main() {
    x := 7
    if x%2 == 0 {
        fmt.Println("even")
    } else {
        fmt.Println("odd")
    }
}
Java
public class Main {
    public static void main(String[] args) {
        int x = 7;
        if (x % 2 == 0) {
            System.out.println("even");
        } else {
            System.out.println("odd");
        }
    }
}
C
#include <stdio.h>

int main(void) {
    int x = 7;
    if (x % 2 == 0) {
        printf("even\n");
    } else {
        printf("odd\n");
    }
    return 0;
}
C++
#include <iostream>

int main() {
    int x = 7;
    if (x % 2 == 0) {
        std::cout << "even\n";
    } else {
        std::cout << "odd\n";
    }
    return 0;
}
PHP
<?php
$x = 7;
if ($x % 2 == 0) {
    echo "even\n";
} else {
    echo "odd\n";
}

Output: odd

for loop from 1 to 5

Python uses range(); Go and the C-family use a classic counter loop (Go has only the for keyword).

Python
for i in range(1, 6):
    print(i)
JavaScript
for (let i = 1; i <= 5; i++) {
  console.log(i);
}
Go
package main

import "fmt"

func main() {
    for i := 1; i <= 5; i++ {
        fmt.Println(i)
    }
}
Java
public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.println(i);
        }
    }
}
C
#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 5; i++) {
        printf("%d\n", i);
    }
    return 0;
}
C++
#include <iostream>

int main() {
    for (int i = 1; i <= 5; i++) {
        std::cout << i << "\n";
    }
    return 0;
}
PHP
<?php
for ($i = 1; $i <= 5; $i++) {
    echo $i . "\n";
}

Output: 1 ⏎ 2 ⏎ 3 ⏎ 4 ⏎ 5

while loop: count down 3..1

The condition is checked before each step; Python has no ++, so it uses -= 1.

Python
n = 3
while n > 0:
    print(n)
    n -= 1
JavaScript
let n = 3;
while (n > 0) {
  console.log(n);
  n--;
}
Go
package main

import "fmt"

func main() {
    n := 3
    for n > 0 {
        fmt.Println(n)
        n--
    }
}
Java
public class Main {
    public static void main(String[] args) {
        int n = 3;
        while (n > 0) {
            System.out.println(n);
            n--;
        }
    }
}
C
#include <stdio.h>

int main(void) {
    int n = 3;
    while (n > 0) {
        printf("%d\n", n);
        n--;
    }
    return 0;
}
C++
#include <iostream>

int main() {
    int n = 3;
    while (n > 0) {
        std::cout << n << "\n";
        n--;
    }
    return 0;
}
PHP
<?php
$n = 3;
while ($n > 0) {
    echo $n . "\n";
    $n--;
}

Output: 3 ⏎ 2 ⏎ 1

Function with a return value

Go, Java, C and C++ declare parameter and return types; in Python, JS and PHP they are optional.

Python
def add(a, b):
    return a + b

print(add(2, 3))
JavaScript
function add(a, b) {
  return a + b;
}

console.log(add(2, 3));
Go
package main

import "fmt"

func add(a int, b int) int {
    return a + b
}

func main() {
    fmt.Println(add(2, 3))
}
Java
public class Main {
    static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(2, 3));
    }
}
C
#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int main(void) {
    printf("%d\n", add(2, 3));
    return 0;
}
C++
#include <iostream>

int add(int a, int b) {
    return a + b;
}

int main() {
    std::cout << add(2, 3) << "\n";
    return 0;
}
PHP
<?php
function add($a, $b) {
    return $a + $b;
}

echo add(2, 3) . "\n";

Output: 5

Array of three numbers and its sum

Go, Java, C and C++ sum with a loop; Python, JS (reduce) and PHP have a built-in way.

Python
nums = [1, 2, 3]
print(sum(nums))
JavaScript
const nums = [1, 2, 3];
console.log(nums.reduce((a, b) => a + b, 0));
Go
package main

import "fmt"

func main() {
    nums := []int{1, 2, 3}
    total := 0
    for _, v := range nums {
        total += v
    }
    fmt.Println(total)
}
Java
public class Main {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        int total = 0;
        for (int v : nums) {
            total += v;
        }
        System.out.println(total);
    }
}
C
#include <stdio.h>

int main(void) {
    int nums[3] = {1, 2, 3};
    int total = 0;
    for (int i = 0; i < 3; i++) {
        total += nums[i];
    }
    printf("%d\n", total);
    return 0;
}
C++
#include <iostream>
#include <numeric>
#include <vector>

int main() {
    std::vector<int> nums = {1, 2, 3};
    std::cout << std::accumulate(nums.begin(), nums.end(), 0) << "\n";
    return 0;
}
PHP
<?php
$nums = [1, 2, 3];
echo array_sum($nums) . "\n";

Output: 6

Iterate over array items

Most languages loop over items directly; C loops over indexes.

Python
for x in [10, 20, 30]:
    print(x)
JavaScript
for (const x of [10, 20, 30]) {
  console.log(x);
}
Go
package main

import "fmt"

func main() {
    for _, x := range []int{10, 20, 30} {
        fmt.Println(x)
    }
}
Java
public class Main {
    public static void main(String[] args) {
        int[] items = {10, 20, 30};
        for (int x : items) {
            System.out.println(x);
        }
    }
}
C
#include <stdio.h>

int main(void) {
    int items[] = {10, 20, 30};
    for (int i = 0; i < 3; i++) {
        printf("%d\n", items[i]);
    }
    return 0;
}
C++
#include <iostream>
#include <vector>

int main() {
    std::vector<int> items = {10, 20, 30};
    for (int x : items) {
        std::cout << x << "\n";
    }
    return 0;
}
PHP
<?php
foreach ([10, 20, 30] as $x) {
    echo $x . "\n";
}

Output: 10 ⏎ 20 ⏎ 30

String length and uppercase

C and C++ uppercase character by character; the other languages have a ready method or function.

Python
s = "Iteris"
print(len(s))
print(s.upper())
JavaScript
const s = "Iteris";
console.log(s.length);
console.log(s.toUpperCase());
Go
package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "Iteris"
    fmt.Println(len(s))
    fmt.Println(strings.ToUpper(s))
}
Java
public class Main {
    public static void main(String[] args) {
        String s = "Iteris";
        System.out.println(s.length());
        System.out.println(s.toUpperCase());
    }
}
C
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void) {
    char s[] = "Iteris";
    printf("%zu\n", strlen(s));
    for (int i = 0; s[i] != '\0'; i++) {
        s[i] = toupper((unsigned char)s[i]);
    }
    printf("%s\n", s);
    return 0;
}
C++
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

int main() {
    std::string s = "Iteris";
    std::cout << s.size() << "\n";
    std::transform(s.begin(), s.end(), s.begin(), ::toupper);
    std::cout << s << "\n";
    return 0;
}
PHP
<?php
$s = "Iteris";
echo strlen($s) . "\n";
echo strtoupper($s) . "\n";

Output: 6 ⏎ ITERIS

Dictionary / map with one key

C has no built-in dictionary: this is the closest honest equivalent, searching an array of key/value pairs with strcmp.

Python
d = {"city": "Kyiv"}
print(d["city"])
JavaScript
const d = { city: "Kyiv" };
console.log(d["city"]);
Go
package main

import "fmt"

func main() {
    d := map[string]string{"city": "Kyiv"}
    fmt.Println(d["city"])
}
Java
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, String> d = new HashMap<>();
        d.put("city", "Kyiv");
        System.out.println(d.get("city"));
    }
}
C
#include <stdio.h>
#include <string.h>

struct Pair {
    const char *key;
    const char *value;
};

int main(void) {
    struct Pair d[] = {{"city", "Kyiv"}};
    for (int i = 0; i < 1; i++) {
        if (strcmp(d[i].key, "city") == 0) {
            printf("%s\n", d[i].value);
        }
    }
    return 0;
}
C++
#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<std::string, std::string> d = {{"city", "Kyiv"}};
    std::cout << d["city"] << "\n";
    return 0;
}
PHP
<?php
$d = ["city" => "Kyiv"];
echo $d["city"] . "\n";

Output: Kyiv

Struct / class with a field and a method

C has no methods: a function takes the struct as an argument; Go attaches methods to structs without classes.

Python
class Person:
    def __init__(self, name):
        self.name = name

    def greet(self):
        return "Hello, " + self.name

print(Person("Ola").greet())
JavaScript
class Person {
  constructor(name) {
    this.name = name;
  }

  greet() {
    return "Hello, " + this.name;
  }
}

console.log(new Person("Ola").greet());
Go
package main

import "fmt"

type Person struct {
    Name string
}

func (p Person) Greet() string {
    return "Hello, " + p.Name
}

func main() {
    p := Person{Name: "Ola"}
    fmt.Println(p.Greet())
}
Java
class Person {
    String name;

    Person(String name) {
        this.name = name;
    }

    String greet() {
        return "Hello, " + name;
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Person("Ola").greet());
    }
}
C
#include <stdio.h>

struct Person {
    const char *name;
};

void greet(struct Person p) {
    printf("Hello, %s\n", p.name);
}

int main(void) {
    struct Person p = {"Ola"};
    greet(p);
    return 0;
}
C++
#include <iostream>
#include <string>

struct Person {
    std::string name;

    std::string greet() const {
        return "Hello, " + name;
    }
};

int main() {
    Person p{"Ola"};
    std::cout << p.greet() << "\n";
    return 0;
}
PHP
<?php
class Person {
    public function __construct(public string $name) {}

    public function greet(): string {
        return "Hello, " . $this->name;
    }
}

echo (new Person("Ola"))->greet() . "\n";

Output: Hello, Ola

try / catch: division by zero

Go and C have no try/catch (Go returns an error, C checks a condition); in JS and C++ division by zero does not throw, so the exception is thrown by hand.

Python
try:
    print(1 / 0)
except ZeroDivisionError:
    print("error")
JavaScript
try {
  const b = 0;
  if (b === 0) throw new Error("division by zero");
  console.log(1 / b);
} catch (e) {
  console.log("error");
}
Go
package main

import (
    "errors"
    "fmt"
)

func div(a, b int) (int, error) {
    if b == 0 {
        return 0, errors.New("division by zero")
    }
    return a / b, nil
}

func main() {
    if _, err := div(1, 0); err != nil {
        fmt.Println("error")
    }
}
Java
public class Main {
    public static void main(String[] args) {
        try {
            int b = 0;
            System.out.println(1 / b);
        } catch (ArithmeticException e) {
            System.out.println("error");
        }
    }
}
C
#include <stdio.h>

int main(void) {
    int b = 0;
    if (b == 0) {
        printf("error\n");
    } else {
        printf("%d\n", 1 / b);
    }
    return 0;
}
C++
#include <iostream>
#include <stdexcept>

int main() {
    try {
        int b = 0;
        if (b == 0) throw std::runtime_error("division by zero");
        std::cout << 1 / b << "\n";
    } catch (const std::exception &e) {
        std::cout << "error\n";
    }
    return 0;
}
PHP
<?php
try {
    echo intdiv(1, 0);
} catch (DivisionByZeroError $e) {
    echo "error\n";
}

Output: error

More in the Lab

Learn a language end to end

Lessons, tasks and a reference for each of these languages.