Learning pathOriginal reference
Lesson 2 of 12

Variables, types, and remembered values

Choose a representation, initialize it, and understand what assignment really changes.

A name, a type, and a current value

A variable is a named storage location. A declaration introduces its name and type; initialization gives its first value. Assignment replaces that current value. Read score = score + 5 as “calculate the old score plus five, then store the result.”

Copying a variable copies its value at that moment. It does not create a live connection. In this program, b stays 5 when a changes to 9. The output is 9 5.

C++23
#include <iostream>

int main() {
    int a = 5;
    int b = a;
    a = 9;
    std::cout << a << ' ' << b << '\n';
    return 0;
}

Choose from the constraints

Use int for suitably bounded whole numbers, long long for larger integers or totals, double for fractional values, char for one character such as 'A', and bool for a true/false fact. Text such as "Ahmed" is a string, which you will study in Session 2.

In the training environment, a 32-bit int holds roughly −2.1 billion to 2.1 billion, while a 64-bit long long holds roughly −9.2 × 10¹⁸ to 9.2 × 10¹⁸. C++ itself only guarantees at least 16 bits for int and at least 64 for long long. Choose for the largest intermediate calculation, not merely the inputs.

Floating-point values are usually approximations. Keep exact counts as integers. A bool normally prints as 1 or 0; use a condition when the required answer is YES or NO.

Initialization, constants, and scope

An ordinary local int without initialization does not have a dependable starting zero. Initialize a sum to 0 and a “found something” flag to false. Use meaningful names; names cannot begin with a digit or contain spaces, and score differs from Score.

const int passMark = 50 gives a meaningful name to a value that should not change. A name declared inside a block is normally available from that declaration until the block ends. Declare a result before a block if you need it afterward.

Optional recognition: short, long, float, long double, and unsigned types also exist. Do not assume long is always 64 bits or make every value unsigned simply because the inputs are nonnegative.

Variables & operators

These inputs represent C++ int values. Integer division discards the fractional part.

a + b9
a − b5
a × b14
a / b3
a % b1

Change a and b to compare integer division with remainder.

Guided simulation · Change the inputs to test your understanding.

Pause & predict

After int score = 10; int saved = score; score += 5;, what are score and saved?

Choose an answer
Try it yourself

Turn the idea into code

Two inputs can each be as large as 1,000,000,000. Choose types and print their sum and product. Explain why choosing only from the individual inputs would be misleading.

Give me a hint

The product can reach 10¹⁸. Make the multiplication itself use long long.

Show the explained solution

Read both values into long long. Their product is then computed with the wider type, and the maximum allowed product fits a signed 64-bit integer.

C++23
#include <iostream>

int main() {
    long long a = 0, b = 0;
    std::cin >> a >> b;
    std::cout << a + b << ' ' << a * b << '\n';
    return 0;
}

Your notes

Ready for the next idea?

Mark complete when you can explain it yourself.

From a problem to a programIdentify the input, output, and constraints, then translate a small plan into C++.Variables, types, and remembered valuesChoose a representation, initialize it, and understand what assignment really changes.Arithmetic without surprisesPredict division and remainder, widen before multiplication, and format decimal answers.Conditions and logical decisionsBuild exclusive categories, combine comparisons, and protect operations with short-circuit logic.Optional: other ways to chooseRecognize switch for exact choices and the conditional operator for choosing a value.For loops: make repetition preciseTrace initialization, condition, body, and update—and avoid an extra iteration.While, do-while, and stopping rulesChoose a loop from its stopping condition and read the exact input format.Break, continue, and early answersDistinguish leaving a loop from skipping one iteration, without losing required input.Five patterns worth understandingUse sums, counts, best-so-far candidates, products, and flags to process a stream.Nested loops: rows and columnsTranslate a shape into two loop roles and count the total work.Take a number apart, one digit at a timeUse remainder and division to process decimal digits, including zero.Multiple cases and a debugging mindsetCombine the patterns, isolate each case, and test the boundaries that can break your idea.Arrays: give every value a placeUnderstand indices, initialization, capacity, active length, and the first safe array loop.Scan, count, search, and compareDistinguish values from positions and preserve the exact tie rule a problem asks for.Change a sequence without losing itUpdate, copy, compare, reverse, and inspect neighbours using the correct bounds.Matrices: think in two coordinatesTraverse tables, total rows and columns, inspect diagonals, and protect grid neighbours.Characters become stringsSeparate characters from numbers, then safely inspect and edit managed text.Read the text the problem actually givesChoose words or full lines, preserve empty cases, and reset only the state that needs it.Scan text: count, filter, transformBuild a result from matching characters without losing leading zeros or skipping shifted text.Join, compare, and find textLearn content comparison, lexicographical order, substring counts, and the not-found sentinel.