Programming Fundamentals

The concepts shared by every language — values and types, variables, control flow, functions, scope, collections, errors and the main paradigms.

Beginner 3 min readLesson 7 of 8

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 reassigned
Static typingDynamic typing
Types checkedAt compile timeAt runtime
ExamplesJava, C#, Go, Rust, TypeScriptPython, JavaScript, Ruby
BenefitsErrors caught early, better tooling, faster codeLess 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");
}

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();

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];
}

Collections

NeedJavaPythonTypeScript
Ordered listArrayListlistArray
Key → valueHashMapdictMap / object
Unique itemsHashSetsetSet
Queue / dequeArrayDequecollections.dequeArray (or a custom class)
Priority queuePriorityQueueheapq(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)
}

Paradigms

ParadigmCore ideaWhere you see it
Imperative / proceduralStep-by-step statements that change stateC, scripts, most code
Object-orientedObjects bundle data and behaviour; encapsulation, inheritance, polymorphismJava, C#, Python, TS
FunctionalPure functions, immutable data, functions as valuesHaskell, Elixir; map/filter/reduce everywhere
DeclarativeDescribe what you want, not howSQL, HTML, React, regex

Modern languages mix paradigms. A deeper look at OOP: OOP fundamentals.

Key takeaways
  • 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.