Chapter 02 · Week 2

Memory — Variables and Types

"For now we see in a mirror dimly, but then face to face." — 1 Corinthians 13:12
2

Play with the trap before you read about it

The biggest source of bugs in this chapter is one weird behavior of C++. The widget below lets you feel it in your hands first. Then come back up.

The Integer Division Trap

Pick two numbers and their types. Watch what C++ actually returns — versus what your math-class instincts expect. The bug that catches everyone.

÷
Math-class expectation
3.5
7 ÷ 2 in your head
What C++ actually returns
3
Both operands are int → integer division → fractional part discarded.
The C++ line you'd write:
int result = 7 / 2;
// result is 3
⚠ The trap is firing. The .5 is gone.
Promote one operand to double using a cast and the math comes back:
double result = static_cast<double>(a) / b;
// result is 3.5 ✓
Try it: Set both types to int and use 105 ÷ 10. You might expect 10.5. You'll get 10. Even when you assign the result to a double, the division has already happened by the time the assignment runs.

Why This Matters

Here's the move every programmer eventually internalizes, often the hard way: The computer does not "know what you mean." It knows what type you said.

If you tell it int and then hand it 3.7, it does not gently round. It chops the .7 off and keeps 3. If you tell it int and ask it to divide 7 / 2, you do not get 3.5. You get 3. The .5 is gone forever. The computer is not malfunctioning — it is doing exactly what you told it.

It's also worth naming the bigger frame: types are the closest thing in programming to honesty. A variable of the wrong type is a lie about what the data is. The compiler will catch some of those lies. The hardest ones to find are the ones the compiler lets through — code that compiles cleanly, runs without crashing, and quietly produces the wrong answer. That experience is its own kind of theological lesson. You can be confident, fluent, even productive, and still be slightly wrong about something foundational. Watch for it.

What Is a Variable, Really?

A variable is a labeled box in memory. The label is its name, the box-size is its type, and what's inside is its value. Pick a type and a value below — watch how the box changes.

score ← the name your code uses 42 int 4 bytes wide 0x7ffe...4c memory address
Try: Set type to int and value to 3.7. Watch what gets stored. Then switch to double — the box gets wider, and the decimal survives.

The Five Types You Need This Week

int — whole numbers

Positive, negative, or zero. No fractional part. The critical thing: dividing two ints produces an int. The trap above. Felt it yet?

double — numbers with decimal points

Real numbers. Anything with a fractional part. The name comes from "double-precision floating-point" — a historical artifact you don't have to care about. Just think: double = "number with a decimal point."

bool — true or false

Holds exactly one of two values: true or false. Named after George Boole, the 19th-century mathematician who built the logic system that lets us reason rigorously about true/false combinations.

char — a single character

Single quotes: 'A'. (Double quotes mean a string.) That distinction matters: 'A' is one byte; "A" is two (the A plus an invisible end-of-string marker).

string — text

You must #include <string> to use it. Holds any length of text. Technically it's a class from the standard library — but treat it as a primitive for now.

An Application Worth Holding in Your Hands

Here's a place these types actually matter. Cosmologists, atheist and Christian, agree on this much: a handful of physical constants are tuned within astonishingly narrow life-permitting ranges. Change them slightly and there is no chemistry, no stars, no us.

The numbers themselves don't argue for anything — but they're worth being able to see. Drag the sliders below. Watch the windows close.

Fine-Tuning Constants Explorer

Five constants. Each one sits in an astonishingly narrow window where life is possible. Tug the slider to perturb the value. Watch the window light up red the instant you leave it.

Sources: Approximate ranges are from the fine-tuning literature (Rees, Collins, Barnes). The point of the widget isn't precision — it's legibility. People reason badly about what they can't see. Make the numbers visible.

What people do with these facts varies. Christians (and plenty of non-Christians) read it as evidence for design. Atheists tend to read it as a multiverse selection effect, or as a brute fact that needs no explanation. Project 2 builds exactly this kind of calculator — and the spec's first warning is that the program is a tool for thinking, not a debate-winning machine.

Coach's Note — The bug above will come for you in Project 2. The fine-tuning math has lots of ratios — (measured - low) / range, perturbed / measured, etc. If even one of those uses two ints, you'll quietly get wrong answers and a card that looks correct but isn't. When in doubt, declare the inputs as double.

Booleans Are Values

You won't write much bool logic this week, but here's the one weird thing to know: in C++, true is 1 and false is 0 when used in arithmetic. You can multiply by a boolean.

int warrior_bonus = (class_char == 'W') * 5;   // adds 5 if 'W', else 0

This is a one-week stopgap. Next chapter you'll meet if statements and never need this trick again. The point of the stopgap is to demonstrate that arithmetic and logic are not separate things in a computer. They're the same thing, looked at from two sides.

This Week's Project

You're ready for Project 2: Fine-Tuning Stats. You'll build a calculator that takes a physical constant, its measured value, the bounds of the life-permitting range, and a perturbation, and reports whether life still works. Every type from this chapter. Every arithmetic operator. The trap is going to try you.


See you next week. We start asking questions.

Check Your Reps

Memory & Types — Quick Check

Question 1 of 4
What does this line print?
int a = 7;
int b = 2;
cout << a / b << endl;
Why: Both a and b are int, so a / b is integer division. The fractional part is truncated (not rounded). The .5 is gone before the assignment ever happens.
Question 2 of 4
Which version correctly computes the real number 3.5?
Why: Both option C and option D promote at least one operand to double before the division runs — so the division produces a double. Options A and B do the integer division first and only cast the (already-truncated) result. The grader may accept either correct answer.
Question 3 of 4
Which of these is the wrong type for storing the cosmological constant 1.1056e-52?
Why: Scientific notation requires a floating-point type. An int stores only whole numbers (and would interpret 1.1056e-52 as approximately zero after the cast). Use double.
Question 4 of 4
What's the difference between 'A' and "A"?
Why: Single quotes mean one char — a single character literal. Double quotes mean a string literal — a sequence of characters plus a hidden null terminator. They are different types, with different sizes, and they compare differently.
YOU FINISHED. NICE WORK.

← WEEK 1: HELLO   ·   BACK TO SYLLABUS