Programming Fundamentals
The concepts shared by every language — values and types, variables, control flow, functions, scope, collections, errors and the main paradigms.
Languages differ in syntax, but almost all are built from the same handful of ideas. Learn them once and picking up a new language becomes mostly a matter of looking up the syntax. Examples here are in Java, Python and TypeScript; you can switch the tabs and the choice is remembered across the site.
Values, types and variables
A value is a piece of data; its type says what it is and what you can do with it. A variable is a name bound to a value.
int count = 3; // integer
double price = 9.99; // floating point
boolean done = false;
char grade = 'A';
String name = "Ada"; // object type
var items = new ArrayList<String>(); // type inferred
final int MAX = 10; // can't be reassignedcount = 3 # int
price = 9.99 # float
done = False # bool
name = "Ada" # str
items: list[str] = [] # optional type hint
MAX = 10 # constant by convention onlylet count = 3; // number
const price = 9.99; // const: can't be reassigned
let done: boolean = false;
const name = "Ada"; // string
const items: string[] = [];| Static typing | Dynamic typing | |
|---|---|---|
| Types checked | At compile time | At runtime |
| Examples | Java, C#, Go, Rust, TypeScript | Python, JavaScript, Ruby |
| Benefits | Errors caught early, better tooling, faster code | Less ceremony, quick to prototype |
Separately, strong vs weak typing describes how much implicit conversion happens: Python refuses "1" + 1; JavaScript makes it "11".
Control flow
if (score >= 90) grade = 'A';
else if (score >= 80) grade = 'B';
else grade = 'C';
for (int i = 0; i < 5; i++) { ... }
for (String item : items) { ... }
while (queue.size() > 0) { ... }
switch (day) {
case SATURDAY, SUNDAY -> System.out.println("weekend");
default -> System.out.println("weekday");
}if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"
for i in range(5): ...
for item in items: ...
while queue: ...
match day:
case "sat" | "sun":
print("weekend")
case _:
print("weekday")if (score >= 90) grade = "A";
else if (score >= 80) grade = "B";
else grade = "C";
for (let i = 0; i < 5; i++) { /* ... */ }
for (const item of items) { /* ... */ }
while (queue.length > 0) { /* ... */ }
switch (day) {
case "sat": case "sun": console.log("weekend"); break;
default: console.log("weekday");
}Functions
A function packages a computation with a name, inputs (parameters) and an output (return value). Good functions do one thing, have descriptive names, and avoid hidden side effects.
static int add(int a, int b) { return a + b; }
// Lambdas: functions as values
Function<Integer, Integer> twice = x -> x * 2;
List<Integer> doubled = nums.stream().map(twice).toList();def add(a: int, b: int = 0) -> int: # default parameter
return a + b
twice = lambda x: x * 2
doubled = [twice(x) for x in nums]function add(a: number, b = 0): number {
return a + b;
}
const twice = (x: number) => x * 2;
const doubled = nums.map(twice);Recursion is a function calling itself on a smaller input until it hits a base case. It's natural for trees and divide-and-conquer. More: Recursion.
Scope and closures
Scope is where a name is visible. Most languages use lexical scope: what's visible depends on where code is written. A closure is a function that remembers variables from the scope it was created in.
Supplier<Integer> makeCounter() {
int[] count = {0}; // captured variables must be effectively final
return () -> ++count[0];
}def make_counter():
count = 0
def inc():
nonlocal count
count += 1
return count
return inc
c = make_counter()
c(); c() # 2function makeCounter() {
let count = 0;
return () => ++count; // closure over count
}
const c = makeCounter();
c(); c(); // 2Collections
| Need | Java | Python | TypeScript |
|---|---|---|---|
| Ordered list | ArrayList | list | Array |
| Key → value | HashMap | dict | Map / object |
| Unique items | HashSet | set | Set |
| Queue / deque | ArrayDeque | collections.deque | Array (or a custom class) |
| Priority queue | PriorityQueue | heapq | (custom heap) |
How they work inside, and their costs: Data structures.
Errors and exceptions
Programs must handle things going wrong: missing files, bad input, network failures.
try {
int n = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.err.println("Not a number: " + input);
} finally {
// always runs (cleanup)
}try:
n = int(text)
except ValueError:
print(f"Not a number: {text}")
else:
print("parsed", n) # runs if no exception
finally:
pass # always runstry {
const data = JSON.parse(text);
} catch (e) {
console.error("Invalid JSON", e);
} finally {
// always runs
}Paradigms
| Paradigm | Core idea | Where you see it |
|---|---|---|
| Imperative / procedural | Step-by-step statements that change state | C, scripts, most code |
| Object-oriented | Objects bundle data and behaviour; encapsulation, inheritance, polymorphism | Java, C#, Python, TS |
| Functional | Pure functions, immutable data, functions as values | Haskell, Elixir; map/filter/reduce everywhere |
| Declarative | Describe what you want, not how | SQL, HTML, React, regex |
Modern languages mix paradigms. A deeper look at OOP: OOP fundamentals.
- Every language has values, types, variables, control flow, functions and collections.
- Static typing catches errors at compile time; dynamic typing defers checks to runtime.
- Closures capture variables from their defining scope.
- Handle errors where you can act on them; don't swallow them.
- Paradigms (imperative, OO, functional, declarative) are styles that most languages mix.