The Building Blocks
Every program — no matter how complex — is built from a small set of primitives. Master these and you can read and write code in any language.
1. Variables
A variable is a named container for a value. The name stays the same; the value can change.
Types you'll use constantly:
| Type | Examples | Used for |
|---|---|---|
| Integer | 0, -3, 1000 | Counts, indices |
| Float | 3.14, -0.5 | Decimals, measurements |
| String | "hello", "42" | Text |
| Boolean | true / false | Flags, conditions |
2. Conditionals
Conditionals let the program make decisions.
The program picks exactly one branch to execute, based on which condition is true first.
3. Loops
Loops repeat a block of code.
For loop — iterate a known number of times or over a collection:
While loop — repeat as long as a condition holds:
4. Functions
A function is a reusable named block of code. Give it a name, define what inputs it takes (parameters), and what it produces (return value).
Functions keep code DRY (Don't Repeat Yourself). Instead of writing the same logic three times in three places, write it once as a function.
5. Arrays / Lists
An array stores an ordered collection of values. Access any element by its index (0-based).
| Operation | Time |
|---|---|
| Read by index | O(1) |
| Append to end | O(1) amortized |
| Insert at front | O(N) (everything shifts) |
| Search | O(N) |
Code Examples in All Languages
6. Putting It Together — FizzBuzz
The classic "can you code?" problem. Print numbers 1–20; for multiples of 3 print "Fizz", multiples of 5 print "Buzz", both print "FizzBuzz".
This one program uses all five concepts: variables (i), conditionals (if/elif/else), a loop (for), the modulo operator as a function-like tool, and implicitly demonstrates the value of readable code.
Key Takeaway
Every algorithm you'll study — sorting, searching, trees, graphs — is composed of exactly these primitives: variables hold state, conditionals branch logic, loops repeat work, functions encapsulate behaviour, and arrays store collections. Learning algorithms is learning how to combine these pieces cleverly.