Using the below you can see the basics of 10 different languages. For most of these langauges you should be able to try them out by generating a REPL.
How to print text to the console
One of the first things you need to learn in any language is how to print text in the console. Being able to print text to console allows us to...
- print the value of variables to check that they have the right values
- print the return values of functions so we can make sure they return the right value
- be used just to print text to confirm parts of our code are running
Javascript
console.log("Hello World")
Python
print("Hello World")
Ruby
puts "Hello World"
PHP
<?php
echo "Hello World";
?>
GO
import fmt
func main(){
fmt.Println("Hello World")
}
Rust
fn main(){
print!("Hello World");
}
Dart
void main(){
print("Hello, World!");
}
C Sharp
using System;
namespace HelloWorldApp {
class HelloWorld {
static void Main(string[] args){
Console.WriteLine("Hello World");
}
}
}
Java
class HelloWorld {
public static void main(String[] args){
System.out.println("Hello, World");
}
}
Ballerina
import ballerina/io;
public function main() {
io:println("Hello World");
}
Declaring Variables
Storing data to use is pivotal in programming. Data is generally stored in variables that we declare. These variables can hold data like numbers, strings and booleans (true/false).
Javascript
let number = 5
let str = "Hello"
let bool = true
console.log(number, str, bool)
Python
number = 5
string = "hello"
boolean = True
print(number, string, boolean)
Ruby
num = 5
str = "Hello"
bool = true
puts num, str, bool
PHP
<?php
$num = 5;
$str = "Hello";
$bool = true;
echo $num;
echo $str;
echo $bool;
?>
GO
import fmt
func main(){
num := 5
str := "Hello"
boolean := true
fmt.Println(num, str, boolean)
}
Rust
fn main(){
let num = 5;
let string = "Hello";
let boolean = true;
print!("{0} - {1} - {2}", num, string, boolean );
}
Dart
void main (){
var number = 5;
var string = "hello";
var boolean = true;
print(number, string, boolean);
}
C Sharp
using System;
namespace MyProgramApp {
class MyProgram {
static void Main(string[] args){
int num = 5;
string str = "Hello";
bool boolean = true;
Console.WriteLine(num);
Console.WriteLine(str);
Console.WriteLine(boolean);
}
}
}
Java
class Main {
public static void main(String[] args){
int num = 5;
String str = "Hello";
boolean bool = true;
System.out.println(num);
System.out.println(str);
System.out.println(bool);
}
}
Ballerina
import ballerina/io;
public function main(){
int num = 5;
string str = "Hello";
boolean bool = true;
io:println(num);
io:println(str);
io:println(bool);
}
Collection arrays and key/value pairs
Usually you have two main collections you'll use most of the time.
Arrays/Lists that will be used to store data in an order that is referenced by a zero based index
A key/value pair structure by which you can reference different values based on a key.
Javascript
const myArray = [1,2,3,4,5]
const myObject = {name: "Alex Merced", age: 35}
console.log(myArray)
console.log(myObject)
Python
my_list = [1,2,3,4,5]
my_dictionary = {"name": "Alex Merced, "age": 35}
print(my_list)
print(my_dictionary)
Ruby
my_array = [1,2,3,4,5]
my_hash = {name: "Alex Merced", age: 35}
puts my_array
puts my_hash
PHP
<?php
$my_array = [1,2,3,4,5];
$my_associative_array = ["name" => "Alex Merced", "age" => 35];
var_dump($my_array);
var_dump($my_associative_array);
?>
GO
import fmt
func main(){
my_slice := []int{1,2,3,4,5}
my_map := map[string]string{"name":"alex merced", "age":"35"}
fmt.Println(my_slice)
fmt.Println(my_map)
}
Rust
use std::collections::HashMap;
fn main(){
let my_array = [1,2,3,4,5];
let mut my_hashmap = HashMap::new();
my_hashmap.insert("name", "Alex Merced");
my_hashmap.insert("age", "35");
println!("{:?}", my_array);
println!("{:?}", my_hashmap);
}
Dart
void main (){
final my_list = [1,2,3,4,5];
final my_map = {"name": "Alex Merced", "age":"35"}
print(my_list);
print(my_map);
}
C Sharp
using System;
using System.Collections;
namespace MyProgramApp {
class MyProgram {
static void Main(string[] args){
int[] my_array = {1,2,3,4,5};
Hashtable my_ht = new Hashtable();
my_ht.Add("name", "Alex Merced");
my_ht.Add("age", "35");
Console.WriteLine(my_array);
Console.WriteLine(my_ht);
}
}
}
Java
import java.util.*;
class MyProgram {
public static void main(String[] args){
int[] my_array = {1,2,3,4,5};
Hashtable my_ht = new Hashtable();
my_ht.put("name", "Alex Merced");
my_ht.put("age", "35");
System.out.println(my_array);
System.out.println(my_ht);
}
}
Ballerina
import ballerin/io;
public function main(){
int[] my_array = [1, 2, 3, 4, 5];
map<string> my_map = {
"name": "Alex Merced",
"age" : "35"
};
io:println(my_array);
io:println(my_map);
}
Defining Function
Functions allow you define blocks of code you can run on-demand. Pivotal to any programming language! There are two steps:
declare/define the function
call/invoke the function
Javascript
function helloWorld(){
console.log("Hello World")
}
helloWorld()
Python
def hello_world():
print("Hello World")
hello_world()
Ruby
def hello_world
puts "Hello World"
end
hello_world
PHP
<?php
function helloWorld(){
echo "Hello World";
}
helloWorld();
?>
GO
import fmt
func helloWorld(){
fmt.Println("hello world")
}
func main(){
helloWorld()
}
Rust
fn hello_world(){
println!("Hello World");
}
fn main(){
hello_world();
}
Dart
void hello_world(){
print("Hello World");
}
void main(){
hello_world()
}
C Sharp
using System;
namespace MyProgramApp {
class MyProgram {
static void Main(string[] args){
HelloWorld();
}
static void HelloWorld(){
Console.WriteLine("Hello World");
}
}
}
Java
class MyProgram {
public static void main(String[] args){
hello_world();
}
public static void hello_world(){
System.out.println("Hello, World");
}
}
Ballerina
import ballerina/io;
function hello_world(){
io:println("Hello World");
}
public function main(){
hello_world();
}