Hello World
#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
Compile and run:
g++ main.cpp -o main
./main
This is the smallest C++ program shape: main starts the program, and std::cout prints to the terminal.
Variables
#include <string>
std::string name = "Dan";
int count = 1;
bool active = true;
auto language = "C++";
This shows a few common C++ value types and auto for type inference when the initializer already makes the type obvious.
Functions
#include <string>
std::string greet(const std::string& name)
{
return "Hello, " + name + "!";
}
This defines a function that takes a name and returns a greeting string. It is the usual pattern for reusable logic in C++.
Hello World
Console.WriteLine("Hello, world!");
Run the project:
dotnet run
This prints a line to standard output. In a small C# app, Console.WriteLine is the usual starting point.
Variables
string name = "Dan";
int count = 1;
bool active = true;
var language = "C#";
This shows common C# types plus var, which lets the compiler infer the type from the value on the right.
Methods
static string Greet(string name)
{
return $"Hello, {name}!";
}
This is a basic C# method: it accepts an argument, builds a string, and returns the result.
Hello World
IO.puts("Hello, world!")
Run the script:
elixir main.exs
This prints a line in Elixir using the IO module. It is the normal first example for a script.
Variables
Values are immutable. Rebinding creates a new binding for the name.
name = "Dan"
count = 1
active = true
These bindings hold values for the current expression flow. In Elixir, you work with new bindings instead of mutating old values.
Pattern Matching
{:ok, name} = {:ok, "Dan"}
[first | rest] = [1, 2, 3]
This pulls values out of a tuple and a list by matching the shape on the left to the data on the right.
Functions
defmodule Greeter do
def greet(name) do
"Hello, #{name}!"
end
end
Greeter.greet("world")
This defines a small module with one function and then calls it. That is the standard way to group behavior in Elixir.
Hello World
-module(main).
-export([hello/0]).
hello() ->
io:format("Hello, world!~n").
Compile and run in the Erlang shell:
c(main).
main:hello().
This defines a tiny Erlang module and a function that prints one line to the shell.
Variables
Variables are single-assignment and start with an uppercase letter.
Name = "Dan",
Count = 1,
Active = true.
These are single-assignment bindings. Once a variable has a value in Erlang, you do not mutate it in place.
Pattern Matching
greet({user, Name}) ->
io:format("Hello, ~s!~n", [Name]).
This matches a tuple argument and pulls the name out directly in the function head.
Hello World
A sample go program is show here.
package main
import "fmt"
func main() {
message := greetMe("world")
fmt.Println(message)
}
func greetMe(name string) string {
return "Hello, " + name + "!"
}
Run the program as below:
$ go run hello.go
This is the normal shape of a tiny Go program: main runs first, and fmt.Println writes output.
Variables
Normal Declaration:
var msg string
msg = "Hello"
Shortcut:
msg := "Hello"
This shows both var and the short := form. The short form is the one you will use most inside functions.
Constants
const Phi = 1.618
Use constants for values that should not change, such as fixed ratios, limits, or labels.
Hello World
main :: IO ()
main = putStrLn "Hello, world!"
Run the file:
runhaskell Main.hs
This prints one line from main, which is the usual entry point for a small Haskell program.
Values
Values are immutable. Type annotations are optional, but useful for clarity.
name :: String
name = "Dan"
count :: Int
count = 1
active :: Bool
active = True
These are immutable bindings. You describe what each value is, not how to update it later.
Functions
greet :: String -> String
greet name = "Hello, " ++ name ++ "!"
double :: Int -> Int
double x = x * 2
This shows two simple pure functions. They take input and return output without changing outside state.
Hello World
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Compile and run:
javac Main.java
java Main
This is the standard Java entry point: the main method runs first, and System.out.println prints to the console.
Variables
String name = "Dan";
int count = 1;
boolean active = true;
These are a few common Java types. Java keeps types explicit, so the declaration tells you exactly what each value holds.
Methods
static String greet(String name) {
return "Hello, " + name + "!";
}
This is a small static method that takes one input and returns one computed string.
Hello World
(format t "Hello, world!~%")
Run with a Common Lisp implementation such as SBCL:
sbcl --script main.lisp
This prints a line in Common Lisp using format, which is a flexible function for building and writing text.
Variables
(defparameter *name* "Dan")
(defparameter *count* 1)
(defparameter *active* t)
These global parameters hold simple values you can reuse across expressions.
Functions
(defun greet (name)
(format nil "Hello, ~A!" name))
(greet "world")
This defines a function and then calls it. Lisp keeps the function shape compact, even for small reusable helpers.
Hello World
<?php
echo "Hello, world!" . PHP_EOL;
Run the script:
php main.php
This writes one line from a PHP script. PHP_EOL keeps the newline portable across environments.
Variables
Variables start with $. Use strict comparisons when the type matters.
$name = "Dan";
$count = 1;
$active = true;
if ($count === 1) {
echo $name;
}
This shows normal PHP variables and a strict comparison, which is the safer choice when type differences matter.
Arrays
PHP arrays can be lists or key-value maps.
$names = ["Ana", "Dan", "Mara"];
$user = [
"id" => 1,
"email" => "[email protected]",
"active" => true,
];
echo $names[0];
echo $user["email"];
PHP arrays can work like lists or maps, so the same structure covers both simple sequences and keyed data.
Conditionals
if ($user["active"]) {
echo "active";
} elseif ($user["id"] === null) {
echo "missing";
} else {
echo "inactive";
}
This is the normal if / elseif / else flow for branching based on the current data.
Loops
Use foreach for arrays. It works with both lists and associative arrays.
foreach ($names as $name) {
echo $name . PHP_EOL;
}
foreach ($user as $key => $value) {
echo $key . ": " . var_export($value, true) . PHP_EOL;
}
These examples show foreach, which is the most practical way to iterate PHP arrays.
Functions
Type parameters and return values where practical.
function greet(string $name): string
{
return "Hello, {$name}!";
}
echo greet("world");
This defines a typed function with a string input and string output, which makes intent clearer and catches mistakes earlier.
Classes
Constructor property promotion keeps simple data objects compact.
class User
{
public function __construct(
public int $id,
public string $email,
public bool $active = true,
) {
}
}
$user = new User(1, "[email protected]");
if ($user->active) {
echo $user->email;
}
This shows a compact data-focused class. Constructor property promotion keeps the class short when it mostly stores values.
Hello World
print("Hello, world!")
Run the script:
python main.py
This prints a line in Python with the built-in print function. It is the normal first script example.
Variables
Python variables are dynamically typed and can be annotated when the expected type matters.
name = "Dan"
age: int = 30
is_active = True
Python lets you bind values without declaring a type first. Add annotations when you want the expected shape to be clearer.
Functions
def greet(name: str) -> str:
return f"Hello, {name}!"
message = greet("world")
print(message)
This defines a function with type hints, calls it, and prints the returned value.